class BinaryParser::TemplateBase

load built-in template file

Public Class Methods

Def(parent_structure=nil, &definition_proc) click to toggle source
# File lib/binary_parser/template_base.rb, line 17
def self.Def(parent_structure=nil, &definition_proc)
  def_structure(parent_structure, &definition_proc)
end
def_structure(parent_structure=nil, &definition_proc) click to toggle source
# File lib/binary_parser/template_base.rb, line 6
def self.def_structure(parent_structure=nil, &definition_proc)
  @structure_def = StructureDefinition.new(instance_methods, parent_structure, &definition_proc)
  @structure_def.names.each do |name|
    def_var_method(name)
  end
end
def_var_method(name) click to toggle source
# File lib/binary_parser/template_base.rb, line 13
def self.def_var_method(name)
  define_method(name){|&block| load(name, &block) }
end
new(binary, parent_scope=nil) click to toggle source
# File lib/binary_parser/template_base.rb, line 25
def initialize(binary, parent_scope=nil)
  @scope = Scope.new(self.class.structure, convert_into_abstract_binary(binary), parent_scope)
end
structure() click to toggle source
# File lib/binary_parser/template_base.rb, line 21
def self.structure
  return @structure_def ||= StructureDefinition.new
end

Public Instance Methods

[](name) click to toggle source
# File lib/binary_parser/template_base.rb, line 50
def [](name)
  @scope.load_var(name)
end
binary_bit_length() click to toggle source

Real length(bit) of held binary

# File lib/binary_parser/template_base.rb, line 83
def binary_bit_length
  @scope.abstract_binary.bit_length
end
content_description() click to toggle source

String that describes this object.

* If you want to print some of this content-description in 'show' method,
  override this method.
# File lib/binary_parser/template_base.rb, line 114
def content_description
  ""
end
convert_into_abstract_binary(object) click to toggle source
# File lib/binary_parser/template_base.rb, line 42
def convert_into_abstract_binary(object)
  return object if object.is_a?(AbstractBinary)
  if object.is_a?(String) && object.encoding == Encoding::BINARY
    return AbstractBinary.new(object)
  end
  raise BadManipulationError, "Argument should be AbstractBinary or BINAY String."
end
hold_enough_binary?() click to toggle source

Whether real length of held binary is NOT smaller than structure-specified length of binary. Special case:

If held binary's length is too short to calculate structure-specified length,
this method throws ParsingError.
# File lib/binary_parser/template_base.rb, line 99
def hold_enough_binary?
  structure_bit_length <= binary_bit_length
end
hold_just_binary?() click to toggle source

Whether real length of held binary is equal to structure-specified length of binary. Special case:

If held binary's length is too short to calculate structure-specified length,
this method throws ParsingError.
# File lib/binary_parser/template_base.rb, line 107
def hold_just_binary?
  structure_bit_length == binary_bit_length
end
load(name, &block) click to toggle source
# File lib/binary_parser/template_base.rb, line 29
def load(name, &block)
  if block
    case block.arity
    when 0
      @scope.load_var(name).instance_eval(&block)
    when 1
      block.call(@scope.load_var(name))
    end
  else
    @scope.load_var(name)
  end
end
names() click to toggle source
# File lib/binary_parser/template_base.rb, line 54
def names
  @scope.names
end
show(recursively=false, out=STDOUT, depth=0) click to toggle source

Print all elements’ information. Args:

recursively => Whether print recursively or not. Default is false.
out         => Print target. Default is STDOUT.
# File lib/binary_parser/template_base.rb, line 122
def show(recursively=false, out=STDOUT, depth=0)
  max_name_length = names.inject(5){|max_len, name| [max_len, name.length].max}
  if names.size > 0
    out.puts "#{" " * (depth*2)}*#{"-" * 80}"
  end
  names.each do |name|
    out.puts sprintf("#{" " * (depth*2)}%-#{max_name_length}s  Pos: %6s  Len: %6s  Type: %10s  Cont: %s",
                     name.to_s,
                     @scope.eval_bit_position(name),
                     @scope.eval_bit_length(name),
                     self[name].class.name.split("::").last,
                     self[name] ? self[name].content_description : "Nil")
    self[name].show(true, out, depth + 1) if recursively && self[name]
  end
end
structure_bit_length() click to toggle source

Structure-specified length(bit) of binary. Special case:

If held binary's length is too short to calculate structure-specified length,
this method throws ParsingError.
# File lib/binary_parser/template_base.rb, line 91
def structure_bit_length
  @scope.eval_entire_bit_length
end
to_chars() click to toggle source

Convert held binary into character-numbers. Example: If held binary is “ABC” in ascii, this returns [0x41, 0x42, 0x43]. Special case:

If held binary's length or start position isn't a multiple of 8,
this method throws BadBinaryManipulationError.
# File lib/binary_parser/template_base.rb, line 78
def to_chars
  @scope.abstract_binary.to_chars
end
to_i() click to toggle source

Convert held binary into unsigned integer. Special case:

If held binary's length is 0, this method throws BadBinaryManipulationError.
# File lib/binary_parser/template_base.rb, line 61
def to_i
  @scope.abstract_binary.to_i
end
to_s() click to toggle source

Convert held binary into string encoded in Encoding::BINARY. Special case:

If held binary's length or start position isn't a multiple of 8,
this method throws BadBinaryManipulationError.
# File lib/binary_parser/template_base.rb, line 69
def to_s
  @scope.abstract_binary.to_s
end