binaryparse 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/binaryparse.gemspec CHANGED
@@ -2,12 +2,13 @@ require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = "binaryparse"
5
- s.version = "0.1.3"
5
+ s.version = "0.1.4"
6
6
  s.author = "Patrick Hurley"
7
7
  s.email = "phurley@gmail.com"
8
8
  s.homepage = "http://binaryparse.rubyforge.org/"
9
9
  s.platform = Gem::Platform::RUBY
10
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."
11
12
  s.files = Dir["**/*"]
12
13
  s.autorequire = "binaryparse"
13
14
  end
data/lib/blocker.rb CHANGED
@@ -6,6 +6,9 @@
6
6
  # (BinaryBlocker::PackedNumberEncoder) and date fields
7
7
  # (BinaryBlocker::PackedDateEncoder)
8
8
 
9
+ # Todo
10
+ # Allow nil/blanks by default
11
+
9
12
  require 'uconv'
10
13
  require 'date'
11
14
  require 'time'
@@ -172,7 +175,7 @@ module BinaryBlocker
172
175
  opts = opts.find { |o| o.respond_to? :to_hash }
173
176
  if opts
174
177
  @opts = @opts.merge(opts)
175
- @value = @opts[:default]
178
+ @value = @opts[:default] || @opts[:key] || @value
176
179
  @pre_block = @opts[:pre_block]
177
180
  @pre_deblock = @opts[:pre_deblock]
178
181
 
@@ -352,7 +355,7 @@ module BinaryBlocker
352
355
 
353
356
  def internal_block(val)
354
357
  if val.nil?
355
- [val.to_i].pack(@format)
358
+ [0].pack(@format)
356
359
  else
357
360
  [val].pack(@format)
358
361
  end
@@ -388,6 +391,7 @@ module BinaryBlocker
388
391
 
389
392
  class FixedStringEncoder < SimpleEncoder
390
393
  def initialize(*opts)
394
+ @value = ''
391
395
  initialize_options(*opts)
392
396
 
393
397
  @length = @opts[:length].to_i
@@ -446,7 +450,7 @@ module BinaryBlocker
446
450
  end
447
451
 
448
452
  def internal_block(val)
449
- ["%0#{@length}d" % val].pack(@format)
453
+ ["%0#{@length}d" % val.to_i].pack(@format)
450
454
  end
451
455
 
452
456
  def internal_deblock(io)
@@ -464,14 +468,22 @@ module BinaryBlocker
464
468
  end
465
469
 
466
470
  def internal_block(val)
467
- super val.year * 10000 + val.month * 100 + val.mday
471
+ if val
472
+ super val.year * 10000 + val.month * 100 + val.mday
473
+ else
474
+ super 0
475
+ end
468
476
  end
469
477
 
470
478
  def internal_deblock(io)
471
479
  buffer = io.read(@bytes)
472
480
  result = buffer.unpack(@format)
473
481
  year, month, day = result.first.unpack("A4A2A2").map { |v| v.to_i }
474
- Date.civil(year, month, day)
482
+ if year.zero?
483
+ nil
484
+ else
485
+ Date.civil(year, month, day)
486
+ end
475
487
  end
476
488
 
477
489
  end
@@ -484,14 +496,22 @@ module BinaryBlocker
484
496
  end
485
497
 
486
498
  def internal_block(val)
487
- super sprintf("%04d%02d%02d%02d%02d%02d", val.year, val.month, val.mday, val.hour, val.min, val.sec).to_i
499
+ if val
500
+ super sprintf("%04d%02d%02d%02d%02d%02d", val.year, val.month, val.mday, val.hour, val.min, val.sec).to_i
501
+ else
502
+ super 0
503
+ end
488
504
  end
489
505
 
490
506
  def internal_deblock(io)
491
507
  buffer = io.read(@bytes)
492
508
  result = buffer.unpack(@format)
493
509
  year, month, day, hour, min, sec = result.first.unpack("A4A2A2A2A2A2").map { |v| v.to_i }
494
- Time.local(year, month, day, hour, min, sec)
510
+ if year.zero?
511
+ nil
512
+ else
513
+ Time.local(year, month, day, hour, min, sec)
514
+ end
495
515
  end
496
516
 
497
517
  end
@@ -699,7 +719,7 @@ module BinaryBlocker
699
719
  end
700
720
 
701
721
  def internal_block(val)
702
- [val.raw_value].pack(@type)
722
+ [val.raw_value || 0].pack(@type)
703
723
  end
704
724
 
705
725
  def internal_deblock(io)
data/test/test_blocker.rb CHANGED
@@ -374,12 +374,9 @@ class TestBlocker < Test::Unit::TestCase
374
374
  b.footer = 67
375
375
 
376
376
  ia = ItemA.new
377
- ia.iid = 1
378
- ia.name = 'widget A'
379
377
  assert_equal(1, ia.iid)
380
378
 
381
379
  ib = ItemB.new
382
- ib.iid = 2
383
380
  ib.name = 'widget B'
384
381
 
385
382
  b.items << ia << ib << ia << ib << ib << ib
metadata CHANGED
@@ -3,15 +3,15 @@ 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.3
7
- date: 2006-09-06 00:00:00 -04:00
6
+ version: 0.1.4
7
+ date: 2006-09-13 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
11
11
  email: phurley@gmail.com
12
12
  homepage: http://binaryparse.rubyforge.org/
13
13
  rubyforge_project:
14
- description:
14
+ 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.
15
15
  autorequire: binaryparse
16
16
  default_executable:
17
17
  bindir: bin
@@ -29,12 +29,12 @@ post_install_message:
29
29
  authors:
30
30
  - Patrick Hurley
31
31
  files:
32
+ - binaryparse-0.1.4.gem
32
33
  - binaryparse.gemspec
33
34
  - examples
34
35
  - lib
35
36
  - test
36
37
  - examples/cmasqls.rb
37
- - examples/pms_input_2_test.rb
38
38
  - examples/readme.txt
39
39
  - lib/blocker.rb
40
40
  - lib/buffered_io.rb
@@ -1,70 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- require 'blocker'
4
- require 'stringio'
5
-
6
- class PMS_UserArea < BinaryBlocker::Blocker
7
- has_one :filler, :uint8
8
- end
9
-
10
- class PMS_ItemMaintenance < BinaryBlocker::Blocker
11
- has_one :record_code, :int8, :key=> 0x13
12
- has_one :sku, :string, :length => 8
13
- has_one :update_code, :int16
14
- has_one :dept, :packed, :length => 6
15
- has_one :classs, :packed, :length => 6
16
- has_one :retail_price, :packed, :length => 12
17
- has_one :secondary_price, :packed, :length => 12
18
- has_one :lowest_price, :packed, :length => 12
19
- has_one :lowest_price_date, :date
20
- has_one :item_status_flag, :uint16
21
- has_one :description, :utf16, :length => 40
22
- has_fixed_array :user_area, 13, [PMS_UserArea]
23
- end
24
-
25
- class PmsInputTest < Test::Unit::TestCase
26
-
27
- def write_the_block(fobj,obj)
28
- fobj.write(obj.block)
29
- end
30
-
31
- def test_create_item
32
-
33
- item = PMS_ItemMaintenance.new
34
- item.record_code = 0x13
35
- item.sku = "50000001"
36
- item.update_code = 0
37
- item.dept = 300
38
- item.classs = 20
39
- item.retail_price = 599
40
- item.secondary_price = 499
41
- item.lowest_price = 399
42
- item.lowest_price_date = Date.new(2006,05,13)
43
- item.item_status_flag = 0
44
- item.description = "I made this up!"
45
- (0..12).each {|i|
46
- item.user_area[i] = PMS_UserArea.new
47
- item.user_area[i].filler = i
48
- }
49
-
50
- filename = "testitem"
51
- File.delete(filename) if File.exist?(filename)
52
- f = File.new(filename, "wb+")
53
- (0..5).each do |i|
54
- item.sku = (50000000 + i).to_s
55
- write_the_block(f,item)
56
- end
57
- f.close
58
-
59
-
60
- File.open(filename, "rb") do |fhndl|
61
- until fhndl.eof?
62
- start = fhndl.pos
63
- rec = PMS_ItemMaintenance.new(fhndl)
64
- puts "#{rec.class}: #{start}-#{fhndl.pos}"
65
- p rec.sku + " " + rec.description
66
- end
67
- end
68
- end
69
-
70
- end