Module: BinaryBlocker

Defined in:
lib/blocker.rb

Defined Under Namespace

Classes: BitFieldEncoder, Blocker, ByteStringEncoder, CountedArrayEncoder, Encoder, FixedArrayEncoder, FixedStringEncoder, FixedUTF16StringEncoder, GroupEncoder, ListOfEncoder, NewPackedNumberEncoder, OneOfEncoder, PackedDateEncoder, PackedDateEncoderMMDDYYYY, PackedDateTimeEncoder, PackedDateTimeHHMMEncoder, PackedDateTimeMMDDYYYYHHMMEncoder, PackedNumberEncoder, SimpleEncoder, SpacedStringEncoder

Class Method Summary collapse

Class Method Details

.klassesObject



19
20
21
# File 'lib/blocker.rb', line 19

def klasses
  @klasses ||= {}
end

.pack_symbolsObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/blocker.rb', line 62

def pack_symbols
  { 
    :int8   => 'c', 
    :uint8  => 'C',
    :int16  => 's',
    :uint16 => 'S',
    :int32  => 'i',   
    :uint32 => 'I',
    :int64  => 'q',    
    :uint64 => 'Q'
  }
end

.pretty_print(obj) ⇒ Object



23
24
25
26
# File 'lib/blocker.rb', line 23

def pretty_print(obj)
  obj.text "FOO"
  #obj.text self.class.pretty_print
end

.register_klass(sym, klass) ⇒ Object

To simplify naming of classes and laying out fields in your structures they can be registered:

BinaryBlocker.register_klass(:string, FixedStringEncoder)



32
33
34
35
# File 'lib/blocker.rb', line 32

def register_klass(sym, klass)
  @klasses ||= {}
  @klasses[sym] = klass
end

.sizeof_format(format) ⇒ Object

Handy helper method that returns the size of a given pack/unpack format string



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/blocker.rb', line 39

def sizeof_format(format)
  length = 0
  format.scan(/(\S_?)\s*(\d*)/).each do |directive,count|
    count = count.to_i
    count = 1 if count == 0
    
    length += case directive
    when 'A', 'a', 'C', 'c', 'Z', 'x' ; count
    when 'B', 'b' ; (count / 8.0).ceil
    when 'D', 'd', 'E', 'G' ; count * 8
    when 'e', 'F', 'f', 'g' ; count * 4
    when 'H', 'h' ; (count / 2.0).ceil
    when 'I', 'i', 'L', 'l', 'N', 'V' ; count * 4
    when 'n', 'S', 's', 'v' ; count * 2
    when 'Q', 'q' ; count * 8
    when 'X' ; count * -1
    else raise ArgumentError.new("#{directive} is not supported in sizeof_format")
    end
  end
  
  length
end

.with_guarded_io_pos(io) ⇒ Object

As we have to process some fields to determine what type they are (that is we sometimes start and half to backup), this routine takes any io (that can be repositioned) and yields to a block – if the block returns not true it will reset the original position of the io stream. If you need to use BinaryBlocker with a non-repositioning stream (like a TCP/IP stream), see the handy BufferedIO class.



81
82
83
84
85
86
# File 'lib/blocker.rb', line 81

def with_guarded_io_pos(io)
  pos = io.pos
  status = yield io
ensure
  io.pos = pos unless status
end