Module: PackTemplate

Defined in:
lib/packtemplate/aliases.rb,
lib/packtemplate/builder.rb,
lib/packtemplate/singleton_class.rb

Overview

TODO:
  • Consider aliased name

  • Fix indent

Defined Under Namespace

Classes: Builder

Constant Summary collapse

ALIASES =
{
  unsigned_char:   :C,
  uint16_t:        :S,
  uint32_t:        :L,
  uint64_t:        :Q,

  singend_char:    :c,
  int16_t:        :s,
  int32_t:        :l,
  int64_t:        :q,

  unsigned_short:    :S!,
  unsigned_int:    :I!,
  unsigned_long:    :L!,
  unsigned_long_long:    :Q!,

  signed_short:    :s!,
  signed_int:    :i!,
  signed_long:    :l!,
  signed_long_long:    :q!,

  uint16_t_big_endian:    :n,
  uint32_t_big_endian:    :N,
  uint16_t_little_endian:    :v,
  uint32_t_little_endian:    :V,

  utf8:    :U,
  ber_compressed_int:    :w,

  double_precision: :D,
  single_precision: :F,
  double_precision_little_endian: :E,
  single_precision_little_endian: :e,
  double_precision_big_endian: :G,
  single_precision_big_endian: :g,

  arbitrary_binary_string_space_padded: :A,
  arbitrary_binary_string_null_padded: :a, # Try squash with `Z` in DSL
  arbitrary_binary_string_null_padded_contain_infinite: :Z, # So ugly :(
  bit_string_msb_first: :B,
  bit_string_lsb_first: :b,
  hex_string_high_nibble_first: :H,
  hex_string_low_nibble_first: :h,
  bit_string_msb_first: :B,
  bit_string_lsb_first: :b,
  hex_string_high_nibble_first: :H,
  hex_string_low_nibble_first: :h,
  uu_encoded: :u,
  mime: :M,
  base64: :m,
  structure: :P,
  null_terminated_string: :P,

  absolute_position: :'@',
  back_up_a_byte: :X,
  null_byte: :x
}.freeze

Class Method Summary collapse

Class Method Details

.build(&block) ⇒ String

DSL Scope

Returns:

  • (String)

    template


9
10
11
12
13
# File 'lib/packtemplate/singleton_class.rb', line 9

def build(&block)
  builder = Builder.new
  builder.instance_exec(&block)
  builder.result
end

.for_summary(summary) ⇒ String Also known as: for_dsl

Returns template

Parameters:

  • summary (String)

Returns:

  • (String)

    template


39
40
41
42
43
44
45
46
47
# File 'lib/packtemplate/singleton_class.rb', line 39

def for_summary(summary)
  build do
    summary.each_line do |line|
      dir, len = line.split
      length = len == ":infinite" ? :infinite : Integer(len)
      __send__ dir, length
    end
  end
end

.summarize(template) ⇒ String Also known as: dsl

Returns summary

Parameters:

  • template (String)

Returns:

  • (String)

    summary


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/packtemplate/singleton_class.rb', line 17

def summarize(template)
  ret = ''

  template.scan(/([[email protected]][!<>]?)([0-9]+|\*)?/) do |dir, len|
    length = if len
               len == '*' ? :infinite : Integer(len)
             else
               1
             end

    aliased = ALIASES.key dir.to_sym

    ret << "#{aliased} #{length.inspect}\n"
  end

  ret
end