libbin 1.0.3 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fdf975677061cebffe202e1d86030fd8c84d32f28c90c6907abd964d854e0cf
4
- data.tar.gz: e54ea09722e208c8eb1368a73dbb9aa452e324e2b18dc59b007a826cb23c680a
3
+ metadata.gz: 2d6b93ee63ec0916ee685a6b1b646a4fb6a742e31158049bc46f387db3a4ee27
4
+ data.tar.gz: dc8c1ea1218694f5b4ad8a53baa60e721a7b533474db9674939b40d25cb7ee06
5
5
  SHA512:
6
- metadata.gz: 8490ee9cdec4a7268aeb19d2cd6bd37fa5af61930f1a3b3c1134e10768c211c2c67030054eb00bbd4c85ff6beff7c8b290ff4927ba86b45312168e49894695a3
7
- data.tar.gz: 95a9b0b79b0bca2f9c7f8eef6154788ce2838ac436b23f958b67af3485bee706dfee7f3720a6bb29e88746a4ba04967479347f9fde9250160ad9a0f57a0b0a61
6
+ metadata.gz: 808407aad8f8de68723199114b84434027b3a936360496785574cfdb34567c6cf6bcffcfb044c02885c9211794bd34b25fb916323724cedfd69dfddd34cf0254
7
+ data.tar.gz: 005fb654400e4b7eaa5c9137aa7ea55d30f61b62d9c4c6f13b385046118b5dcbddda3347331397d1f1cb6cf217c3aef3a50ebd493dc123ea1c8a8f792d6fcb69
data/LICENSE CHANGED
File without changes
File without changes
data/ext/libbin/half.c CHANGED
File without changes
data/ext/libbin/half.h CHANGED
File without changes
@@ -11,7 +11,7 @@ static VALUE pghalf_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
11
11
  Check_Type(str, T_STRING);
12
12
  Check_Type(pack_str, T_STRING);
13
13
  VALUE arr = rb_funcall(str, rb_intern("unpack"), 1, pack_str);
14
- uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
14
+ uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
15
15
  union float_u res;
16
16
 
17
17
  res.i = pghalf_to_float(val);
@@ -22,7 +22,7 @@ static VALUE half_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
22
22
  Check_Type(str, T_STRING);
23
23
  Check_Type(pack_str, T_STRING);
24
24
  VALUE arr = rb_funcall(str, rb_intern("unpack"), 1, pack_str);
25
- uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
25
+ uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
26
26
  union float_u res;
27
27
 
28
28
  res.i = half_to_float(val);
@@ -32,25 +32,25 @@ static VALUE half_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
32
32
  static VALUE pghalf_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
33
33
  Check_Type(number, T_FLOAT);
34
34
  union float_u val;
35
- uint16_t res;
35
+ uint16_t res;
36
36
 
37
37
  val.f = NUM2DBL(number);
38
38
  res = pghalf_from_float(val.i);
39
- VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
39
+ VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
40
40
 
41
- return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
41
+ return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
42
42
  }
43
43
 
44
44
  static VALUE half_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
45
45
  Check_Type(number, T_FLOAT);
46
46
  union float_u val;
47
- uint16_t res;
47
+ uint16_t res;
48
48
 
49
49
  val.f = NUM2DBL(number);
50
50
  res = half_from_float(val.i);
51
- VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
51
+ VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
52
52
 
53
- return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
53
+ return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
54
54
  }
55
55
 
56
56
  void Init_libbin_c() {
data/ext/libbin/pghalf.c CHANGED
File without changes
data/ext/libbin/pghalf.h CHANGED
File without changes
File without changes
@@ -3,9 +3,10 @@ module LibBin
3
3
  module RangeRefinement
4
4
  refine Range do
5
5
  def +(other)
6
- Range::new(first <= other.first ? first : other.first,
7
- last >= other.last ? last : other.last,
8
- exclude_end?)
6
+ return other.dup unless min
7
+ return self.dup unless other.min
8
+ Range::new(min <= other.min ? min : other.min,
9
+ max >= other.max ? max : other.max)
9
10
  end
10
11
  end
11
12
  end
@@ -26,7 +27,7 @@ module LibBin
26
27
  @members = nil
27
28
  else
28
29
  @members = args[0]
29
- @range = @members.values.flatten.compact.collect(&:range).reduce { |memo, obj| memo + obj }
30
+ @range = @members.values.flatten.compact.collect(&:range).reduce(:+)
30
31
  end
31
32
  end
32
33
 
@@ -52,7 +53,7 @@ module LibBin
52
53
  if args.length == 2
53
54
  @range = Range::new(args[0], args[1])
54
55
  else
55
- @range = args[0].values.flatten.compact.collect(&:range).reduce { |memo, obj| memo + obj }
56
+ @range = args[0].values.flatten.compact.collect(&:range).reduce(:+)
56
57
  end
57
58
  end
58
59
 
@@ -70,14 +71,52 @@ module LibBin
70
71
 
71
72
  end
72
73
 
74
+ class Field
75
+ attr_reader :name,
76
+ :type,
77
+ :length,
78
+ :count,
79
+ :offset,
80
+ :sequence,
81
+ :condition
82
+
83
+ def sequence?
84
+ @sequence
85
+ end
86
+
87
+ def relative_offset?
88
+ @relative_offset
89
+ end
90
+
91
+ def initialize(name, type, length, count, offset, sequence, condition, relative_offset)
92
+ @name = name
93
+ @type = type
94
+ @length = length
95
+ @count = count
96
+ @offset = offset
97
+ @sequence = sequence
98
+ @condition = condition
99
+ @relative_offset = relative_offset
100
+ end
101
+
102
+ end
103
+
73
104
  class DataConverter
74
105
 
75
- rl = lambda { |type, str|
76
- str.unpack(type.to_s).first
106
+ rl = lambda { |type, str, number = nil|
107
+ if number
108
+ str.unpack(type.to_s+number.to_s)
109
+ else
110
+ str.unpack(type.to_s).first
111
+ end
77
112
  }
78
113
 
79
- sl = lambda { |type, value|
80
- [value].pack(type.to_s)
114
+ sl = lambda { |type, value, number = nil|
115
+ if number
116
+ value.pack(type.to_s+number.to_s)
117
+ else
118
+ [value].pack(type.to_s)
119
+ end
81
120
  }
82
121
 
83
122
  l = lambda { |type|
@@ -161,6 +200,7 @@ module LibBin
161
200
  :D => 8,
162
201
  :E => 8,
163
202
  :G => 8,
203
+ :a => 1,
164
204
  :"a*" => -1,
165
205
  :half => 2,
166
206
  :half_le => 2,
@@ -186,6 +226,50 @@ module LibBin
186
226
  }
187
227
  }
188
228
 
229
+ rhl = lambda { |type, str, number = nil|
230
+ if number
231
+ number.times.collect { |i| LibBin::half_from_string(str[i*2,2], type) }
232
+ else
233
+ LibBin::half_from_string(str, type)
234
+ end
235
+ }
236
+
237
+ shl = lambda { |type, value, number = nil|
238
+ if number
239
+ str = ""
240
+ number.times { |i| str << LibBin::half_to_string(value[i], type) }
241
+ str
242
+ else
243
+ LibBin::half_to_string(value, type)
244
+ end
245
+ }
246
+
247
+ hl = lambda { |type|
248
+ [rhl.curry[type], shl.curry[type]]
249
+ }
250
+
251
+ rpghl = lambda { |type, str, number = nil|
252
+ if number
253
+ number.times.collect { |i| LibBin::pghalf_from_string(str[i*2,2], type) }
254
+ else
255
+ LibBin::pghalf_from_string(str, type)
256
+ end
257
+ }
258
+
259
+ spghl = lambda { |type, value, number = nil|
260
+ if number
261
+ str = ""
262
+ number.times { |i| str << LibBin::pghalf_to_string(value[i], type) }
263
+ str
264
+ else
265
+ LibBin::pghalf_to_string(value, type)
266
+ end
267
+ }
268
+
269
+ pghl = lambda { |type|
270
+ [rpghl.curry[type], spghl.curry[type]]
271
+ }
272
+
189
273
  DATA_ENDIAN[true].merge!( {
190
274
  :c => l["c"],
191
275
  :C => l["C"],
@@ -217,19 +301,12 @@ module LibBin
217
301
  :D => l["G"],
218
302
  :E => l["E"],
219
303
  :G => l["G"],
220
- :"a*" => l["a*"],
221
- :half => [ lambda { |str| LibBin::half_from_string(str, "S>") },
222
- lambda { |v| LibBin::half_to_string(v, "S>") } ],
223
- :half_le => [ lambda { |str| LibBin::half_from_string(str, "S<") },
224
- lambda { |v| LibBin::half_to_string(v, "S<") } ],
225
- :half_be => [ lambda { |str| LibBin::half_from_string(str, "S>") },
226
- lambda { |v| LibBin::half_to_string(v, "S>") } ],
227
- :pghalf => [ lambda { |str| LibBin::pghalf_from_string(str, "S>") },
228
- lambda { |v| LibBin::pghalf_to_string(v, "S>") } ],
229
- :pghalf_le => [ lambda { |str| LibBin::pghalf_from_string(str, "S<") },
230
- lambda { |v| LibBin::pghalf_to_string(v, "S<") } ],
231
- :pghalf_be => [ lambda { |str| LibBin::pghalf_from_string(str, "S>") },
232
- lambda { |v| LibBin::pghalf_to_string(v, "S>") } ]
304
+ :half => hl["S>"],
305
+ :half_le => hl["S<"],
306
+ :half_be => hl["S>"],
307
+ :pghalf => pghl["S>"],
308
+ :pghalf_le => pghl["S<"],
309
+ :pghalf_be => pghl["S>"]
233
310
  } )
234
311
  DATA_ENDIAN[false].merge!( {
235
312
  :c => l["c"],
@@ -262,19 +339,12 @@ module LibBin
262
339
  :D => l["E"],
263
340
  :E => l["E"],
264
341
  :G => l["G"],
265
- :"a*" => l["a*"],
266
- :half => [ lambda { |str| LibBin::half_from_string(str, "S<") },
267
- lambda { |v| LibBin::half_to_string(v, "S<") } ],
268
- :half_le => [ lambda { |str| LibBin::half_from_string(str, "S<") },
269
- lambda { |v| LibBin::half_to_string(v, "S<") } ],
270
- :half_be => [ lambda { |str| LibBin::half_from_string(str, "S>") },
271
- lambda { |v| LibBin::half_to_string(v, "S>") } ],
272
- :pghalf => [ lambda { |str| LibBin::pghalf_from_string(str, "S<") },
273
- lambda { |v| LibBin::pghalf_to_string(v, "S<") } ],
274
- :pghalf_le => [ lambda { |str| LibBin::pghalf_from_string(str, "S<") },
275
- lambda { |v| LibBin::pghalf_to_string(v, "S<") } ],
276
- :pghalf_be => [ lambda { |str| LibBin::pghalf_from_string(str, "S>") },
277
- lambda { |v| LibBin::pghalf_to_string(v, "S>") } ]
342
+ :half => hl["S<"],
343
+ :half_le => hl["S<"],
344
+ :half_be => hl["S>"],
345
+ :pghalf => pghl["S<"],
346
+ :pghalf_le => pghl["S<"],
347
+ :pghalf_be => pghl["S>"]
278
348
  } )
279
349
 
280
350
 
@@ -284,8 +354,9 @@ module LibBin
284
354
  @size
285
355
  end
286
356
 
287
- def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape)
288
- kind::new(previous_offset, previous_offset + @size - 1)
357
+ def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape, length = nil)
358
+ length = 1 unless length
359
+ kind::new(previous_offset, previous_offset - 1 + length * @size)
289
360
  end
290
361
 
291
362
  def self.init(symbol)
@@ -295,20 +366,22 @@ module LibBin
295
366
  @rl_le, @sl_le = DATA_ENDIAN[false][symbol]
296
367
  end
297
368
 
298
- def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil)
299
- str = input.read(@size)
300
- input_big ? @rl_be[str] : @rl_le[str]
369
+ def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
370
+ l = (length ? length : 1)
371
+ str = input.read(@size*l)
372
+ input_big ? @rl_be[str, length] : @rl_le[str, length]
301
373
  end
302
374
 
303
- def self.dump(value, output, output_big = LibBin::default_big?, _ = nil, _ = nil)
304
- str = (output_big ? @sl_be[value] : @sl_le[value])
375
+ def self.dump(value, output, output_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
376
+ str = (output_big ? @sl_be[value, length] : @sl_le[value, length])
305
377
  output.write(str)
306
378
  end
307
379
 
308
- def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, _ = nil, _ = nil)
309
- str = input.read(@size)
310
- value = (input_big ? @rl_be[str] : @rl_le[str])
311
- str = (output_big ? @sl_be[value] : @sl_le[value])
380
+ def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, _ = nil, _ = nil, length = nil)
381
+ l = (length ? length : 1)
382
+ str = input.read(@size*l)
383
+ value = (input_big ? @rl_be[str, length] : @rl_le[str, length])
384
+ str = (output_big ? @sl_be[value, length] : @sl_le[value, length])
312
385
  output.write(str)
313
386
  value
314
387
  end
@@ -317,40 +390,48 @@ module LibBin
317
390
 
318
391
  class Str < Scalar
319
392
 
320
- def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil)
321
- str = (@size < 0 ? input.readline("\x00") : input.read(@size))
322
- input_big ? @rl_be[str] : @rl_le[str]
393
+ def self.size(value, previous_offset = 0, parent = nil, index = nil, length = nil)
394
+ length ? length : value.size
395
+ end
396
+
397
+ def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
398
+ str = (length ? input.read(length) : input.readline("\x00"))
323
399
  end
324
400
 
325
- def self.convert(input, output, input_big = LibBin::default_big?, output_big = !LibBin::default_big, _ = nil, _ = nil)
326
- str = (@size < 0 ? input.readline("\x00") : input.read(@size))
327
- value = (input_big ? @rl_be[str] : @rl_le[str])
328
- str = (output_big ? @sl_be[value] : @sl_le[value])
401
+ def self.convert(input, output, input_big = LibBin::default_big?, output_big = !LibBin::default_big, _ = nil, _ = nil, length = nil)
402
+ str = (length ? input.read(length) : input.readline("\x00"))
329
403
  output.write(str)
330
- value
404
+ str
331
405
  end
332
406
 
333
- def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape)
334
- if @size < 0
335
- kind::new(previous_offset, previous_offset + value.size - 1)
407
+ def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape, length = nil)
408
+ if length
409
+ kind::new(previous_offset, previous_offset + length - 1)
336
410
  else
337
- kind::new(previous_offset, previous_offset + @size - 1)
411
+ kind::new(previous_offset, previous_offset + value.size - 1)
338
412
  end
339
413
  end
340
414
 
415
+ def self.dump(value, output, output_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
416
+ if length
417
+ output.write([value].pack("Z#{length}"))
418
+ else
419
+ output.write(value)
420
+ end
421
+ end
341
422
  end
342
423
 
343
- def self.register_field(field, type, count: nil, offset: nil, sequence: false, condition: nil)
424
+ def self.register_field(field, type, length: nil, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
344
425
  if type.kind_of?(Symbol)
345
426
  if type[0] == 'a'
346
- c = Class::new(Str) do init(sym) end
347
- @fields.push([field, c, count, offset, sequence, condition])
427
+ real_type = Class::new(Str) do init(sym) end
348
428
  else
349
- @fields.push([field, const_get(SCALAR_TYPES[type][0]), count, offset, sequence, condition])
429
+ real_type = const_get(SCALAR_TYPES[type][0])
350
430
  end
351
431
  else
352
- @fields.push([field, type, count, offset, sequence, condition])
432
+ real_type = type
353
433
  end
434
+ @fields.push(Field::new(field, real_type, length, count, offset, sequence, condition, relative_offset))
354
435
  attr_accessor field
355
436
  end
356
437
 
@@ -361,8 +442,8 @@ module LibBin
361
442
  init(#{symbol.inspect})
362
443
  end
363
444
 
364
- def self.#{name}(field, count: nil, offset: nil, sequence: false, condition: nil)
365
- @fields.push([field, #{klassname}, count, offset, sequence, condition])
445
+ def self.#{name}(field, length: nil, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
446
+ @fields.push(Field::new(field, #{klassname}, length, count, offset, sequence, condition, relative_offset))
366
447
  attr_accessor field
367
448
  end
368
449
  EOF
@@ -401,16 +482,15 @@ EOF
401
482
  create_scalar_type(:pghalf_le)
402
483
  create_scalar_type(:pghalf_be)
403
484
 
404
- def self.string( field, length = nil, count: nil, offset: nil, sequence: false, condition: nil)
405
- sym = (length ? :"a#{length}" : :"a*")
485
+ def self.string( field, length = nil, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
486
+ sym = (length ? :"a" : :"a*")
406
487
  c = Class::new(Str) do
407
488
  init(sym)
408
489
  end
409
- @fields.push([field, c, count, offset, sequence, condition])
490
+ @fields.push(Field::new(field, c, length, count, offset, sequence, condition, relative_offset))
410
491
  attr_accessor field
411
492
  end
412
493
 
413
-
414
494
  end
415
495
 
416
496
  end
data/lib/libbin.rb CHANGED
@@ -117,10 +117,11 @@ module LibBin
117
117
  end
118
118
  end
119
119
 
120
- def __decode_seek_offset(offset)
120
+ def __decode_seek_offset(offset, relative_offset)
121
121
  return nil unless offset
122
122
  offset = __decode_expression(offset)
123
123
  return false if offset == 0x0
124
+ offset += @__position if relative_offset
124
125
  @__cur_position = offset
125
126
  @__input.seek(offset) if @__input
126
127
  @__output.seek(offset) if @__output
@@ -141,97 +142,106 @@ module LibBin
141
142
  return __decode_expression(type)
142
143
  end
143
144
 
144
- def __decode_static_conditions(type, count, offset, sequence, condition)
145
+ def __decode_length(length)
146
+ __decode_expression(length)
147
+ end
148
+
149
+ def __decode_static_conditions(field)
145
150
  @__offset = nil
146
151
  @__condition = nil
147
152
  @__type = nil
153
+ @__length = nil
148
154
  @__count = nil
149
- unless sequence
150
- @__offset = __decode_seek_offset(offset)
155
+ unless field.sequence?
156
+ @__offset = __decode_seek_offset(field.offset, field.relative_offset?)
151
157
  throw :ignored, nil if @__offset == false
152
- @__condition = __decode_condition(condition)
158
+ @__condition = __decode_condition(field.condition)
153
159
  throw :ignored, nil unless @__condition
154
- @__type = __decode_type(type)
160
+ @__type = __decode_type(field.type)
161
+ @__length = __decode_length(field.length)
155
162
  end
156
- @__count = __decode_count(count)
163
+ @__count = __decode_count(field.count)
157
164
  end
158
165
 
159
- def __decode_dynamic_conditions(type, offset, sequence, condition)
160
- return true unless sequence
166
+ def __decode_dynamic_conditions(field)
167
+ return true unless field.sequence?
161
168
  @__offset = nil
162
169
  @__condition = nil
163
170
  @__type = nil
164
- @__offset = __decode_seek_offset(offset)
171
+ @__length = nil
172
+ @__offset = __decode_seek_offset(field.offset, field.relative_offset?)
165
173
  return false if @__offset == false
166
- @__condition = __decode_condition(condition)
174
+ @__condition = __decode_condition(field.condition)
167
175
  return false unless @__condition
168
- @__type = __decode_type(type)
176
+ @__type = __decode_type(field.type)
177
+ @__length = __decode_length(field.length)
169
178
  return true
170
179
  end
171
180
 
172
181
  def __restore_context
173
182
  @__iterator = nil
174
183
  @__type = nil
184
+ @__length = nil
175
185
  @__count = nil
176
186
  @__offset = nil
177
187
  @__condition = nil
178
188
  end
179
189
 
180
- def __convert_field(field, type, count, offset, sequence, condition)
181
- __decode_static_conditions(type, count, offset, sequence, condition)
190
+ def __convert_field(field)
191
+ __decode_static_conditions(field)
182
192
  vs = @__count.times.collect do |it|
183
193
  @__iterator = it
184
- if __decode_dynamic_conditions(type, offset, sequence, condition)
185
- @__type::convert(@__input, @__output, @__input_big, @__output_big, self, it)
194
+ if __decode_dynamic_conditions(field)
195
+ @__type::convert(@__input, @__output, @__input_big, @__output_big, self, it, @__length)
186
196
  else
187
197
  nil
188
198
  end
189
199
  end
190
200
  __restore_context
191
- vs = vs.first unless count
201
+ vs = vs.first unless field.count
192
202
  vs
193
203
  end
194
204
 
195
- def __load_field(field, type, count, offset, sequence, condition)
196
- __decode_static_conditions(type, count, offset, sequence, condition)
205
+ def __load_field(field)
206
+ __decode_static_conditions(field)
197
207
  vs = @__count.times.collect do |it|
198
208
  @__iterator = it
199
- if __decode_dynamic_conditions(type, offset, sequence, condition)
200
- @__type::load(@__input, @__input_big, self, it)
209
+ if __decode_dynamic_conditions(field)
210
+ @__type::load(@__input, @__input_big, self, it, @__length)
201
211
  else
202
212
  nil
203
213
  end
204
214
  end
205
215
  __restore_context
206
- vs = vs.first unless count
216
+ vs = vs.first unless field.count
207
217
  vs
208
218
  end
209
219
 
210
- def __dump_field(vs, field, type, count, offset, sequence, condition)
211
- __decode_static_conditions(type, count, offset, sequence, condition)
212
- vs = [vs] unless count
220
+ def __dump_field(vs, field)
221
+ __decode_static_conditions(field)
222
+ vs = [vs] unless field.count
213
223
  vs.each_with_index do |v, it|
214
224
  @__iterator = it
215
- if __decode_dynamic_conditions(type, offset, sequence, condition)
216
- @__type::dump(v, @__output, @__output_big, self, it)
225
+ if __decode_dynamic_conditions(field)
226
+ @__type::dump(v, @__output, @__output_big, self, it, @__length)
217
227
  end
218
228
  end
219
229
  __restore_context
220
230
  end
221
231
 
222
- def __shape_field(vs, previous_offset, kind, field, type, count, offset, sequence, condition)
223
- __decode_static_conditions(type, count, offset, sequence, condition)
224
- vs = [vs] unless count
232
+ def __shape_field(vs, previous_offset, kind, field)
233
+ __decode_static_conditions(field)
234
+ vs = [vs] unless field.count
225
235
  vs = vs.each_with_index.collect do |v, it|
226
236
  @__iterator = it
227
- if __decode_dynamic_conditions(type, offset, sequence, condition)
228
- sh = @__type::shape(v, @__cur_position, self, it, kind)
237
+ if __decode_dynamic_conditions(field)
238
+ sh = @__type::shape(v, @__cur_position, self, it, kind, @__length)
229
239
  @__cur_position = sh.last + 1 if sh.last && sh.last >= 0
230
240
  sh
231
241
  end
232
242
  end
233
243
  __restore_context
234
- vs = vs.first unless count
244
+ vs = vs.first unless field.count
235
245
  vs
236
246
  end
237
247
 
@@ -242,15 +252,15 @@ module LibBin
242
252
  def __shape(previous_offset = 0, parent = nil, index = nil, kind = DataShape)
243
253
  __set_size_type(previous_offset, parent, index)
244
254
  members = {}
245
- self.class.instance_variable_get(:@fields).each { |name, type, *args|
255
+ self.class.instance_variable_get(:@fields).each { |field|
246
256
  begin
247
- vs = send(name)
257
+ vs = send(field.name)
248
258
  member = catch(:ignored) do
249
- __shape_field(vs, previous_offset, kind, name, type, *args)
259
+ __shape_field(vs, previous_offset, kind, field)
250
260
  end
251
- members[name] = member
261
+ members[field.name] = member
252
262
  rescue
253
- STDERR.puts "#{self.class}: #{name}(#{type})"
263
+ STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
254
264
  raise
255
265
  end
256
266
  }
@@ -260,14 +270,14 @@ module LibBin
260
270
  end
261
271
 
262
272
  def __convert_fields
263
- self.class.instance_variable_get(:@fields).each { |name, type, *args|
273
+ self.class.instance_variable_get(:@fields).each { |field|
264
274
  begin
265
275
  vs = catch(:ignored) do
266
- __convert_field(name, type, *args)
276
+ __convert_field(field)
267
277
  end
268
- send("#{name}=", vs)
278
+ send("#{field.name}=", vs)
269
279
  rescue
270
- STDERR.puts "#{self.class}: #{name}(#{type})"
280
+ STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
271
281
  raise
272
282
  end
273
283
  }
@@ -275,14 +285,14 @@ module LibBin
275
285
  end
276
286
 
277
287
  def __load_fields
278
- self.class.instance_variable_get(:@fields).each { |name, type, *args|
288
+ self.class.instance_variable_get(:@fields).each { |field|
279
289
  begin
280
290
  vs = catch(:ignored) do
281
- __load_field(name, type, *args)
291
+ __load_field(field)
282
292
  end
283
- send("#{name}=", vs)
293
+ send("#{field.name}=", vs)
284
294
  rescue
285
- STDERR.puts "#{self.class}: #{name}(#{type})"
295
+ STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
286
296
  raise
287
297
  end
288
298
  }
@@ -290,14 +300,14 @@ module LibBin
290
300
  end
291
301
 
292
302
  def __dump_fields
293
- self.class.instance_variable_get(:@fields).each { |name, type, *args|
303
+ self.class.instance_variable_get(:@fields).each { |field|
294
304
  begin
295
- vs = send(name)
305
+ vs = send(field.name)
296
306
  catch(:ignored) do
297
- __dump_field(vs, name, type, *args)
307
+ __dump_field(vs, field)
298
308
  end
299
309
  rescue
300
- STDERR.puts "#{self.class}: #{name}(#{type})"
310
+ STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
301
311
  raise
302
312
  end
303
313
  }
@@ -325,28 +335,57 @@ module LibBin
325
335
  self
326
336
  end
327
337
 
328
- def self.convert(input, output, input_big = LibBin::default_big?, output_big = !LibBin::default_big?, parent = nil, index = nil)
329
- h = self::new
330
- h.__convert(input, output, input_big, output_big, parent, index)
331
- h
338
+ def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil)
339
+ if length
340
+ length.times.collect {
341
+ h = self::new
342
+ h.__convert(input, output, input_big, output_big, parent, index)
343
+ }
344
+ else
345
+ h = self::new
346
+ h.__convert(input, output, input_big, output_big, parent, index)
347
+ end
332
348
  end
333
349
 
334
- def self.load(input, input_big = LibBin::default_big?, parent = nil, index = nil)
335
- h = self::new
336
- h.__load(input, input_big, parent, index)
337
- h
350
+ def self.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
351
+ if length
352
+ length.times.collect {
353
+ h = self::new
354
+ h.__load(input, input_big, parent, index)
355
+ }
356
+ else
357
+ h = self::new
358
+ h.__load(input, input_big, parent, index)
359
+ end
338
360
  end
339
361
 
340
- def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil)
341
- value.__dump(output, output_big, parent, index)
362
+ def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
363
+ if length
364
+ length.times.collect { |i|
365
+ value[i].__dump(output, output_big, parent, index)
366
+ }
367
+ value
368
+ else
369
+ value.__dump(output, output_big, parent, index)
370
+ end
342
371
  end
343
372
 
344
- def self.size(value, previous_offset = 0, parent = nil, index = nil)
345
- value.__shape(previous_offset, parent, index).size
373
+ def self.size(value, previous_offset = 0, parent = nil, index = nil, length = nil)
374
+ if length
375
+ shape(value, previous_offset, parent, index, length).size
376
+ else
377
+ value.__shape(previous_offset, parent, index).size
378
+ end
346
379
  end
347
380
 
348
- def self.shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape)
349
- value.__shape(previous_offset, parent, index, kind = DataShape)
381
+ def self.shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
382
+ if length
383
+ kind::new(length.times.collect { |i|
384
+ value[i].__shape(previous_offset, parent, index, kind)
385
+ })
386
+ else
387
+ value.__shape(previous_offset, parent, index, kind)
388
+ end
350
389
  end
351
390
 
352
391
  end
data/libbin.gemspec CHANGED
@@ -1,14 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'libbin'
3
- s.version = "1.0.3"
3
+ s.version = "1.0.7"
4
4
  s.author = "Brice Videau"
5
- s.email = "brice.videau@imag.fr"
5
+ s.email = "brice.videau@gmail.com"
6
6
  s.homepage = "https://github.com/kerilk/libbin"
7
7
  s.summary = "Library for loading and converting binary files"
8
8
  s.description = "Read, write and convert Binary data in Ruby."
9
9
  s.files = Dir[ 'libbin.gemspec', 'LICENSE', 'lib/**/*.rb', 'ext/libbin/extconf.rb', 'ext/libbin/*.c', 'ext/libbin/*.h' ]
10
10
  s.extensions << 'ext/libbin/extconf.rb'
11
- s.has_rdoc = false
12
11
  s.license = 'BSD-2-Clause'
13
12
  s.required_ruby_version = '>= 2.0.0'
14
13
  s.add_dependency 'float-formats', '~> 0.3', '>=0.3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libbin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice Videau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-04 00:00:00.000000000 Z
11
+ date: 2021-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: float-formats
@@ -31,7 +31,7 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.3.0
33
33
  description: Read, write and convert Binary data in Ruby.
34
- email: brice.videau@imag.fr
34
+ email: brice.videau@gmail.com
35
35
  executables: []
36
36
  extensions:
37
37
  - ext/libbin/extconf.rb
@@ -67,8 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.7.6
70
+ rubygems_version: 3.1.2
72
71
  signing_key:
73
72
  specification_version: 4
74
73
  summary: Library for loading and converting binary files