Class: BinParserElement

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

Defined Under Namespace

Modules: Endianness, Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, offset, size, endianness) ⇒ BinParserElement

Returns a new instance of BinParserElement.



22
23
24
25
26
27
# File 'lib/binp.rb', line 22

def initialize(name, offset, size, endianness)
  @name = name
  @offset = offset
  @size = size
  @endianness = endianness
end

Instance Attribute Details

#endiannessObject (readonly)

Returns the value of attribute endianness.



20
21
22
# File 'lib/binp.rb', line 20

def endianness
  @endianness
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/binp.rb', line 20

def name
  @name
end

#offsetObject (readonly)

Returns the value of attribute offset.



20
21
22
# File 'lib/binp.rb', line 20

def offset
  @offset
end

#sizeObject (readonly)

Returns the value of attribute size.



20
21
22
# File 'lib/binp.rb', line 20

def size
  @size
end

Class Method Details

.calculate_pack_template_string(size) ⇒ Object



79
80
81
# File 'lib/binp.rb', line 79

def self.calculate_pack_template_string(size)
  'C' * size
end

.calculate_unpack_template_string(type, endianness) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/binp.rb', line 47

def self.calculate_unpack_template_string(type, endianness)

  # 特殊な条件
  case type
  when BinParserElement::Type::UINT16
    if endianness == BinParserElement::Endianness::BIG_ENDIAN
      # ビッグエンディアン、符号なし 16 bit 整数
      return 'n'
    else
      # リトルエンディアン、符号なし 16 bit 整数
      return 'v'
    end
  when BinParserElement::Type::UINT32
    if endianness == BinParserElement::Endianness::BIG_ENDIAN
      # ビッグエンディアン、符号あり 32 bit 整数
      return 'N'
    else
      # リトルエンディアン、符号あり 32 bit 整数
      return 'V'
    end
  when BinParserElement::Type::UINT8
    # 符号なし 8 bit 整数
    return 'C'
  when BinParserElement::Type::INT8
    # 符号あり 8 bit 整数
    return 'c'
  end

  # その他
  type[:value] + endianness[:value]
end

.get_flags(uint8_array, config) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/binp.rb', line 94

def self.get_flags(uint8_array, config)
  offset = config['offset']
  layout = config['layout']
  target = uint8_array.slice(offset, 1)
  layout.map { |e|
    c = config.dup
    c['name'] = e['name']

    # ビットが立っているかの確認
    c['value'] = target[0] & (1 << e['position'])

    # true_label, false_label が存在すればそれに設定。
    # 存在しなければデフォルト値('ON', 'OFF')に設定。
    if c['value'] != 0
      c['value'] = e.fetch('true_label', 'ON')
    else
      c['value'] = e.fetch('false_label', 'OFF')
    end
    c
  }
end

.get_value(uint8_array, offset, size, type, endianness, value_labels = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/binp.rb', line 83

def self.get_value(uint8_array, offset, size, type, endianness, value_labels={})
  target = uint8_array.slice(offset, size)
  pack_template_string = calculate_pack_template_string(size)
  unpack_template_string = calculate_unpack_template_string(type, endianness)
  value = target.pack(pack_template_string).unpack(unpack_template_string)[0]

  # value_label が設定されている数値であれば、それに置き換える
  value_labels ||= {}
  value_labels.fetch(value, value)
end