Method: LibBin::Structure::Str.convert

Defined in:
ext/libbin/data_types.c

.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil) ⇒ String

Convert a string field by loading it from input, dumping it to output, and returning the loaded field.

Parameters:

  • input (IO)

    the stream to load the field from.

  • output (IO)

    the stream to dump the field to.

  • input_big (Boolean) (defaults to: LibBin::default_big?)

    the endianness of input

  • output_big (Boolean) (defaults to: !input_big)

    the endianness of output.

  • parent (Structure) (defaults to: nil)

    ignored.

  • index (Integer) (defaults to: nil)

    ignored.

  • length (Integer) (defaults to: nil)

    if given the length of the string to reqd. Else the string is considered NULL terminated.

Returns:

  • (String)

    the Ruby representation of the string.



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'ext/libbin/data_types.c', line 438

static VALUE cStr_convert(int argc, VALUE* argv, VALUE self) {
  (void)self;
  VALUE input;
  VALUE output;
  VALUE length;
  rb_scan_args(argc, argv, "25", &input, &output, NULL, NULL, NULL, NULL, &length);
  VALUE str;
  if (NIL_P(length))
    str = rb_funcall(input, rb_intern("readline"), 1, rb_str_new_static("", 1));
  else {
    str = rb_funcall(input, id_read, 1, length);
    if (NIL_P(str) || RSTRING_LEN(str) < NUM2LONG(length))
      rb_raise(rb_eRuntimeError,
        "could not read enough data: got %ld needed %zu", RSTRING_LEN(str), NUM2LONG(length));
  }
  rb_funcall(output, id_write, 1, str);
  return str;
}