Class: BinaryParser::LoopList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/binary_parser/loop_list.rb

Direct Known Subclasses

WhileList

Instance Method Summary collapse

Constructor Details

#initialize(definition, abstract_binary, parent_scope) ⇒ LoopList

Returns a new instance of LoopList.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/binary_parser/loop_list.rb', line 5

def initialize(definition, abstract_binary, parent_scope)
  list, rest_binary = [], abstract_binary
  while rest_binary.bit_length > 0
    template = definition.klass.new(rest_binary, parent_scope)
    if template.structure_bit_length == 0
      raise ParsingError, "0 bit-length repetition happens. This means infinite loop."
    end
    rest_binary = rest_binary.sub(:bit_index => template.structure_bit_length)
    list << template
  end
  @list = list
end

Instance Method Details

#[](index) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/binary_parser/loop_list.rb', line 22

def [](index)
  unless @list[index]
    raise BadManipulationError, "Index is out of bounds. List size is #{@list.size}." +
      "You accessed list[#{index}]." 
  end
  return @list[index]
end

#content_descriptionObject

String that describes this object.



37
38
39
# File 'lib/binary_parser/loop_list.rb', line 37

def content_description
  "list with #{size} elements"
end

#each(&block) ⇒ Object



18
19
20
# File 'lib/binary_parser/loop_list.rb', line 18

def each(&block)
  @list.each(&block)
end

#show(recursively = false, out = STDOUT, depth = 0) ⇒ Object

Print all elements’ information. Args:

recursively => Whether print recursively or not. Default is false.
out         => Print target. Default is STDOUT.


45
46
47
48
49
50
51
52
53
54
# File 'lib/binary_parser/loop_list.rb', line 45

def show(recursively=false, out=STDOUT, depth=0)
  @list.each_with_index do |element, i|
    out.puts sprintf(" " * (depth * 2) + "%-5s::%20s  Len: %s Cont: %s",
                     "[#{i}]",
                     element.class.name ? element.class.name.split("::").last : "AnonymouseTemplate",
                     element.structure_bit_length,
                     element.content_description)
    element.show(true, out, depth + 1) if recursively
  end
end

#sizeObject Also known as: length



30
31
32
# File 'lib/binary_parser/loop_list.rb', line 30

def size
  return @list.size
end