binaryparse 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/blocker.rb CHANGED
@@ -6,10 +6,11 @@
6
6
  # (BinaryBlocker::PackedNumberEncoder) and date fields
7
7
  # (BinaryBlocker::PackedDateEncoder)
8
8
 
9
- # Todo
10
- # Allow nil/blanks by default
9
+ begin
10
+ require 'uconv'
11
+ rescue LoadError
12
+ end
11
13
 
12
- require 'uconv'
13
14
  require 'date'
14
15
  require 'time'
15
16
 
@@ -221,20 +222,44 @@ module BinaryBlocker
221
222
  # here.
222
223
  class GroupEncoder < Encoder
223
224
  class << self
225
+ def attributes
226
+ @attributes
227
+ end
228
+
229
+ def attributes=(a)
230
+ @attributes=a
231
+ end
232
+
233
+ def lookup
234
+ @lookup
235
+ end
236
+
237
+ def lookup=(l)
238
+ @lookup=l
239
+ end
240
+
241
+ def klasses
242
+ @klasses
243
+ end
244
+
245
+ def klasses=(k)
246
+ @klasses = k
247
+ end
248
+
224
249
  def inherited(obj)
225
250
  obj.instance_eval do
226
- @klasses = BinaryBlocker.klasses.clone
227
- @attributes = []
228
- @lookup = {}
251
+ self.klasses = self.klasses || BinaryBlocker.klasses.clone
252
+ self.attributes = self.attributes || []
253
+ self.lookup = self.lookup || {}
229
254
  end
230
255
  super
231
256
  end
232
257
 
233
258
  # One and only one (this is the easiest :-)
234
259
  def has_one(sym, klass, *opts)
235
- klass = @klasses[klass] if @klasses[klass]
236
- @lookup[sym] = @attributes.size
237
- @attributes << lambda { klass.new(*opts) }
260
+ klass = self.klasses[klass] if self.klasses[klass]
261
+ self.lookup[sym] = self.attributes.size
262
+ self.attributes << lambda { klass.new(*opts) }
238
263
  end
239
264
 
240
265
  def include_klasses(klasses, *opts)
@@ -250,31 +275,31 @@ module BinaryBlocker
250
275
 
251
276
  def has_one_of(sym, klasses, *opts)
252
277
  klasses = include_klasses(klasses, *opts)
253
- @lookup[sym] = @attributes.size
254
- @attributes << lambda { OneOfEncoder.new(klasses, *opts) }
278
+ self.lookup[sym] = self.attributes.size
279
+ self.attributes << lambda { OneOfEncoder.new(klasses, *opts) }
255
280
  end
256
281
 
257
282
  def has_counted_array(sym, count_type, klasses, *opts)
258
283
  klasses = include_klasses(klasses, *opts)
259
- @lookup[sym] = @attributes.size
260
- @attributes << lambda { CountedArrayEncoder.new(count_type, klasses, *opts) }
284
+ self.lookup[sym] = self.attributes.size
285
+ self.attributes << lambda { CountedArrayEncoder.new(count_type, klasses, *opts) }
261
286
  end
262
287
 
263
288
  def has_fixed_array(sym, count, klasses, *opts)
264
289
  klasses = include_klasses(klasses, *opts)
265
- @lookup[sym] = @attributes.size
266
- @attributes << lambda { FixedArrayEncoder.new(count, klasses, *opts) }
290
+ self.lookup[sym] = self.attributes.size
291
+ self.attributes << lambda { FixedArrayEncoder.new(count, klasses, *opts) }
267
292
  end
268
293
 
269
294
  def has_bit_field(sym, type, bit_info, *opts)
270
- @lookup[sym] = @attributes.size
271
- @attributes << lambda { BitFieldEncoder.new(type, bit_info, *opts) }
295
+ self.lookup[sym] = self.attributes.size
296
+ self.attributes << lambda { BitFieldEncoder.new(type, bit_info, *opts) }
272
297
  end
273
298
 
274
299
  def has_list_of(sym, klasses, *opts)
275
300
  klasses = include_klasses(klasses, *opts)
276
- @lookup[sym] = @attributes.size
277
- @attributes << lambda { ListOfEncoder.new(klasses, *opts) }
301
+ self.lookup[sym] = self.attributes.size
302
+ self.attributes << lambda { ListOfEncoder.new(klasses, *opts) }
278
303
  end
279
304
 
280
305
  def register_klass(sym, klass)
@@ -285,14 +310,6 @@ module BinaryBlocker
285
310
  def clear_registered_klasses
286
311
  @klasses = {}
287
312
  end
288
-
289
- def attributes
290
- @attributes
291
- end
292
-
293
- def lookup
294
- @lookup
295
- end
296
313
  end
297
314
 
298
315
  def initialize(*opts)
@@ -302,7 +319,9 @@ module BinaryBlocker
302
319
  end
303
320
 
304
321
  def block
305
- @value.inject("") { |a,b| a + b.block }
322
+ @value.inject("") do |a,b|
323
+ a + b.block
324
+ end
306
325
  end
307
326
 
308
327
  def deblock(io)
@@ -408,6 +427,7 @@ module BinaryBlocker
408
427
 
409
428
  initialize_data(*opts)
410
429
  end
430
+
411
431
  end
412
432
  BinaryBlocker.register_klass(:string, FixedStringEncoder)
413
433
 
@@ -572,7 +592,11 @@ module BinaryBlocker
572
592
  end
573
593
 
574
594
  def internal_block(val)
575
- val.block if val
595
+ if val
596
+ val.block
597
+ else
598
+ @classes.first.block
599
+ end
576
600
  end
577
601
 
578
602
  def internal_deblock(io)
data/test/test_blocker.rb CHANGED
@@ -440,6 +440,43 @@ class TestBlocker < Test::Unit::TestCase
440
440
  assert_nil(t2.t)
441
441
  end
442
442
 
443
+ class BBPackNumRecord < BinaryBlocker::Blocker
444
+ has_one :p, :packed, :length => 2
445
+ end
446
+
447
+ def test_null_packed
448
+ t = BBPackNumRecord.new
449
+ assert(buf = t.block)
450
+
451
+ t2 = BBPackNumRecord.new(buf)
452
+ assert(t2)
453
+ assert_equal(0, t2.p)
454
+ end
455
+
456
+ def test_big_packed
457
+ t = BBPackNumRecord.new
458
+ t.p = 200
459
+ assert(buf = t.block)
460
+ assert_equal(1, buf.size)
461
+
462
+ t2 = BBPackNumRecord.new(buf)
463
+ assert(t2)
464
+ assert(t2.p < 100)
465
+ end
466
+
467
+ class Filler < BinaryBlocker::Blocker
468
+ has_one :f, :uint8
469
+ end
470
+
471
+ class NullFillerTest < BinaryBlocker::Blocker
472
+ has_fixed_array :fooboo, 3, [Filler]
473
+ end
474
+
475
+ def test_null_filler
476
+ t = NullFillerTest.new
477
+ assert(buf = t.block)
478
+ end
479
+
443
480
  class BBSpaceString < BinaryBlocker::Blocker
444
481
  has_one :name, :sstring, :length => 10
445
482
  has_one :last, :string, :length => 5
@@ -451,4 +488,14 @@ class TestBlocker < Test::Unit::TestCase
451
488
  assert(ss)
452
489
  assert_equal("Name", ss.name)
453
490
  end
491
+
492
+ class BBSubClass < BBSpaceString
493
+ end
494
+
495
+ def test_subclass
496
+ buf = "Name Last "
497
+ ss = BBSubClass.new(buf)
498
+ assert(ss)
499
+ #assert_equal("Name", ss.name)
500
+ end
454
501
  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.6
7
- date: 2006-09-14 00:00:00 -04:00
6
+ version: 0.2.0
7
+ date: 2006-09-30 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
@@ -15,7 +15,7 @@ description: Binaryparse is a simple Ruby DSL to parse semi-complicated binary s
15
15
  autorequire: binaryparse
16
16
  default_executable:
17
17
  bindir: bin
18
- has_rdoc: false
18
+ has_rdoc: true
19
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
20
20
  requirements:
21
21
  - - ">"
@@ -29,16 +29,12 @@ post_install_message:
29
29
  authors:
30
30
  - Patrick Hurley
31
31
  files:
32
- - binaryparse.gemspec
33
- - examples
34
- - lib
35
- - test
32
+ - test/test_blocker.rb
33
+ - lib/buffered_io.rb
34
+ - lib/blocker.rb
36
35
  - examples/cmasqls.rb
37
- - examples/readme.txt
38
36
  - examples/voter.rb
39
- - lib/blocker.rb
40
- - lib/buffered_io.rb
41
- - test/test_blocker.rb
37
+ - examples/readme.txt
42
38
  test_files: []
43
39
 
44
40
  rdoc_options: []
data/binaryparse.gemspec DELETED
@@ -1,19 +0,0 @@
1
- require 'rubygems'
2
-
3
- spec = Gem::Specification.new do |s|
4
- s.name = "binaryparse"
5
- s.version = "0.1.6"
6
- s.author = "Patrick Hurley"
7
- s.email = "phurley@gmail.com"
8
- s.homepage = "http://binaryparse.rubyforge.org/"
9
- s.platform = Gem::Platform::RUBY
10
- s.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."
11
- s.description = "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."
12
- s.files = Dir["**/*"]
13
- s.autorequire = "binaryparse"
14
- end
15
-
16
- if __FILE__ == $PROGRAM_NAME
17
- Gem::manage_gems
18
- Gem::Builder.new(spec).build
19
- end