Class: CBOR::Unpacker

Inherits:
Object
  • Object
show all
Defined in:
ext/cbor/unpacker_class.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/cbor/unpacker_class.c', line 70

static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE io = Qnil;
    VALUE options = Qnil;

    if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
        /* Qnil */

    } else if(argc == 1) {
        VALUE v = argv[0];
        if(rb_type(v) == T_HASH) {
            options = v;
        } else {
            io = v;
        }

    } else if(argc == 2) {
        io = argv[0];
        options = argv[1];
        if(rb_type(options) != T_HASH) {
            rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
        }

    } else {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
    }

    UNPACKER(self, uk);
    if(io != Qnil || options != Qnil) {
        MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, options);
    }

    // TODO options

    return self;
}

Instance Method Details

#bufferObject



123
124
125
126
127
# File 'ext/cbor/unpacker_class.c', line 123

static VALUE Unpacker_buffer(VALUE self)
{
    UNPACKER(self, uk);
    return uk->buffer_ref;
}

#eachObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'ext/cbor/unpacker_class.c', line 265

static VALUE Unpacker_each(VALUE self)
{
    UNPACKER(self, uk);

#ifdef RETURN_ENUMERATOR
    RETURN_ENUMERATOR(self, 0, 0);
#endif

    if(msgpack_buffer_has_io(UNPACKER_BUFFER_(uk))) {
        /* rescue EOFError only if io is set */
        return rb_rescue2(Unpacker_each_impl, self,
                Unpacker_rescue_EOFError, self,
                rb_eEOFError, NULL);
    } else {
        return Unpacker_each_impl(self);
    }
}

#feed(data) ⇒ Object

rb_define_method(cMessagePack_Unpacker, “peek_next_type”, Unpacker_peek_next_type, 0); // TODO



225
226
227
228
229
230
231
232
233
234
# File 'ext/cbor/unpacker_class.c', line 225

static VALUE Unpacker_feed(VALUE self, VALUE data)
{
    UNPACKER(self, uk);

    StringValue(data);

    msgpack_buffer_append_string(UNPACKER_BUFFER_(uk), data);

    return self;
}

#feed_each(data) ⇒ Object



283
284
285
286
287
288
# File 'ext/cbor/unpacker_class.c', line 283

static VALUE Unpacker_feed_each(VALUE self, VALUE data)
{
    // TODO optimize
    Unpacker_feed(self, data);
    return Unpacker_each(self);
}

#readObject Also known as: unpack



129
130
131
132
133
134
135
136
137
138
139
# File 'ext/cbor/unpacker_class.c', line 129

static VALUE Unpacker_read(VALUE self)
{
    UNPACKER(self, uk);

    int r = msgpack_unpacker_read(uk, 0);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    return msgpack_unpacker_get_last_object(uk);
}

#read_array_headerObject



168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/cbor/unpacker_class.c', line 168

static VALUE Unpacker_read_array_header(VALUE self)
{
    UNPACKER(self, uk);

    uint64_t size;
    int r = msgpack_unpacker_read_array_header(uk, &size);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    return rb_ull2inum(size);
}

#read_map_headerObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/cbor/unpacker_class.c', line 181

static VALUE Unpacker_read_map_header(VALUE self)
{
    UNPACKER(self, uk);

    uint64_t size;
    int r = msgpack_unpacker_read_map_header(uk, &size);
    if(r < 0) {
        raise_unpacker_error((int)r);
    }

    return rb_ull2inum(size);
}

#resetObject



290
291
292
293
294
295
296
297
# File 'ext/cbor/unpacker_class.c', line 290

static VALUE Unpacker_reset(VALUE self)
{
    UNPACKER(self, uk);

    msgpack_unpacker_reset(uk);

    return Qnil;
}

#skipObject



141
142
143
144
145
146
147
148
149
150
151
# File 'ext/cbor/unpacker_class.c', line 141

static VALUE Unpacker_skip(VALUE self)
{
    UNPACKER(self, uk);

    int r = msgpack_unpacker_skip(uk, 0);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    return Qnil;
}

#skip_nilObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'ext/cbor/unpacker_class.c', line 153

static VALUE Unpacker_skip_nil(VALUE self)
{
    UNPACKER(self, uk);

    int r = msgpack_unpacker_skip_nil(uk);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    if(r) {
        return Qtrue;
    }
    return Qfalse;
}