libbin 1.0.8 → 2.0.0

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: ac7b79d2e9bfeed5936680b7b192091988b66bebf8b7a9606468212f176929ae
4
- data.tar.gz: 273e0156acc7f86fb799f7b5aafc99c3fb428ac69bcbe5fbda2a265d9543fb04
3
+ metadata.gz: 8a0a92cec0fde131c6254ad8b5f8a405e68c2a9bd7699a11e7cd24d7e5d0c110
4
+ data.tar.gz: 17732019c6941e79fcfaf233aba722e08b6470e851adae24a01b9e36c90608ca
5
5
  SHA512:
6
- metadata.gz: 1268f8a9247714f2f2dac82b929f559eba7a8561b75f6fa89474e8a3d12b88723d37a6d065b679c41861dd3454a7a19b6d4e41f415a76f8fb77fa5ae3fed1a7d
7
- data.tar.gz: e9dd37966771af9750ec9a302048cd14e156d7fd1f4c82c5336f1db50a96e00b9a1b5d04627a21abec24f725556c8fd1dbb9808be567bf09ba429c7b66d5e13a
6
+ metadata.gz: 2caa512271ce77e4f23ad01d5eb9a1c4a9049eefd8578dcc8b69730183b25311a60a0a1ae97674b9f87778f9e9dad09b5fdcb2acb7a4a3d50110b388f03e106a
7
+ data.tar.gz: 600b1878941b3fa53cc9191bc7de026b9f205ce36520915925a6d4664ddba00d92bcdd4c035424d35eb4b97d2eaf732c2339aad785c603ada01790026f1f0f63
@@ -2,30 +2,42 @@
2
2
  #include "./libbin_c.h"
3
3
 
4
4
  VALUE cDataShape;
5
+ VALUE cDataRange;
5
6
  VALUE cScalar;
6
7
 
7
8
  static ID id_read, id_write;
8
9
 
9
- /* size(value, previous_offset = 0, parent = nil, index = nil, length = nil)*/
10
+ static VALUE cScalar_always_align(VALUE self) {
11
+ return Qfalse;
12
+ }
13
+
14
+ /* align() */
15
+ #define MAKE_TYPE_ALIGN(CLASS, MAPPED_TYPE) \
16
+ static VALUE CLASS ## _align(VALUE self) { \
17
+ (void)self; \
18
+ return INT2FIX(sizeof(MAPPED_TYPE)); \
19
+ }
20
+
21
+ /* size(value = nil, previous_offset = 0, parent = nil, index = nil, length = nil) */
10
22
  #define MAKE_TYPE_SIZE(CLASS, MAPPED_TYPE) \
11
23
  static VALUE CLASS ## _size(int argc, VALUE* argv, VALUE self) { \
12
24
  (void)self; \
13
25
  VALUE length; \
14
- rb_scan_args(argc, argv, "14", NULL, NULL, NULL, NULL, &length); \
26
+ rb_scan_args(argc, argv, "05", NULL, NULL, NULL, NULL, &length); \
15
27
  if (RTEST(length)) \
16
28
  return ULL2NUM(sizeof(MAPPED_TYPE)*NUM2ULL(length)); \
17
29
  else \
18
30
  return INT2FIX(sizeof(MAPPED_TYPE)); \
19
31
  }
20
32
 
21
- /* shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil) */
33
+ /* shape(value = nil, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil) */
22
34
  #define MAKE_TYPE_SHAPE(CLASS, MAPPED_TYPE) \
23
35
  static VALUE CLASS ## _shape(int argc, VALUE* argv, VALUE self) { \
24
36
  (void)self; \
25
37
  VALUE previous_offset; \
26
38
  VALUE kind; \
27
39
  VALUE length; \
28
- rb_scan_args(argc, argv, "15", NULL, &previous_offset, NULL, NULL, &kind, &length); \
40
+ rb_scan_args(argc, argv, "06", NULL, &previous_offset, NULL, NULL, &kind, &length); \
29
41
  if (NIL_P(previous_offset)) \
30
42
  previous_offset = INT2FIX(0); \
31
43
  if (NIL_P(kind)) \
@@ -179,28 +191,30 @@ static VALUE CLASS ## _convert(int argc, VALUE* argv, VALUE self) {
179
191
  VALUE str = rb_funcall(input, id_read, 1, ULL2NUM(cnt)); \
180
192
  if (NIL_P(str) || RSTRING_LEN(str) < (long)cnt) \
181
193
  rb_raise(rb_eRuntimeError, "could not read enough data: got %ld needed %zu", \
182
- NIL_P(str) ? 0 : RSTRING_LEN(str), cnt); \
194
+ NIL_P(str) ? 0 : RSTRING_LEN(str), cnt); \
183
195
  MAPPED_TYPE *data = (MAPPED_TYPE *)RSTRING_PTR(str); \
184
196
  CONVERT(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT); \
185
197
  rb_funcall(output, id_write, 1, str); \
186
198
  return res; \
187
199
  }
188
200
 
189
- #define MAKE_CLASS_DEFINE(CLASS_NAME, CLASS) \
190
- static void define_ ## CLASS() { \
191
- CLASS = rb_define_class_under(cDataConverter, #CLASS_NAME, cScalar); \
192
- rb_define_singleton_method(CLASS, "size", CLASS ## _size, -1); \
193
- rb_define_singleton_method(CLASS, "shape", CLASS ## _shape, -1); \
194
- rb_define_singleton_method(CLASS, "load", CLASS ## _load, -1); \
195
- rb_define_singleton_method(CLASS, "dump", CLASS ## _dump, -1); \
196
- rb_define_singleton_method(CLASS, "convert", CLASS ## _convert, -1); \
201
+ #define MAKE_CLASS_DEFINE(CLASS_NAME, CLASS) \
202
+ static void define_ ## CLASS() { \
203
+ CLASS = rb_define_class_under(cStructure, #CLASS_NAME, cScalar); \
204
+ rb_define_singleton_method(CLASS, "align", CLASS ## _align, 0); \
205
+ rb_define_singleton_method(CLASS, "size", CLASS ## _size, -1); \
206
+ rb_define_singleton_method(CLASS, "shape", CLASS ## _shape, -1); \
207
+ rb_define_singleton_method(CLASS, "load", CLASS ## _load, -1); \
208
+ rb_define_singleton_method(CLASS, "dump", CLASS ## _dump, -1); \
209
+ rb_define_singleton_method(CLASS, "convert", CLASS ## _convert, -1); \
197
210
  }
198
211
 
199
212
  #define MAKE_STATIC_OBJECT(CLASS) \
200
213
  static VALUE CLASS;
201
214
 
202
215
  #define MAKE_CLASS_TYPE_ENDIAN_EX(CLASS_NAME, CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN) \
203
- MAKE_STATIC_OBJECT(CLASS) \
216
+ MAKE_STATIC_OBJECT(CLASS) \
217
+ MAKE_TYPE_ALIGN(CLASS, MAPPED_TYPE) \
204
218
  MAKE_TYPE_SIZE(CLASS, MAPPED_TYPE) \
205
219
  MAKE_TYPE_SHAPE(CLASS, MAPPED_TYPE) \
206
220
  MAKE_TYPE_LOAD(CLASS, MAPPED_TYPE, RUBY_CONVERT_TO, NATIVE_CONVERT_TO, MAKE_LOAD ## ENDIAN) \
@@ -344,6 +358,11 @@ MAKE_CLASSES(Double, 64, DBL2NUM, NUM2DBL, unpack_double, pack_double)
344
358
 
345
359
  static VALUE cStr;
346
360
 
361
+ static VALUE cStr_align(VALUE self) {
362
+ (void)self;
363
+ return INT2FIX(sizeof(char));
364
+ }
365
+
347
366
  static VALUE cStr_size(int argc, VALUE* argv, VALUE self) {
348
367
  (void)self;
349
368
  VALUE value;
@@ -436,18 +455,90 @@ static VALUE cStr_convert(int argc, VALUE* argv, VALUE self) {
436
455
  }
437
456
 
438
457
  static void define_cStr() {
439
- cStr = rb_define_class_under(cDataConverter, "Str", cScalar);
458
+ cStr = rb_define_class_under(cStructure, "Str", cScalar);
459
+ /**
460
+ * Returns the alignement of the underlying character type.
461
+ * @return [Integer]
462
+ */
463
+ rb_define_singleton_method(cStr, "align", cStr_align, 0);
464
+ /**
465
+ * @overload size(value, offset = 0, parent = nil, index = nil, length = nil)
466
+ * Returns the size of a string.
467
+ * @param value [Object] string to dump.
468
+ * @param offset [Integer] ignored.
469
+ * @param parent [Structure] ignored.
470
+ * @param index [Integer] ignored.
471
+ * @param length [Integer] if given the length of the vector. Else
472
+ * the size of the string.
473
+ * @return [Integer] the size of the string or <tt>sizeof(char) * length</tt>.
474
+ */
440
475
  rb_define_singleton_method(cStr, "size", cStr_size, -1);
476
+ /**
477
+ * @overload shape(value, offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
478
+ * Returns the shape of a string field
479
+ * @param value [Object] ignored.
480
+ * @param offset [Integer] start of the shape.
481
+ * @param parent [Structure] ignored.
482
+ * @param index [Integer] ignored.
483
+ * @param kind [Class] shape class. Will be instantiated through
484
+ * new with the +offset+ and <tt>offset + sizeof($3) * length - 1</tt>.
485
+ * @param length [Integer] if given the length of the string to
486
+ * consider. Else the length is the size of the string.
487
+ * @return [kind] a new instance of +kind+
488
+ */
441
489
  rb_define_singleton_method(cStr, "shape", cStr_shape, -1);
490
+ /**
491
+ * @overload load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
492
+ * Load a string field from +input+, and return it.
493
+ * @param input [IO] the stream to load the field from.
494
+ * @param input_big [Boolean] the endianness of +input+
495
+ * @param parent [Structure] ignored.
496
+ * @param index [Integer] ignored.
497
+ * @param length [Integer] if given the length of the string. Else
498
+ * the string is considered NULL terminated.
499
+ * @return [String] the Ruby representation of the string.
500
+ */
442
501
  rb_define_singleton_method(cStr, "load", cStr_load, -1);
502
+ /**
503
+ * @overload dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
504
+ * Dump a string field to +output+.
505
+ * @param value [Numeric, Array<Numeric>] the Ruby representation
506
+ * of the string.
507
+ * @param output [IO] the stream to dump the field to.
508
+ * @param output_big [Boolean] the endianness of +output+.
509
+ * @param parent [Structure] ignored.
510
+ * @param index [Integer] ignored.
511
+ * @param length [Integer] if given the length of the string to dump. Else
512
+ * the length is the size of the string.
513
+ * @return [nil]
514
+ */
443
515
  rb_define_singleton_method(cStr, "dump", cStr_dump, -1);
516
+ /**
517
+ * @overload convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil)
518
+ * Convert a string field by loading it from +input+,
519
+ * dumping it to +output+, and returning the loaded field.
520
+ * @param input [IO] the stream to load the field from.
521
+ * @param output [IO] the stream to dump the field to.
522
+ * @param input_big [Boolean] the endianness of +input+
523
+ * @param output_big [Boolean] the endianness of +output+.
524
+ * @param parent [Structure] ignored.
525
+ * @param index [Integer] ignored.
526
+ * @param length [Integer] if given the length of the string to reqd. Else
527
+ * the string is considered NULL terminated.
528
+ * @return [String] the Ruby representation of the string.
529
+ */
444
530
  rb_define_singleton_method(cStr, "convert", cStr_convert, -1);
445
531
  }
446
532
 
447
533
  void define_cScalar() {
448
534
  id_read = rb_intern("read");
449
535
  id_write = rb_intern("write");
450
- cScalar = rb_define_class_under(cDataConverter, "Scalar", rb_cObject);
536
+ cScalar = rb_define_class_under(cStructure, "Scalar", rb_cObject);
537
+ /**
538
+ * Returns false as scalars are not required to be aligned.
539
+ * @return [false] return false.
540
+ */
541
+ rb_define_singleton_method(cScalar, "always_align", cScalar_always_align, 0);
451
542
  MAKE_CALL_DEFINES(Half);
452
543
  MAKE_CALL_DEFINES(PGHalf);
453
544
  MAKE_CALL_DEFINES(Int8);
@@ -8,7 +8,8 @@ union float_u {
8
8
 
9
9
  extern VALUE cScalar;
10
10
  extern VALUE cDataShape;
11
- extern VALUE cDataConverter;
11
+ extern VALUE cDataRange;
12
+ extern VALUE cStructure;
12
13
 
13
14
  void define_cScalar();
14
15