Class: Wankel::StreamParser

Inherits:
Object
  • Object
show all
Defined in:
ext/wankel/wankel_stream_parser.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/wankel/wankel_stream_parser.c', line 32

VALUE stream_parser_initialize(int argc, VALUE * argv, VALUE self) {
    VALUE defaults = rb_const_get(c_wankel, intern_DEFAULTS);
    VALUE klass = rb_funcall(self, rb_intern("class"), 0);
    VALUE options, rbufsize;
    stream_parser * p;

    rb_scan_args(argc, argv, "01", &options);
    if (rb_const_defined(klass, intern_DEFAULTS)) { 
        defaults = rb_funcall(defaults, intern_merge, 1, rb_const_get(klass, intern_DEFAULTS));
    }
    if(options == Qnil) {
        rb_iv_set(self, "@options", rb_funcall(defaults, intern_clone, 0) );
    } else {
        Check_Type(options, T_HASH);
        rb_iv_set(self, "@options", rb_funcall(defaults, intern_merge, 1, options) );
    }
    options = rb_iv_get(self, "@options");

    Data_Get_Struct(self, stream_parser, p);
    p->callbacks = stream_parser_callbacks(self);
    p->alloc_funcs.malloc = yajl_helper_malloc;
    p->alloc_funcs.realloc = yajl_helper_realloc;
    p->alloc_funcs.free = yajl_helper_free;
    p->h = yajl_alloc(&p->callbacks, &p->alloc_funcs, (void *)self);
    
    yajl_configure(p->h, options);
        
    rbufsize = rb_hash_aref(options, sym_read_buffer_size);
    Check_Type(rbufsize, T_FIXNUM);
    p->rbufsize = rbufsize;
    
    if(rb_hash_aref(options, sym_symbolize_keys) == Qtrue) {
        p->symbolize_keys = 1;
    } else {
        p->symbolize_keys = 0;
    }
    
    return self;
}

Instance Method Details

#completeObject



120
121
122
123
124
125
126
127
128
129
130
# File 'ext/wankel/wankel_stream_parser.c', line 120

static VALUE stream_parser_complete(VALUE self) {
    yajl_status status;
    stream_parser * p;
    Data_Get_Struct(self, stream_parser, p);
    

    status = yajl_complete_parse(p->h);
    yajl_helper_check_status(p->h, status, 0, NULL, 0);

    return Qnil;
}

#parse(*args) ⇒ Object



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
# File 'ext/wankel/wankel_stream_parser.c', line 72

static VALUE stream_parser_parse(int argc, VALUE * argv, VALUE self) {
    const char * cptr;
    size_t len;
    yajl_status status;
    stream_parser * p;
    VALUE input;
    Data_Get_Struct(self, stream_parser, p);
	
    rb_scan_args(argc, argv, "10", &input);
    if (TYPE(input) == T_STRING) {
        cptr = RSTRING_PTR(input);
        len = RSTRING_LEN(input);
        status = yajl_parse(p->h, (const unsigned char*)cptr, len);
        yajl_helper_check_status(p->h, status, 1, (const unsigned char*)cptr, len);
    } else if (rb_respond_to(input, intern_io_read)) {
        VALUE chunk = rb_str_new(0, FIX2LONG(p->rbufsize));
        while (rb_funcall(input, intern_io_read, 2, p->rbufsize, chunk) != Qnil) {
            cptr = RSTRING_PTR(chunk);
            len = RSTRING_LEN(chunk);
            status = yajl_parse(p->h, (const unsigned char*)cptr, len);
            yajl_helper_check_status(p->h, status, 1, (const unsigned char*)cptr, len);
        }
    } else {
        rb_raise(e_parseError, "input must be a string or an IO");
    }
    
    status = yajl_complete_parse(p->h);
    yajl_helper_check_status(p->h, status, 0, NULL, 0);
    
    return Qnil;
}

#write(input) ⇒ Object Also known as: <<



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/wankel/wankel_stream_parser.c', line 104

static VALUE stream_parser_write(VALUE self, VALUE input) {
    const char * cptr;
    size_t len;
    yajl_status status;
    stream_parser * p;
    Data_Get_Struct(self, stream_parser, p);
    
    Check_Type(input, T_STRING);
    cptr = RSTRING_PTR(input);
    len = RSTRING_LEN(input);
    status = yajl_parse(p->h, (const unsigned char*)cptr, len);
    yajl_helper_check_status(p->h, status, 1, (const unsigned char*)cptr, len);
    
    return Qnil;
}