Module: IOStruct::HashFmt

Included in:
IOStruct
Defined in:
lib/iostruct/hash_fmt.rb

Constant Summary collapse

KNOWN_FIELD_TYPES_REVERSED =

rubocop:disable Style/WordArray

{
  'C' => ['uint8_t',  'unsigned char', '_BYTE'],
  'S' => ['uint16_t', 'unsigned short'],
  'I' => ['uint32_t', 'unsigned', 'unsigned int'],
  'L' => ['unsigned long'],
  'Q' => ['uint64_t', 'unsigned long long'],

  'c' => ['int8_t',  'char', 'signed char'],
  's' => ['int16_t', 'short', 'signed short'],
  'i' => ['int32_t', 'int', 'signed', 'signed int'],
  'l' => ['long',    'signed long'],
  'q' => ['int64_t', 'long long', 'signed long long'],

  # Big-endian (network byte order)
  'n' => ['uint16_be', 'uint16_t_be', 'be16'],
  'N' => ['uint32_be', 'uint32_t_be', 'be32'],

  # Little-endian (VAX byte order)
  'v' => ['uint16_le', 'uint16_t_le', 'le16'],
  'V' => ['uint32_le', 'uint32_t_le', 'le32'],

  # Floats
  'd' => ['double'],
  'f' => ['float'],
  'E' => ['double_le'],  # double-precision, little-endian
  'e' => ['float_le'],   # single-precision, little-endian
  'G' => ['double_be'],  # double-precision, big-endian
  'g' => ['float_be'],   # single-precision, big-endian
}.freeze
KNOWN_FIELD_TYPES =

rubocop:enable Style/WordArray

KNOWN_FIELD_TYPES_REVERSED.map { |t, a| a.map { |v| [v, t] } }.flatten.each_slice(2).to_h

Instance Method Summary collapse

Instance Method Details

#get_type_size(typename) ⇒ Object

for external use



40
41
42
43
44
# File 'lib/iostruct/hash_fmt.rb', line 40

def get_type_size(typename)
  type_code = KNOWN_FIELD_TYPES[typename.to_s]
  f_size, = PackFmt::FMTSPEC[type_code]
  f_size
end