binaryparse 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/binaryparse.gemspec +17 -17
  2. data/doc/binaryparse.chm +0 -0
  3. data/doc/bphelp.zip +0 -0
  4. data/doc/classes/BinaryBlocker/BitFieldEncoder.html +1 -1
  5. data/doc/classes/BinaryBlocker/Blocker.html +1 -1
  6. data/doc/classes/BinaryBlocker/CountedArrayEncoder.html +1 -1
  7. data/doc/classes/BinaryBlocker/Encoder.html +1 -1
  8. data/doc/classes/BinaryBlocker/FixedArrayEncoder.html +1 -1
  9. data/doc/classes/BinaryBlocker/FixedStringEncoder.html +1 -1
  10. data/doc/classes/BinaryBlocker/FixedUTF16StringEncoder.html +1 -1
  11. data/doc/classes/BinaryBlocker/GroupEncoder.html +1 -1
  12. data/doc/classes/BinaryBlocker/ListOfEncoder.html +1 -1
  13. data/doc/classes/BinaryBlocker/OneOfEncoder.html +1 -1
  14. data/doc/classes/BinaryBlocker/PackedDateEncoder.html +1 -1
  15. data/doc/classes/BinaryBlocker/PackedDateTimeEncoder.html +1 -1
  16. data/doc/classes/BinaryBlocker/PackedNumberEncoder.html +1 -1
  17. data/doc/classes/BinaryBlocker/SimpleEncoder.html +1 -1
  18. data/doc/classes/BinaryBlocker.html +1 -1
  19. data/doc/classes/BufferedIO.html +1 -1
  20. data/doc/classes/TestBlocker/BBDate.html +1 -1
  21. data/doc/classes/TestBlocker/BBList.html +1 -1
  22. data/doc/classes/TestBlocker/BBPacked.html +1 -1
  23. data/doc/classes/TestBlocker/BBString.html +1 -1
  24. data/doc/classes/TestBlocker/BBSub1.html +1 -1
  25. data/doc/classes/TestBlocker/BBSub2.html +1 -1
  26. data/doc/classes/TestBlocker/BBTest1.html +1 -1
  27. data/doc/classes/TestBlocker/BBTest2.html +1 -1
  28. data/doc/classes/TestBlocker/BBTest3.html +1 -1
  29. data/doc/classes/TestBlocker/BBTest4.html +1 -1
  30. data/doc/classes/TestBlocker/BBTest5.html +1 -1
  31. data/doc/classes/TestBlocker/BBTest6.html +1 -1
  32. data/doc/classes/TestBlocker/BBTest7.html +1 -1
  33. data/doc/classes/TestBlocker/BBTime.html +1 -1
  34. data/doc/classes/TestBlocker/BBUTF16.html +1 -1
  35. data/doc/classes/TestBlocker/ItemA.html +1 -1
  36. data/doc/classes/TestBlocker/ItemB.html +1 -1
  37. data/doc/classes/TestBlocker.html +1 -1
  38. data/doc/contents.hhc +670 -0
  39. data/doc/created.rid +1 -1
  40. data/doc/files/lib/blocker_rb.html +1 -1
  41. data/doc/files/lib/buffered_io_rb.html +1 -1
  42. data/doc/files/test/test_blocker_rb.html +1 -1
  43. data/doc/index.hhk +599 -0
  44. data/doc/rdoc.hhp +15 -0
  45. data/examples/cmasqls.rb +82 -0
  46. data/examples/readme.txt +2 -0
  47. data/lib/blocker.rb +13 -2
  48. data/test/test_blocker.rb +25 -1
  49. metadata +9 -2
@@ -0,0 +1,82 @@
1
+ require 'blocker'
2
+ require 'stringio'
3
+
4
+ class CMASqlHeader < BinaryBlocker::Blocker
5
+ has_one :record_type, :uint8, :key => 0
6
+ has_one :register, :uint32
7
+ has_one :transno, :uint32
8
+ has_one :trandate, :string, :length => 8
9
+ has_one :trantime, :string, :length => 4
10
+ has_one :datasource, :string, :length => 64
11
+ end
12
+
13
+ class CMASqlStmt < BinaryBlocker::Blocker
14
+ has_one :record_type, :uint8, :key => 1
15
+ has_one :sql_flags, :uint32
16
+ has_one :sequence, :uint32
17
+ has_one :stmt, :string, :length => 80
18
+ end
19
+
20
+ class CMASqlTrailer < BinaryBlocker::Blocker
21
+ has_one :record_type, :uint8, :key => 99
22
+ has_one :register, :uint32
23
+ has_one :transno, :uint32
24
+ has_one :delim, :string, :length => 3, :key => "\xde\r\n"
25
+ end
26
+
27
+ class CMASQLRec < BinaryBlocker::Blocker
28
+ has_one :header, CMASqlHeader
29
+ has_list_of :items, [CMASqlStmt]
30
+ has_one :footer, CMASqlTrailer
31
+
32
+ StatementStruct = Struct.new(:sql, :flags)
33
+ def statements
34
+ stmts = []
35
+ stmt = StatementStruct.new("")
36
+ items.each do |item|
37
+ stmt.flags = item.sql_flags unless stmt.flags
38
+ stmt.sql += item.stmt
39
+
40
+ if item.sequence == 0
41
+ stmts << stmt
42
+ stmt = StatementStruct.new("")
43
+ end
44
+ end
45
+ stmts
46
+ end
47
+
48
+ def inspect
49
+ result = StringIO.new
50
+ result.puts "Header: #{header.datasource}"
51
+ statements.each do |item|
52
+ result.puts "Stmt (#{item.flags}): #{item.sql}"
53
+ end
54
+ result.puts "Trailer: #{footer.record_type}"
55
+ result.string
56
+ end
57
+ end
58
+
59
+ # Note using binary blocker to parse out "junk" is pretty expensive, but
60
+ # then again you only pay the time when there is an error, so it might not
61
+ # be a bad idea
62
+ class CMASQLJunk < BinaryBlocker::Blocker
63
+ has_one :junk, :int8
64
+ end
65
+
66
+ class CMASQLSmartRec < BinaryBlocker::Blocker
67
+ has_one_of :rec, [CMASQLRec, CMASqlTrailer, CMASQLJunk]
68
+ end
69
+
70
+ def read_trans(io)
71
+ CMASQLRec.new(io)
72
+ end
73
+
74
+ File.open("cmasqls.dat", "rb") do |sqldat|
75
+ until sqldat.eof?
76
+ start = sqldat.pos
77
+ rec = CMASQLSmartRec.new(sqldat)
78
+ next if CMASQLJunk === rec.rec
79
+ puts "#{rec.rec.class}: #{start}-#{sqldat.pos}"
80
+ p rec.rec
81
+ end
82
+ end
@@ -0,0 +1,2 @@
1
+ I need to create some more examples. This is an example from work, but I do not have
2
+ an example datafile for you (yet).
data/lib/blocker.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # might include files with headers and a variable number of body records
5
5
  # followed by a footer. It also supports, packed numeric
6
6
  # (BinaryBlocker::PackedNumberEncoder) and date fields
7
- # (BinaryBlocker::PackedDateEncoder)
7
+ # (BinaryBlocker::PackedDateEncoder)
8
8
 
9
9
  require 'uconv'
10
10
  require 'date'
@@ -81,6 +81,7 @@ module BinaryBlocker
81
81
  # This is the base class for all the various encoders. It supports a variety
82
82
  # of options (as Symbols in a Hash):
83
83
  #
84
+ # default:: used as the default value for the element
84
85
  # pre_block:: passed the value before being blocked
85
86
  # post_block:: passed the blocked value before returned
86
87
  # pre_deblock:: passed the io before attempting to deblock it (hard to imagine
@@ -97,6 +98,7 @@ module BinaryBlocker
97
98
  #
98
99
  # Options (lambda):
99
100
  #
101
+ # default:: used as the default value for the element
100
102
  # pre_block:: passed the value before being blocked
101
103
  # post_block:: passed the blocked value before returned
102
104
  # pre_deblock:: passed the io before attempting to deblock it (hard to imagine
@@ -170,6 +172,7 @@ module BinaryBlocker
170
172
  opts = opts.find { |o| o.respond_to? :to_hash }
171
173
  if opts
172
174
  @opts = @opts.merge(opts)
175
+ @value = @opts[:default]
173
176
  @pre_block = @opts[:pre_block]
174
177
  @pre_deblock = @opts[:pre_deblock]
175
178
 
@@ -348,7 +351,11 @@ module BinaryBlocker
348
351
  end
349
352
 
350
353
  def internal_block(val)
351
- [val].pack(@format)
354
+ if val.nil?
355
+ [val.to_i].pack(@format)
356
+ else
357
+ [val].pack(@format)
358
+ end
352
359
  end
353
360
 
354
361
  def internal_deblock(io)
@@ -603,6 +610,10 @@ module BinaryBlocker
603
610
  @value[offset] = val
604
611
  end
605
612
 
613
+ def each
614
+ @value.each { |v| yield v }
615
+ end
616
+
606
617
  def <<(val)
607
618
  @value << val
608
619
  end
data/test/test_blocker.rb CHANGED
@@ -408,5 +408,29 @@ class TestBlocker < Test::Unit::TestCase
408
408
  assert_equal(b2.items[4].name, ib.name)
409
409
  assert_equal(b2.items[5].name, ib.name)
410
410
  end
411
- end
411
+
412
+ class BBDefaultTest < BinaryBlocker::Blocker
413
+ has_one :foo, :int16, :default => 7
414
+ has_one :bar, :int16
415
+ has_one :str, :string, :length => 20, :default => 'troaeipo'
416
+ end
417
+
418
+ def test_default
419
+ b = BBDefaultTest.new
420
+ assert_equal(7, b.foo)
421
+ assert_equal(nil, b.bar)
422
+ assert_equal('troaeipo', b.str)
423
+
424
+ b.foo = nil
425
+ assert_equal(nil, b.foo)
426
+ b.bar = 3
427
+
428
+ buf = b.block
429
+
430
+ b2 = BBDefaultTest.new(buf)
431
+ assert_equal(0, b2.foo)
432
+ assert_equal(3, b2.bar)
433
+ assert_equal('troaeipo', b2.str)
434
+ end
412
435
 
436
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: binaryparse
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-08-08 00:00:00 -04:00
6
+ version: 0.1.2
7
+ date: 2006-08-29 00:00:00 -04:00
8
8
  summary: Binaryparse is a simple Ruby DSL to parse semi-complicated binary structures. This includes structures dynamic in length, which cannot be handled by DL::Struct or BitStructEx.
9
9
  require_paths:
10
10
  - lib
@@ -34,15 +34,20 @@ files:
34
34
  - examples
35
35
  - lib
36
36
  - test
37
+ - doc/binaryparse.chm
38
+ - doc/bphelp.zip
37
39
  - doc/classes
40
+ - doc/contents.hhc
38
41
  - doc/created.rid
39
42
  - doc/dot
40
43
  - doc/files
41
44
  - doc/fr_class_index.html
42
45
  - doc/fr_file_index.html
43
46
  - doc/fr_method_index.html
47
+ - doc/index.hhk
44
48
  - doc/index.html
45
49
  - doc/rdoc-style.css
50
+ - doc/rdoc.hhp
46
51
  - doc/classes/BinaryBlocker
47
52
  - doc/classes/BinaryBlocker.html
48
53
  - doc/classes/BufferedIO.html
@@ -92,6 +97,8 @@ files:
92
97
  - doc/files/lib/blocker_rb.html
93
98
  - doc/files/lib/buffered_io_rb.html
94
99
  - doc/files/test/test_blocker_rb.html
100
+ - examples/cmasqls.rb
101
+ - examples/readme.txt
95
102
  - lib/blocker.rb
96
103
  - lib/buffered_io.rb
97
104
  - test/test_blocker.rb