Module: Gonzui::AutoPack

Defined in:
lib/gonzui/dbm.rb,
ext/autopack/autopack.c

Defined Under Namespace

Classes: Adaptor

Constant Summary collapse

ID =
Adaptor.new(lambda {|id|  pack_id(id) },
lambda {|str| unpack_id(str) })
Fixnum =
Adaptor.new(lambda {|id|  pack_fixnum(id) },
lambda {|str| unpack_fixnum(str) })
Symbol =
Adaptor.new(lambda {|sym| sym.to_s},
lambda {|str| str.intern})
String =
Adaptor.new(nil, nil)
GZString =
Adaptor.new(lambda {|str| Zlib::Deflate.deflate(str) },
lambda {|str| Zlib::Inflate.inflate(str) })

Class Method Summary collapse

Class Method Details

.pack_fixnum(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'ext/autopack/autopack.c', line 19

static VALUE
rb_autopack_pack_fixnum(VALUE obj, VALUE value)
{
    int n;
    unsigned char str[4];
    if (TYPE(value) != T_FIXNUM) {
        rb_raise(rb_eTypeError, "Fixnum expected");
    }
    n = FIX2INT(value);
    str[0] = (n >> 24) & 0xff;
    str[1] = (n >> 16) & 0xff;
    str[2] = (n >>  8) & 0xff;
    str[3] =  n        & 0xff;
    return rb_str_new(str, 4);
}

.pack_id(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'ext/autopack/autopack.c', line 19

static VALUE
rb_autopack_pack_fixnum(VALUE obj, VALUE value)
{
    int n;
    unsigned char str[4];
    if (TYPE(value) != T_FIXNUM) {
        rb_raise(rb_eTypeError, "Fixnum expected");
    }
    n = FIX2INT(value);
    str[0] = (n >> 24) & 0xff;
    str[1] = (n >> 16) & 0xff;
    str[2] = (n >>  8) & 0xff;
    str[3] =  n        & 0xff;
    return rb_str_new(str, 4);
}

.pack_id2(id1, id2) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/autopack/autopack.c', line 35

static VALUE
rb_autopack_pack_id2(VALUE obj, VALUE id1, VALUE id2)
{
    int n1, n2;
    unsigned char str[8];
    if (!(TYPE(id1) == T_FIXNUM && TYPE(id2) == T_FIXNUM)) {
        rb_raise(rb_eTypeError, "Fixnum expected");
    }
    n1 = FIX2INT(id1);
    n2 = FIX2INT(id2);
    str[0] = (n1 >> 24) & 0xff;
    str[1] = (n1 >> 16) & 0xff;
    str[2] = (n1 >>  8) & 0xff;
    str[3] =  n1        & 0xff;
    str[4] = (n2 >> 24) & 0xff;
    str[5] = (n2 >> 16) & 0xff;
    str[6] = (n2 >>  8) & 0xff;
    str[7] =  n2        & 0xff;
    return rb_str_new(str, 8);
}

.unpack_fixnum(value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'ext/autopack/autopack.c', line 56

static VALUE
rb_autopack_unpack_fixnum(VALUE obj, VALUE value)
{
    int n;
    unsigned char *str;
    if (TYPE(value) != T_STRING) {
        rb_raise(rb_eTypeError, "String expected");
    }
    str = RSTRING_PTR(value);
    n = (str[0] << 24) + (str[1] << 16) + (str[2] << 8) + str[3];
    return INT2FIX(n);
}

.unpack_id(value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'ext/autopack/autopack.c', line 56

static VALUE
rb_autopack_unpack_fixnum(VALUE obj, VALUE value)
{
    int n;
    unsigned char *str;
    if (TYPE(value) != T_STRING) {
        rb_raise(rb_eTypeError, "String expected");
    }
    str = RSTRING_PTR(value);
    n = (str[0] << 24) + (str[1] << 16) + (str[2] << 8) + str[3];
    return INT2FIX(n);
}