File: hash_load.c

package info (click to toggle)
ruby-ox 2.14.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,504 kB
  • sloc: xml: 39,683; ansic: 9,626; ruby: 6,441; sh: 47; makefile: 2
file content (309 lines) | stat: -rw-r--r-- 8,314 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* hash_load.c
 * Copyright (c) 2011, Peter Ohler
 * All rights reserved.
 */

#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ox.h"
#include "ruby.h"

#define MARK_INC 256

// The approach taken for the hash and has_no_attrs parsing is to push just
// the key on to the stack and then decide what to do on the way up/out.

static VALUE create_top(PInfo pi) {
    volatile VALUE top = rb_hash_new();

    helper_stack_push(&pi->helpers, 0, top, HashCode);
    pi->obj = top;

    return top;
}

static void mark_value(PInfo pi, VALUE val) {
    if (NULL == pi->marked) {
        pi->marked    = ALLOC_N(VALUE, MARK_INC);
        pi->mark_size = MARK_INC;
    } else if (pi->mark_size <= pi->mark_cnt) {
        pi->mark_size += MARK_INC;
        REALLOC_N(pi->marked, VALUE, pi->mark_size);
    }
    pi->marked[pi->mark_cnt] = val;
    pi->mark_cnt++;
}

static bool marked(PInfo pi, VALUE val) {
    if (NULL != pi->marked) {
        VALUE *vp = pi->marked + pi->mark_cnt - 1;

        for (; pi->marked <= vp; vp--) {
            if (val == *vp) {
                return true;
            }
        }
    }
    return false;
}

static void unmark(PInfo pi, VALUE val) {
    if (NULL != pi->marked) {
        VALUE *vp = pi->marked + pi->mark_cnt - 1;
        int    i;

        for (i = 0; pi->marked <= vp; vp--, i++) {
            if (val == *vp) {
                for (; 0 < i; i--, vp++) {
                    *vp = *(vp + 1);
                }
                pi->mark_cnt--;
                break;
            }
        }
    }
}

static void add_str(PInfo pi, VALUE s) {
    Helper         parent = helper_stack_peek(&pi->helpers);
    volatile VALUE a;

    if (0 != pi->options->rb_enc) {
        rb_enc_associate(s, pi->options->rb_enc);
    }
    switch (parent->type) {
    case NoCode:
        parent->obj  = s;
        parent->type = StringCode;
        break;
    case ArrayCode: rb_ary_push(parent->obj, s); break;
    default:
        a = rb_ary_new();
        rb_ary_push(a, parent->obj);
        rb_ary_push(a, s);
        parent->obj  = a;
        parent->type = ArrayCode;
        break;
    }
}

static void add_text(PInfo pi, char *text, int closed) {
    VALUE s = rb_str_new2(text);
    if (0 != pi->options->rb_enc) {
        rb_enc_associate(s, pi->options->rb_enc);
    }
    add_str(pi, s);
}

static void add_cdata(PInfo pi, const char *text, size_t len) {
    VALUE s = rb_str_new2(text);
    if (0 != pi->options->rb_enc) {
        rb_enc_associate(s, pi->options->rb_enc);
    }
    add_str(pi, s);
}

static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
    VALUE s = rb_str_new2(ename);
    if (0 != pi->options->rb_enc) {
        rb_enc_associate(s, pi->options->rb_enc);
    }
    if (helper_stack_empty(&pi->helpers)) {
        create_top(pi);
    }
    if (NULL != attrs && NULL != attrs->name) {
        volatile VALUE h = rb_hash_new();
        volatile VALUE key;
        volatile VALUE val;
        volatile VALUE a;

        for (; 0 != attrs->name; attrs++) {
            key = rb_str_new2(attrs->name);
            if (0 != pi->options->rb_enc) {
                rb_enc_associate(key, pi->options->rb_enc);
            }
            if (Qnil != pi->options->attr_key_mod) {
                key = rb_funcall(pi->options->attr_key_mod, ox_call_id, 1, key);
            } else if (Yes == pi->options->sym_keys) {
                key = rb_id2sym(rb_intern_str(key));
            }
            val = rb_str_new2(attrs->value);
            if (0 != pi->options->rb_enc) {
                rb_enc_associate(val, pi->options->rb_enc);
            }
            rb_hash_aset(h, key, val);
        }
        a = rb_ary_new();
        rb_ary_push(a, h);
        mark_value(pi, a);
        helper_stack_push(&pi->helpers, rb_intern_str(s), a, ArrayCode);
    } else {
        helper_stack_push(&pi->helpers, rb_intern_str(s), Qnil, NoCode);
    }
}

static void add_element_no_attrs(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
    VALUE s = rb_str_new2(ename);
    if (0 != pi->options->rb_enc) {
        rb_enc_associate(s, pi->options->rb_enc);
    }
    if (helper_stack_empty(&pi->helpers)) {
        create_top(pi);
    }
    helper_stack_push(&pi->helpers, rb_intern_str(s), Qnil, NoCode);
}

static int umark_hash_cb(VALUE key, VALUE value, VALUE x) {
    unmark((PInfo)x, value);

    return ST_CONTINUE;
}

static void end_element_core(PInfo pi, const char *ename, bool check_marked) {
    Helper         e      = helper_stack_pop(&pi->helpers);
    Helper         parent = helper_stack_peek(&pi->helpers);
    volatile VALUE pobj   = parent->obj;
    volatile VALUE found  = Qundef;
    volatile VALUE key;
    volatile VALUE a;

    if (NoCode == e->type) {
        e->obj = Qnil;
    }
    if (Qnil != pi->options->element_key_mod) {
        key = rb_funcall(pi->options->element_key_mod, ox_call_id, 1, rb_id2str(e->var));
    } else if (Yes == pi->options->sym_keys) {
        key = rb_id2sym(e->var);
    } else {
        key = rb_id2str(e->var);
    }
    // Make sure the parent is a Hash. If not set then make a Hash. If an
    // Array or non-Hash then append to array or create and append.
    switch (parent->type) {
    case NoCode:
        pobj         = rb_hash_new();
        parent->obj  = pobj;
        parent->type = HashCode;
        break;
    case ArrayCode:
        pobj = rb_hash_new();
        rb_ary_push(parent->obj, pobj);
        break;
    case HashCode: found = rb_hash_lookup2(parent->obj, key, Qundef); break;
    default:
        a = rb_ary_new();
        rb_ary_push(a, parent->obj);
        pobj = rb_hash_new();
        rb_ary_push(a, pobj);
        parent->obj  = a;
        parent->type = ArrayCode;
        break;
    }
    if (Qundef == found) {
        rb_hash_aset(pobj, key, e->obj);
    } else if (RUBY_T_ARRAY == rb_type(found)) {
        if (check_marked && marked(pi, found)) {
            unmark(pi, found);
            a = rb_ary_new();
            rb_ary_push(a, found);
            rb_ary_push(a, e->obj);
            rb_hash_aset(pobj, key, a);
        } else {
            rb_ary_push(found, e->obj);
        }
    } else {  // something there other than an array
        if (check_marked && marked(pi, e->obj)) {
            unmark(pi, e->obj);
        }
        a = rb_ary_new();
        rb_ary_push(a, found);
        rb_ary_push(a, e->obj);
        rb_hash_aset(pobj, key, a);
    }
    if (check_marked && NULL != pi->marked && RUBY_T_HASH == rb_type(e->obj)) {
        rb_hash_foreach(e->obj, umark_hash_cb, (VALUE)pi);
    }
}

static void end_element(PInfo pi, const char *ename) {
    end_element_core(pi, ename, true);
}

static void end_element_no_attrs(PInfo pi, const char *ename) {
    end_element_core(pi, ename, false);
}

static void finish(PInfo pi) {
    xfree(pi->marked);
}

static void set_encoding_from_instruct(PInfo pi, Attr attrs) {
    for (; 0 != attrs->name; attrs++) {
        if (0 == strcmp("encoding", attrs->name)) {
            pi->options->rb_enc = rb_enc_find(attrs->value);
        }
    }
}

static void instruct(PInfo pi, const char *target, Attr attrs, const char *content) {
    if (0 == strcmp("xml", target)) {
        set_encoding_from_instruct(pi, attrs);
    }
}

struct _parseCallbacks _ox_hash_callbacks = {
    instruct,
    NULL,
    NULL,
    NULL,
    add_text,
    add_element,
    end_element,
    finish,
};

ParseCallbacks ox_hash_callbacks = &_ox_hash_callbacks;

struct _parseCallbacks _ox_hash_cdata_callbacks = {
    instruct,
    NULL,
    NULL,
    add_cdata,
    add_text,
    add_element,
    end_element,
    finish,
};

ParseCallbacks ox_hash_cdata_callbacks = &_ox_hash_cdata_callbacks;

struct _parseCallbacks _ox_hash_no_attrs_callbacks = {
    instruct,
    NULL,
    NULL,
    NULL,
    add_text,
    add_element_no_attrs,
    end_element_no_attrs,
    NULL,
};

ParseCallbacks ox_hash_no_attrs_callbacks = &_ox_hash_no_attrs_callbacks;

struct _parseCallbacks _ox_hash_no_attrs_cdata_callbacks = {
    instruct,
    NULL,
    NULL,
    add_cdata,
    add_text,
    add_element_no_attrs,
    end_element_no_attrs,
    NULL,
};

ParseCallbacks ox_hash_no_attrs_cdata_callbacks = &_ox_hash_no_attrs_cdata_callbacks;