lib/smcutil/file_reader.rb in smcutil-0.1.2 vs lib/smcutil/file_reader.rb in smcutil-0.1.3

- old
+ new

@@ -1,20 +1,20 @@ module SmcUtil LINE_COMMAND = /((?<type>[[:upper:]]):(?<offset>([[:xdigit:]]{2})+:)?|(?<continue>\+\s+:))(?<length>\d+):(?<data>([[:xdigit:]]{2})+):(?<check>[[:xdigit:]]{2})/ class FileReader - attr_reader :header + attr_reader :headers attr_reader :signature attr_reader :regions def initialize(data) # Setup the collected data - @header = String.new - @signature = String.new - @regions = {} + @headers = [] + @signature = '' + @regions = [] current_region = nil data.each_line.with_index do |line, index| # Zero based indexing is not compatible with how people think about lines in a file @@ -35,21 +35,26 @@ sum = data.codepoints.map { |c| c.to_i }.reduce(:+) & 0xFF raise "LINE #{index}: Checksum does not match; exptected #{check}, got #{sum}" unless sum == check case match[:type] when 'H' - @header += data + @headers << data when 'S' @signature += data when 'D' - current_region = match[:offset].to_i(16) - @regions[current_region] = String.new - @regions[current_region] += data + if match[:offset] + current_offset = match[:offset].scan(/../).map { |x| x.hex.chr }.join.bytes.inject {|a, b| (a << 8) + b } + + current_region = SmcUtil::Region.new current_offset, '' + @regions << current_region + end + + current_region.data += data else raise "LINE #{index}: Command #{match[:type]} not recognised" unless match[:continue] raise "LINE #{index}: Continuation row with no data region set" unless current_region - @regions[current_region] += data + current_region.data += data end end end def self.parse(path) \ No newline at end of file