File: code.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 (214 lines) | stat: -rw-r--r-- 6,431 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
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
// Copyright (c) 2017 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#include "code.h"

#include "dump.h"

inline static VALUE resolve_classname(VALUE mod, const char *classname) {
    VALUE clas = Qundef;
    ID    ci   = rb_intern(classname);

    if (rb_const_defined_at(mod, ci)) {
        clas = rb_const_get_at(mod, ci);
    }
    return clas;
}

static VALUE path2class(const char *name) {
    char        class_name[1024];
    VALUE       clas;
    char       *end = class_name + sizeof(class_name) - 1;
    char       *s;
    const char *n = name;

    clas = rb_cObject;
    for (s = class_name; '\0' != *n; n++) {
        if (':' == *n) {
            *s = '\0';
            n++;
            if (':' != *n) {
                return Qundef;
            }
            if (Qundef == (clas = resolve_classname(clas, class_name))) {
                return Qundef;
            }
            s = class_name;
        } else if (end <= s) {
            return Qundef;
        } else {
            *s++ = *n;
        }
    }
    *s = '\0';

    return resolve_classname(clas, class_name);
}

bool oj_code_dump(Code codes, VALUE obj, int depth, Out out) {
    VALUE clas = rb_obj_class(obj);
    Code  c    = codes;

    for (; NULL != c->name; c++) {
        if (Qundef == c->clas) {  // indicates not defined
            continue;
        }
        if (Qnil == c->clas) {
            c->clas = path2class(c->name);
        }
        if (clas == c->clas && c->active) {
            c->encode(obj, depth, out);
            return true;
        }
    }
    return false;
}

VALUE
oj_code_load(Code codes, VALUE clas, VALUE args) {
    Code c = codes;

    for (; NULL != c->name; c++) {
        if (Qundef == c->clas) {  // indicates not defined
            continue;
        }
        if (Qnil == c->clas) {
            c->clas = path2class(c->name);
        }
        if (clas == c->clas) {
            if (NULL == c->decode) {
                break;
            }
            return c->decode(clas, args);
        }
    }
    return Qnil;
}

void oj_code_set_active(Code codes, VALUE clas, bool active) {
    Code c = codes;

    for (; NULL != c->name; c++) {
        if (Qundef == c->clas) {  // indicates not defined
            continue;
        }
        if (Qnil == c->clas) {
            c->clas = path2class(c->name);
        }
        if (clas == c->clas || Qnil == clas) {
            c->active = active;
            if (Qnil != clas) {
                break;
            }
        }
    }
}

bool oj_code_has(Code codes, VALUE clas, bool encode) {
    Code c = codes;

    for (; NULL != c->name; c++) {
        if (Qundef == c->clas) {  // indicates not defined
            continue;
        }
        if (Qnil == c->clas) {
            c->clas = path2class(c->name);
        }
        if (clas == c->clas) {
            if (encode) {
                return c->active && NULL != c->encode;
            } else {
                return c->active && NULL != c->decode;
            }
        }
    }
    return false;
}

void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class) {
    int         d2        = depth + 1;
    int         d3        = d2 + 1;
    size_t      sep_len   = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
    const char *classname = rb_obj_classname(obj);
    size_t      len       = strlen(classname);
    size_t      size      = d2 * out->indent + 10 + len + out->opts->create_id_len + sep_len;
    bool        no_comma  = true;

    assure_size(out, size);
    *out->cur++ = '{';

    if (with_class) {
        fill_indent(out, d2);
        *out->cur++ = '"';
        APPEND_CHARS(out->cur, out->opts->create_id, out->opts->create_id_len);
        *out->cur++ = '"';
        if (0 < out->opts->dump_opts.before_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
        }
        *out->cur++ = ':';
        if (0 < out->opts->dump_opts.after_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
        }
        *out->cur++ = '"';
        APPEND_CHARS(out->cur, classname, len);
        *out->cur++ = '"';
        no_comma    = false;
    }
    size = d3 * out->indent + 2;
    for (; NULL != attrs->name; attrs++) {
        assure_size(out, size + attrs->len + sep_len + 2);
        if (no_comma) {
            no_comma = false;
        } else {
            *out->cur++ = ',';
        }
        fill_indent(out, d2);
        *out->cur++ = '"';
        APPEND_CHARS(out->cur, attrs->name, attrs->len);
        *out->cur++ = '"';
        if (0 < out->opts->dump_opts.before_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
        }
        *out->cur++ = ':';
        if (0 < out->opts->dump_opts.after_size) {
            APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
        }
        if (Qundef == attrs->value) {
            if (Qundef != attrs->time) {
                switch (out->opts->time_format) {
                case RubyTime: oj_dump_ruby_time(attrs->time, out); break;
                case XmlTime: oj_dump_xml_time(attrs->time, out); break;
                case UnixZTime: oj_dump_time(attrs->time, out, true); break;
                case UnixTime:
                default: oj_dump_time(attrs->time, out, false); break;
                }
            } else {
                char   buf[32];
                char  *b   = buf + sizeof(buf) - 1;
                bool   neg = false;
                long   num = attrs->num;
                size_t cnt = 0;

                if (0 > num) {
                    neg = true;
                    num = -num;
                }
                *b-- = '\0';
                if (0 < num) {
                    b = oj_longlong_to_string(num, neg, b);
                } else {
                    *b = '0';
                }
                cnt = sizeof(buf) - (b - buf) - 1;
                assure_size(out, cnt);
                APPEND_CHARS(out->cur, b, cnt);
            }
        } else {
            oj_dump_compat_val(attrs->value, d3, out, true);
        }
    }
    assure_size(out, depth * out->indent + 2);
    fill_indent(out, depth);
    *out->cur++ = '}';
    *out->cur   = '\0';
}