class Bzip2::InternalStr

Internal

@private

Public Instance Methods

read(p1 = v1) click to toggle source
static VALUE bz_str_read(int argc, VALUE *argv, VALUE obj) {
    struct bz_str *bzs;
    VALUE res, len;
    int count;

    Data_Get_Struct(obj, struct bz_str, bzs);
    rb_scan_args(argc, argv, "01", &len);
    if (NIL_P(len)) {
        count = RSTRING_LEN(bzs->str);
    } else {
        count = NUM2INT(len);
        if (count < 0) {
            rb_raise(rb_eArgError, "negative length %d given", count);
        }
    }
    if (!count || bzs->pos == -1) {
        return Qnil;
    }
    if ((bzs->pos + count) >= RSTRING_LEN(bzs->str)) {
        res = rb_str_new(RSTRING_PTR(bzs->str) + bzs->pos,
            RSTRING_LEN(bzs->str) - bzs->pos);
        bzs->pos = -1;
    } else {
        res = rb_str_new(RSTRING_PTR(bzs->str) + bzs->pos, count);
        bzs->pos += count;
    }
    return res;
}