Class: BinaryParser::TemplateBase
- Inherits:
-
Object
- Object
- BinaryParser::TemplateBase
- Includes:
- BuiltInTemplate
- Defined in:
- lib/binary_parser.rb,
lib/template_base.rb
Overview
load built-in template file
Direct Known Subclasses
BuiltInTemplate::Binary, BuiltInTemplate::Flag, BuiltInTemplate::UInt, NamelessTemplate
Class Method Summary collapse
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) ⇒ TemplateBase
constructor
A new instance of TemplateBase.
- #method_missing(name, *args) ⇒ 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.
Constructor Details
#initialize(binary) ⇒ TemplateBase
Returns a new instance of TemplateBase.
16 17 18 |
# File 'lib/template_base.rb', line 16 def initialize(binary) @scope = Scope.new(self.class.structure, convert_into_abstract_binary(binary)) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
28 29 30 |
# File 'lib/template_base.rb', line 28 def method_missing(name, *args) return @scope.method_missing(name, *args) end |
Class Method Details
.Def(&definition_proc) ⇒ Object
10 |
# File 'lib/template_base.rb', line 10 def self.Def(&definition_proc) def_structure(&definition_proc) end |
.def_structure(&definition_proc) ⇒ Object
5 6 7 8 |
# File 'lib/template_base.rb', line 5 def self.def_structure(&definition_proc) used_method_names = self.instance_methods + Scope.instance_methods @structure_def = StructureDefinition.new(used_method_names, &definition_proc) end |
.structure ⇒ Object
12 13 14 |
# File 'lib/template_base.rb', line 12 def self.structure return @structure_def ||= StructureDefinition.new end |
Instance Method Details
#[](name) ⇒ Object
32 33 34 |
# File 'lib/template_base.rb', line 32 def [](name) return @scope.method_missing(name) end |
#binary_bit_length ⇒ Object
Real length(bit) of held binary
65 66 67 |
# File 'lib/template_base.rb', line 65 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.
96 97 98 |
# File 'lib/template_base.rb', line 96 def content_description "" end |
#convert_into_abstract_binary(object) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/template_base.rb', line 20 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.
81 82 83 |
# File 'lib/template_base.rb', line 81 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.
89 90 91 |
# File 'lib/template_base.rb', line 89 def hold_just_binary? structure_bit_length == binary_bit_length end |
#names ⇒ Object
36 37 38 |
# File 'lib/template_base.rb', line 36 def names return @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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/template_base.rb', line 104 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.
73 74 75 |
# File 'lib/template_base.rb', line 73 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.
60 61 62 |
# File 'lib/template_base.rb', line 60 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.
43 44 45 |
# File 'lib/template_base.rb', line 43 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.
51 52 53 |
# File 'lib/template_base.rb', line 51 def to_s @scope.abstract_binary.to_s end |