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
- .klasses ⇒ Object
- .pack_symbols ⇒ Object
- .pretty_print(obj) ⇒ Object
-
.register_klass(sym, klass) ⇒ Object
To simplify naming of classes and laying out fields in your structures they can be registered:.
-
.sizeof_format(format) ⇒ Object
Handy helper method that returns the size of a given pack/unpack format string.
-
.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.
Class Method Details
.klasses ⇒ Object
14 15 16 |
# File 'lib/blocker.rb', line 14 def klasses @klasses ||= {} end |
.pack_symbols ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/blocker.rb', line 57 def pack_symbols { :int8 => 'c', :uint8 => 'C', :int16 => 's', :uint16 => 'S', :int32 => 'i', :uint32 => 'I', :int64 => 'q', :uint64 => 'Q' } end |
.pretty_print(obj) ⇒ Object
18 19 20 21 |
# File 'lib/blocker.rb', line 18 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)
27 28 29 30 |
# File 'lib/blocker.rb', line 27 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/blocker.rb', line 34 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.
76 77 78 79 80 81 |
# File 'lib/blocker.rb', line 76 def with_guarded_io_pos(io) pos = io.pos status = yield io ensure io.pos = pos unless status end |