Class: IntelHex::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/intel_hex/file_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileReader

Returns a new instance of FileReader.



5
6
7
8
9
# File 'lib/intel_hex/file_reader.rb', line 5

def initialize(filename)
  @filename = filename
  @address_base = 0
  @address_mask = 0xffff
end

Instance Method Details

#each_byte_with_addressObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/intel_hex/file_reader.rb', line 27

def each_byte_with_address
  return enum_for(:each_byte_with_address) unless block_given?

  each_record do |record|
    case record.type
    when :data
      record.each_byte_with_address do |byte, offset|
        yield byte, (@address_base + (offset & 0xffff)) & @address_mask
      end
    when :esa
      @address_base = record.value << 4   # bits 4..19 of address
      @address_mask = 0xfffff             # 20 bit address size
    when :ela
      @address_base = record.value << 16  # bits 16..31 of address
      @address_mask = 0xffffffff          # 32 bit address size
    end
  end

  nil
end

#each_recordObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/intel_hex/file_reader.rb', line 11

def each_record
  return enum_for(:each_record) unless block_given?

  file = File.open(@filename, "r")

  begin
    file.each_line do |line|
      yield Record.parse(line.chomp)
    end
  rescue EOFError
    return nil
  end

  nil
end