File: scp.c

package info (click to toggle)
ruby-oj 3.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ansic: 19,402; ruby: 11,451; makefile: 17
file content (188 lines) | stat: -rw-r--r-- 5,846 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2012 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "encode.h"
#include "intern.h"
#include "oj.h"
#include "parse.h"

static VALUE noop_start(ParseInfo pi) {
    return Qnil;
}

static void noop_end(ParseInfo pi) {
}

static void noop_add_value(ParseInfo pi, VALUE val) {
}

static void noop_add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
}

static void noop_add_num(ParseInfo pi, NumInfo ni) {
}

static VALUE noop_hash_key(ParseInfo pi, const char *key, size_t klen) {
    return Qundef;
}

static void noop_hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
}

static void noop_hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
}

static void noop_hash_set_value(ParseInfo pi, Val kval, VALUE value) {
}

static void noop_array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
}

static void noop_array_append_num(ParseInfo pi, NumInfo ni) {
}

static void noop_array_append_value(ParseInfo pi, VALUE value) {
}

static void add_value(ParseInfo pi, VALUE val) {
    rb_funcall(pi->handler, oj_add_value_id, 1, val);
}

static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
    volatile VALUE rstr = rb_str_new(str, len);

    rstr = oj_encode(rstr);
    rb_funcall(pi->handler, oj_add_value_id, 1, rstr);
}

static void add_num(ParseInfo pi, NumInfo ni) {
    rb_funcall(pi->handler, oj_add_value_id, 1, oj_num_as_value(ni));
}

static VALUE start_hash(ParseInfo pi) {
    return rb_funcall(pi->handler, oj_hash_start_id, 0);
}

static void end_hash(ParseInfo pi) {
    rb_funcall(pi->handler, oj_hash_end_id, 0);
}

static VALUE start_array(ParseInfo pi) {
    return rb_funcall(pi->handler, oj_array_start_id, 0);
}

static void end_array(ParseInfo pi) {
    rb_funcall(pi->handler, oj_array_end_id, 0);
}

static VALUE hash_key(ParseInfo pi, const char *key, size_t klen) {
    return rb_funcall(pi->handler, oj_hash_key_id, 1, rb_str_new(key, klen));
}

static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
    volatile VALUE rstr = rb_str_new(str, len);

    rstr = oj_encode(rstr);
    rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, kval), rstr);
}

static void hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
    rb_funcall(pi->handler,
               oj_hash_set_id,
               3,
               stack_peek(&pi->stack)->val,
               oj_calc_hash_key(pi, kval),
               oj_num_as_value(ni));
}

static void hash_set_value(ParseInfo pi, Val kval, VALUE value) {
    rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, kval), value);
}

static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
    volatile VALUE rstr = rb_str_new(str, len);

    rstr = oj_encode(rstr);
    rb_funcall(pi->handler, oj_array_append_id, 2, stack_peek(&pi->stack)->val, rstr);
}

static void array_append_num(ParseInfo pi, NumInfo ni) {
    rb_funcall(pi->handler, oj_array_append_id, 2, stack_peek(&pi->stack)->val, oj_num_as_value(ni));
}

static void array_append_value(ParseInfo pi, VALUE value) {
    rb_funcall(pi->handler, oj_array_append_id, 2, stack_peek(&pi->stack)->val, value);
}

VALUE
oj_sc_parse(int argc, VALUE *argv, VALUE self) {
    struct _parseInfo pi;
    VALUE             input = argv[1];

    parse_info_init(&pi);
    pi.err_class = Qnil;
    pi.max_depth = 0;
    pi.options   = oj_default_options;
    if (3 == argc) {
        oj_parse_options(argv[2], &pi.options);
    }
    if (rb_block_given_p()) {
        pi.proc = Qnil;
    } else {
        pi.proc = Qundef;
    }
    pi.handler = *argv;

    pi.start_hash  = rb_respond_to(pi.handler, oj_hash_start_id) ? start_hash : noop_start;
    pi.end_hash    = rb_respond_to(pi.handler, oj_hash_end_id) ? end_hash : noop_end;
    pi.hash_key    = rb_respond_to(pi.handler, oj_hash_key_id) ? hash_key : noop_hash_key;
    pi.start_array = rb_respond_to(pi.handler, oj_array_start_id) ? start_array : noop_start;
    pi.end_array   = rb_respond_to(pi.handler, oj_array_end_id) ? end_array : noop_end;
    if (rb_respond_to(pi.handler, oj_hash_set_id)) {
        pi.hash_set_value = hash_set_value;
        pi.hash_set_cstr  = hash_set_cstr;
        pi.hash_set_num   = hash_set_num;
        pi.expect_value   = 1;
    } else {
        pi.hash_set_value = noop_hash_set_value;
        pi.hash_set_cstr  = noop_hash_set_cstr;
        pi.hash_set_num   = noop_hash_set_num;
        pi.expect_value   = 0;
    }
    if (rb_respond_to(pi.handler, oj_array_append_id)) {
        pi.array_append_value = array_append_value;
        pi.array_append_cstr  = array_append_cstr;
        pi.array_append_num   = array_append_num;
        pi.expect_value       = 1;
    } else {
        pi.array_append_value = noop_array_append_value;
        pi.array_append_cstr  = noop_array_append_cstr;
        pi.array_append_num   = noop_array_append_num;
        pi.expect_value       = 0;
    }
    if (rb_respond_to(pi.handler, oj_add_value_id)) {
        pi.add_cstr     = add_cstr;
        pi.add_num      = add_num;
        pi.add_value    = add_value;
        pi.expect_value = 1;
    } else {
        pi.add_cstr     = noop_add_cstr;
        pi.add_num      = noop_add_num;
        pi.add_value    = noop_add_value;
        pi.expect_value = 0;
    }
    pi.has_callbacks = true;

    if (T_STRING == rb_type(input)) {
        return oj_pi_parse(argc - 1, argv + 1, &pi, 0, 0, 1);
    } else {
        return oj_pi_sparse(argc - 1, argv + 1, &pi, 0);
    }
}