Class: LibBin::Structure::Str
- Inherits:
-
Scalar
- Object
- Scalar
- LibBin::Structure::Str
- Defined in:
- lib/libbin/data_types.rb,
ext/libbin/data_types.c
Overview
A string type, can be NULL terminated or have an arbitrary length.
Class Method Summary collapse
-
.align ⇒ Integer
Returns the alignement of the underlying character type.
-
.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 tooutput
, and returning the loaded field. -
.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil) ⇒ nil
Dump a string field to
output
. -
.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil) ⇒ String
Load a string field from
input
, and return it. -
.shape(value, offset = 0, parent = nil, index = nil, kind = DataShape, length = nil) ⇒ kind
Returns the shape of a string field.
-
.size(value, offset = 0, parent = nil, index = nil, length = nil) ⇒ Integer
Returns the size of a string.
Class Method Details
.align ⇒ Integer
Returns the alignement of the underlying character type.
361 362 363 364 |
# File 'ext/libbin/data_types.c', line 361
static VALUE cStr_align(VALUE self) {
(void)self;
return INT2FIX(sizeof(char));
}
|
.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil) ⇒ 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;
}
|
.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil) ⇒ nil
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'ext/libbin/data_types.c', line 413
static VALUE cStr_dump(int argc, VALUE* argv, VALUE self) {
(void)self;
VALUE value;
VALUE output;
VALUE length;
rb_scan_args(argc, argv, "24", &value, &output, NULL, NULL, NULL, &length);
if (NIL_P(length))
rb_funcall(output, id_write, 1, value);
else {
long l = NUM2LONG(length);
long vl = RSTRING_LEN(value);
VALUE str;
if (vl > l)
str = rb_str_new(RSTRING_PTR(value), l);
else {
str = rb_str_buf_new(l);
rb_str_cat(str, RSTRING_PTR(value), vl);
for (long i = 0; i < l - vl; i++)
rb_str_cat(str, "", 1);
}
rb_funcall(output, id_write, 1, str);
}
return Qnil;
}
|
.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil) ⇒ String
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'ext/libbin/data_types.c', line 397
static VALUE cStr_load(int argc, VALUE* argv, VALUE self) {
(void)self;
VALUE input;
VALUE length;
rb_scan_args(argc, argv, "14", &input, NULL, NULL, NULL, &length);
if (NIL_P(length))
return rb_funcall(input, rb_intern("readline"), 1, rb_str_new_static("", 1));
else {
VALUE 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));
return str;
}
}
|
.shape(value, offset = 0, parent = nil, index = nil, kind = DataShape, length = nil) ⇒ kind
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'ext/libbin/data_types.c', line 377
static VALUE cStr_shape(int argc, VALUE* argv, VALUE self) {
(void)self;
VALUE value;
VALUE previous_offset;
VALUE kind;
VALUE length;
rb_scan_args(argc, argv, "15", &value, &previous_offset, NULL, NULL, &kind, &length);
if (NIL_P(previous_offset))
previous_offset = INT2FIX(0);
if (NIL_P(kind))
kind = cDataShape;
if (NIL_P(length)) {
VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) - 1 + RSTRING_LEN(value)) };
return rb_class_new_instance(2, args, kind);
} else {
VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) - 1 + NUM2LL(length)) };
return rb_class_new_instance(2, args, kind);
}
}
|
.size(value, offset = 0, parent = nil, index = nil, length = nil) ⇒ Integer
366 367 368 369 370 371 372 373 374 375 |
# File 'ext/libbin/data_types.c', line 366
static VALUE cStr_size(int argc, VALUE* argv, VALUE self) {
(void)self;
VALUE value;
VALUE length;
rb_scan_args(argc, argv, "14", &value, NULL, NULL, NULL, &length);
if (RTEST(length))
return length;
else
return rb_funcall(value, rb_intern("bytesize"), 0);
}
|