Class: BinaryParser::TemplateBase
- Inherits:
-
Object
- Object
- BinaryParser::TemplateBase
- Includes:
- BuiltInTemplate
- Defined in:
- lib/binary_parser.rb,
lib/binary_parser/template_base.rb
Overview
load built-in template file
Direct Known Subclasses
BuiltInTemplate::Binary, BuiltInTemplate::Flag, BuiltInTemplate::UInt
Constant Summary
Constants included from BuiltInTemplate
BuiltInTemplate::BCD, BuiltInTemplate::BCD_f1, BuiltInTemplate::BCD_f10, BuiltInTemplate::BCD_f2, BuiltInTemplate::BCD_f3, BuiltInTemplate::BCD_f4, BuiltInTemplate::BCD_f5, BuiltInTemplate::BCD_f6, BuiltInTemplate::BCD_f7, BuiltInTemplate::BCD_f8, BuiltInTemplate::BCD_f9
Class Method Summary collapse
- .Def(parent_structure = nil, &definition_proc) ⇒ Object
- .def_structure(parent_structure = nil, &definition_proc) ⇒ Object
- .def_var_method(name) ⇒ Object
- .structure ⇒ Object
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#binary_bit_length ⇒ Object
Real length(bit) of held binary.
-
#content_description ⇒ Object
String that describes this object.
- #convert_into_abstract_binary(object) ⇒ Object
-
#hold_enough_binary? ⇒ Boolean
Whether real length of held binary is NOT smaller than structure-specified length of binary.
-
#hold_just_binary? ⇒ Boolean
Whether real length of held binary is equal to structure-specified length of binary.
-
#initialize(binary, parent_scope = nil) ⇒ TemplateBase
constructor
A new instance of TemplateBase.
- #load(name, &block) ⇒ Object
- #names ⇒ Object
-
#show(recursively = false, out = STDOUT, depth = 0) ⇒ Object
Print all elements’ information.
-
#structure_bit_length ⇒ Object
Structure-specified length(bit) of binary.
-
#to_chars ⇒ Object
Convert held binary into character-numbers.
-
#to_i ⇒ Object
Convert held binary into unsigned integer.
-
#to_s ⇒ Object
Convert held binary into string encoded in Encoding::BINARY.
Methods included from BuiltInTemplate
Constructor Details
#initialize(binary, parent_scope = nil) ⇒ TemplateBase
Returns a new instance of TemplateBase.
25 26 27 |
# 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 |
Class Method Details
.Def(parent_structure = nil, &definition_proc) ⇒ Object
17 18 19 |
# 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) ⇒ Object
6 7 8 9 10 11 |
# 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) ⇒ Object
13 14 15 |
# File 'lib/binary_parser/template_base.rb', line 13 def self.def_var_method(name) define_method(name){|&block| load(name, &block) } end |
.structure ⇒ Object
21 22 23 |
# File 'lib/binary_parser/template_base.rb', line 21 def self.structure return @structure_def ||= StructureDefinition.new end |
Instance Method Details
#[](name) ⇒ Object
50 51 52 |
# File 'lib/binary_parser/template_base.rb', line 50 def [](name) @scope.load_var(name) end |
#binary_bit_length ⇒ Object
Real length(bit) of held binary
83 84 85 |
# File 'lib/binary_parser/template_base.rb', line 83 def binary_bit_length @scope.abstract_binary.bit_length end |
#content_description ⇒ Object
String that describes this object.
* If you want to print some of this content-description in 'show' method,
override this method.
114 115 116 |
# File 'lib/binary_parser/template_base.rb', line 114 def content_description "" end |
#convert_into_abstract_binary(object) ⇒ Object
42 43 44 45 46 47 48 |
# 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? ⇒ Boolean
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.
99 100 101 |
# File 'lib/binary_parser/template_base.rb', line 99 def hold_enough_binary? structure_bit_length <= binary_bit_length end |
#hold_just_binary? ⇒ Boolean
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.
107 108 109 |
# File 'lib/binary_parser/template_base.rb', line 107 def hold_just_binary? structure_bit_length == binary_bit_length end |
#load(name, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# 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 ⇒ Object
54 55 56 |
# File 'lib/binary_parser/template_base.rb', line 54 def names @scope.names 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.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# 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 ⇒ Object
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.
91 92 93 |
# File 'lib/binary_parser/template_base.rb', line 91 def structure_bit_length @scope.eval_entire_bit_length end |
#to_chars ⇒ Object
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.
78 79 80 |
# File 'lib/binary_parser/template_base.rb', line 78 def to_chars @scope.abstract_binary.to_chars end |
#to_i ⇒ Object
Convert held binary into unsigned integer. Special case:
If held binary's length is 0, this method throws BadBinaryManipulationError.
61 62 63 |
# File 'lib/binary_parser/template_base.rb', line 61 def to_i @scope.abstract_binary.to_i end |
#to_s ⇒ Object
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.
69 70 71 |
# File 'lib/binary_parser/template_base.rb', line 69 def to_s @scope.abstract_binary.to_s end |