binaryparse 0.2.0 → 0.2.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.
- data/examples/voter.rb +9 -5
- data/lib/blocker.rb +53 -41
- metadata +9 -6
data/examples/voter.rb
CHANGED
@@ -52,12 +52,16 @@ File.open("voter1.txt", "wb") do |vinfo|
|
|
52
52
|
vinfo.print "Hurley Susan 82 "
|
53
53
|
end
|
54
54
|
|
55
|
-
|
55
|
+
start = Time.new
|
56
|
+
count = 0
|
57
|
+
File.open("v.txt") do |vinfo|
|
56
58
|
until vinfo.eof
|
57
59
|
voter = Voter.new(vinfo)
|
58
|
-
|
60
|
+
count += 1
|
59
61
|
# You might want to do something more interesting here...
|
60
|
-
puts voter.last_name
|
61
|
-
puts voter.first_name
|
62
|
+
#puts voter.last_name
|
63
|
+
#puts voter.first_name
|
62
64
|
end
|
63
|
-
end
|
65
|
+
end
|
66
|
+
stop = Time.new
|
67
|
+
puts "Processed #{count} records in #{stop - start} seconds"
|
data/lib/blocker.rb
CHANGED
@@ -19,7 +19,7 @@ module BinaryBlocker
|
|
19
19
|
def klasses
|
20
20
|
@klasses ||= {}
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
# To simplify naming of classes and laying out fields in your structures
|
24
24
|
# they can be registered:
|
25
25
|
#
|
@@ -28,7 +28,7 @@ module BinaryBlocker
|
|
28
28
|
@klasses ||= {}
|
29
29
|
@klasses[sym] = klass
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
# Handy helper method that returns the size of a given pack/unpack format
|
33
33
|
# string
|
34
34
|
def sizeof_format(format)
|
@@ -53,7 +53,7 @@ module BinaryBlocker
|
|
53
53
|
|
54
54
|
length
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
def pack_symbols
|
58
58
|
{
|
59
59
|
:int8 => 'c',
|
@@ -66,7 +66,7 @@ module BinaryBlocker
|
|
66
66
|
:uint64 => 'Q'
|
67
67
|
}
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
# As we have to process some fields to determine what type they are
|
71
71
|
# (that is we sometimes start and half to backup), this routine takes
|
72
72
|
# any io (that can be repositioned) and yields to a block -- if the
|
@@ -97,7 +97,7 @@ module BinaryBlocker
|
|
97
97
|
# It also supports either a string or io parameter which will be used to
|
98
98
|
# initialize the class
|
99
99
|
class Encoder
|
100
|
-
|
100
|
+
|
101
101
|
# Parameters: (io | buf, options_hash)
|
102
102
|
#
|
103
103
|
# Options (lambda):
|
@@ -130,7 +130,7 @@ module BinaryBlocker
|
|
130
130
|
io.write(result) if io
|
131
131
|
result
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
# This routine takes an io and will parse the stream
|
135
135
|
# on success it returns the object, on failure it
|
136
136
|
# returns a nil
|
@@ -148,19 +148,19 @@ module BinaryBlocker
|
|
148
148
|
self.value || self.valid?
|
149
149
|
end
|
150
150
|
end
|
151
|
-
|
151
|
+
|
152
152
|
def key_value?
|
153
153
|
@opts[:key]
|
154
154
|
end
|
155
|
-
|
155
|
+
|
156
156
|
protected
|
157
|
-
|
157
|
+
|
158
158
|
# Override valid? to allow check constraints on a particular
|
159
159
|
# record type, on failure
|
160
160
|
def valid?
|
161
161
|
true
|
162
162
|
end
|
163
|
-
|
163
|
+
|
164
164
|
def value
|
165
165
|
v = @value
|
166
166
|
if @get_filter
|
@@ -197,7 +197,7 @@ module BinaryBlocker
|
|
197
197
|
if data.respond_to? :to_str
|
198
198
|
raise "Unable to parse block" unless deblock(StringIO.new(data))
|
199
199
|
elsif data.respond_to? :read
|
200
|
-
raise "Unable to parse block" unless deblock(data)
|
200
|
+
raise "Unable to parse block" unless deblock(data)
|
201
201
|
end
|
202
202
|
end
|
203
203
|
|
@@ -208,7 +208,7 @@ module BinaryBlocker
|
|
208
208
|
valid?
|
209
209
|
end && result
|
210
210
|
end
|
211
|
-
|
211
|
+
|
212
212
|
def internal_block(value)
|
213
213
|
value
|
214
214
|
end
|
@@ -217,7 +217,7 @@ module BinaryBlocker
|
|
217
217
|
''
|
218
218
|
end
|
219
219
|
end
|
220
|
-
|
220
|
+
|
221
221
|
# All Encoders that store multiple items subclass from
|
222
222
|
# here.
|
223
223
|
class GroupEncoder < Encoder
|
@@ -237,7 +237,7 @@ module BinaryBlocker
|
|
237
237
|
def lookup=(l)
|
238
238
|
@lookup=l
|
239
239
|
end
|
240
|
-
|
240
|
+
|
241
241
|
def klasses
|
242
242
|
@klasses
|
243
243
|
end
|
@@ -245,7 +245,7 @@ module BinaryBlocker
|
|
245
245
|
def klasses=(k)
|
246
246
|
@klasses = k
|
247
247
|
end
|
248
|
-
|
248
|
+
|
249
249
|
def inherited(obj)
|
250
250
|
obj.instance_eval do
|
251
251
|
self.klasses = self.klasses || BinaryBlocker.klasses.clone
|
@@ -254,7 +254,7 @@ module BinaryBlocker
|
|
254
254
|
end
|
255
255
|
super
|
256
256
|
end
|
257
|
-
|
257
|
+
|
258
258
|
# One and only one (this is the easiest :-)
|
259
259
|
def has_one(sym, klass, *opts)
|
260
260
|
klass = self.klasses[klass] if self.klasses[klass]
|
@@ -278,7 +278,7 @@ module BinaryBlocker
|
|
278
278
|
self.lookup[sym] = self.attributes.size
|
279
279
|
self.attributes << lambda { OneOfEncoder.new(klasses, *opts) }
|
280
280
|
end
|
281
|
-
|
281
|
+
|
282
282
|
def has_counted_array(sym, count_type, klasses, *opts)
|
283
283
|
klasses = include_klasses(klasses, *opts)
|
284
284
|
self.lookup[sym] = self.attributes.size
|
@@ -290,13 +290,13 @@ module BinaryBlocker
|
|
290
290
|
self.lookup[sym] = self.attributes.size
|
291
291
|
self.attributes << lambda { FixedArrayEncoder.new(count, klasses, *opts) }
|
292
292
|
end
|
293
|
-
|
293
|
+
|
294
294
|
def has_bit_field(sym, type, bit_info, *opts)
|
295
295
|
self.lookup[sym] = self.attributes.size
|
296
296
|
self.attributes << lambda { BitFieldEncoder.new(type, bit_info, *opts) }
|
297
297
|
end
|
298
|
-
|
299
|
-
|
298
|
+
|
299
|
+
def has_list_of(sym, klasses, *opts)
|
300
300
|
klasses = include_klasses(klasses, *opts)
|
301
301
|
self.lookup[sym] = self.attributes.size
|
302
302
|
self.attributes << lambda { ListOfEncoder.new(klasses, *opts) }
|
@@ -315,7 +315,19 @@ module BinaryBlocker
|
|
315
315
|
def initialize(*opts)
|
316
316
|
@lookup = self.class.lookup.clone
|
317
317
|
@value = self.class.attributes.map { |a| a.call }
|
318
|
-
super
|
318
|
+
super
|
319
|
+
|
320
|
+
opts.each do |o|
|
321
|
+
if o.respond_to? :to_hash
|
322
|
+
o.keys.each do |key|
|
323
|
+
if pos = @lookup[key.to_sym]
|
324
|
+
unless @value[pos].key_value?
|
325
|
+
@value[pos].value = o[key]
|
326
|
+
end
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
319
331
|
end
|
320
332
|
|
321
333
|
def block
|
@@ -345,7 +357,7 @@ module BinaryBlocker
|
|
345
357
|
end
|
346
358
|
super
|
347
359
|
end
|
348
|
-
|
360
|
+
|
349
361
|
def value
|
350
362
|
self
|
351
363
|
end
|
@@ -358,14 +370,14 @@ module BinaryBlocker
|
|
358
370
|
@value.all? { |a| a.valid? }
|
359
371
|
end
|
360
372
|
end
|
361
|
-
|
373
|
+
|
362
374
|
class SimpleEncoder < Encoder
|
363
|
-
|
375
|
+
|
364
376
|
def self.register(sym, fmt, *opts)
|
365
377
|
klass = Class.new(SimpleEncoder)
|
366
378
|
klass.send(:define_method,:initialize) do |*opts|
|
367
379
|
initialize_options(*opts)
|
368
|
-
|
380
|
+
|
369
381
|
@format = fmt
|
370
382
|
@length = BinaryBlocker.sizeof_format(@format)
|
371
383
|
|
@@ -474,7 +486,7 @@ module BinaryBlocker
|
|
474
486
|
end
|
475
487
|
BinaryBlocker.register_klass(:utf16_string, FixedUTF16StringEncoder)
|
476
488
|
BinaryBlocker.register_klass(:utf16, FixedUTF16StringEncoder)
|
477
|
-
|
489
|
+
|
478
490
|
class PackedNumberEncoder < SimpleEncoder
|
479
491
|
def initialize(*opts)
|
480
492
|
initialize_options(*opts)
|
@@ -502,7 +514,7 @@ module BinaryBlocker
|
|
502
514
|
end
|
503
515
|
end
|
504
516
|
BinaryBlocker.register_klass(:packed, PackedNumberEncoder)
|
505
|
-
|
517
|
+
|
506
518
|
class PackedDateEncoder < PackedNumberEncoder
|
507
519
|
def initialize_options(*opts)
|
508
520
|
super
|
@@ -565,7 +577,7 @@ module BinaryBlocker
|
|
565
577
|
Time.local(year, month, day, hour, min, sec)
|
566
578
|
end
|
567
579
|
end
|
568
|
-
|
580
|
+
|
569
581
|
def valid?
|
570
582
|
case @value
|
571
583
|
when Time : true
|
@@ -580,7 +592,7 @@ module BinaryBlocker
|
|
580
592
|
BinaryBlocker.register_klass(:datetime, PackedDateTimeEncoder)
|
581
593
|
|
582
594
|
class OneOfEncoder < Encoder
|
583
|
-
|
595
|
+
|
584
596
|
def inspect
|
585
597
|
"OneOf #{@classes.join(',')} -> #{@obj.class} -> #{@obj.inspect}"
|
586
598
|
end
|
@@ -620,12 +632,12 @@ module BinaryBlocker
|
|
620
632
|
super
|
621
633
|
end
|
622
634
|
end
|
623
|
-
|
635
|
+
|
624
636
|
def valid?
|
625
637
|
@obj && @obj.valid?
|
626
638
|
end
|
627
639
|
end
|
628
|
-
|
640
|
+
|
629
641
|
class FixedArrayEncoder < GroupEncoder
|
630
642
|
def initialize(count, classes, *opts)
|
631
643
|
initialize_options(*opts)
|
@@ -648,7 +660,7 @@ module BinaryBlocker
|
|
648
660
|
@value = result
|
649
661
|
end
|
650
662
|
end
|
651
|
-
|
663
|
+
|
652
664
|
def [](offset)
|
653
665
|
raise RangeError.new("Access (#{offset}) out of range (#{@count})") unless (0...@count) === offset
|
654
666
|
@value[offset]
|
@@ -659,7 +671,7 @@ module BinaryBlocker
|
|
659
671
|
@value[offset] = val
|
660
672
|
end
|
661
673
|
end
|
662
|
-
|
674
|
+
|
663
675
|
class ListOfEncoder < GroupEncoder
|
664
676
|
def initialize(classes, *opts)
|
665
677
|
initialize_options(*opts)
|
@@ -684,7 +696,7 @@ module BinaryBlocker
|
|
684
696
|
end
|
685
697
|
@value = result
|
686
698
|
end
|
687
|
-
|
699
|
+
|
688
700
|
def [](offset)
|
689
701
|
raise RangeError.new("Access (#{offset}) out of range (#{@value.size})") unless (0...@value.size) === offset
|
690
702
|
@value[offset]
|
@@ -708,7 +720,7 @@ module BinaryBlocker
|
|
708
720
|
end
|
709
721
|
alias length size
|
710
722
|
end
|
711
|
-
|
723
|
+
|
712
724
|
class CountedArrayEncoder < GroupEncoder
|
713
725
|
def initialize(count_type, classes, *opts)
|
714
726
|
# this is dynamic now, but we init to zero for the range checking
|
@@ -736,7 +748,7 @@ module BinaryBlocker
|
|
736
748
|
end
|
737
749
|
@value = result
|
738
750
|
end
|
739
|
-
|
751
|
+
|
740
752
|
def [](offset)
|
741
753
|
raise RangeError.new("Access (#{offset}) out of range (#{@count})") unless (0...@count) === offset
|
742
754
|
@value[offset]
|
@@ -757,7 +769,7 @@ module BinaryBlocker
|
|
757
769
|
end
|
758
770
|
alias length size
|
759
771
|
end
|
760
|
-
|
772
|
+
|
761
773
|
class BitFieldEncoder < Encoder
|
762
774
|
def initialize(type, bit_info, *opts)
|
763
775
|
@type = BinaryBlocker.pack_symbols[type] || type
|
@@ -782,7 +794,7 @@ module BinaryBlocker
|
|
782
794
|
initialize_options(*opts)
|
783
795
|
initialize_data(*opts)
|
784
796
|
end
|
785
|
-
|
797
|
+
|
786
798
|
def internal_block(val)
|
787
799
|
[val.raw_value || 0].pack(@type)
|
788
800
|
end
|
@@ -792,7 +804,7 @@ module BinaryBlocker
|
|
792
804
|
result = buffer.unpack(@type)
|
793
805
|
result.first
|
794
806
|
end
|
795
|
-
|
807
|
+
|
796
808
|
def value
|
797
809
|
self
|
798
810
|
end
|
@@ -820,9 +832,9 @@ module BinaryBlocker
|
|
820
832
|
# which seemed wrong, not sure why
|
821
833
|
end
|
822
834
|
end
|
823
|
-
|
835
|
+
|
824
836
|
end
|
825
|
-
|
837
|
+
|
826
838
|
class Blocker < GroupEncoder
|
827
839
|
def inspect
|
828
840
|
"Blocker: #{@value.inspect} (index: #{@lookup})"
|
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.2.
|
7
|
-
date: 2006-
|
6
|
+
version: 0.2.2
|
7
|
+
date: 2006-11-03 00:00:00 -05: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
|
@@ -29,12 +29,15 @@ post_install_message:
|
|
29
29
|
authors:
|
30
30
|
- Patrick Hurley
|
31
31
|
files:
|
32
|
-
-
|
33
|
-
- lib
|
34
|
-
-
|
32
|
+
- examples
|
33
|
+
- lib
|
34
|
+
- test
|
35
35
|
- examples/cmasqls.rb
|
36
|
-
- examples/voter.rb
|
37
36
|
- examples/readme.txt
|
37
|
+
- examples/voter.rb
|
38
|
+
- lib/blocker.rb
|
39
|
+
- lib/buffered_io.rb
|
40
|
+
- test/test_blocker.rb
|
38
41
|
test_files: []
|
39
42
|
|
40
43
|
rdoc_options: []
|