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

#include "odd.h"

#include <string.h>

#include "mem.h"

static Odd odds = NULL;
static ID  sec_id;
static ID  sec_fraction_id;
static ID  to_f_id;
static ID  numerator_id;
static ID  denominator_id;
static ID  rational_id;

static void set_class(Odd odd, const char *classname) {
    const char **np;
    ID          *idp;

    odd->classname = classname;
    odd->clen      = strlen(classname);
    odd->clas      = rb_const_get(rb_cObject, rb_intern(classname));
    rb_gc_register_mark_object(odd->clas);
    odd->create_obj = odd->clas;
    rb_gc_register_mark_object(odd->create_obj);
    odd->create_op = rb_intern("new");
    odd->is_module = (T_MODULE == rb_type(odd->clas));
    odd->raw       = 0;
    for (np = odd->attr_names, idp = odd->attrs; 0 != *np; np++, idp++) {
        *idp = rb_intern(*np);
    }
    *idp = 0;
}

static VALUE get_datetime_secs(VALUE obj) {
    volatile VALUE rsecs = rb_funcall(obj, sec_id, 0);
    volatile VALUE rfrac = rb_funcall(obj, sec_fraction_id, 0);
    long           sec   = NUM2LONG(rsecs);
    long long      num   = NUM2LL(rb_funcall(rfrac, numerator_id, 0));
    long long      den   = NUM2LL(rb_funcall(rfrac, denominator_id, 0));

    num += sec * den;

    return rb_funcall(rb_cObject, rational_id, 2, rb_ll2inum(num), rb_ll2inum(den));
}

static void print_odd(Odd odd) {
    const char **np;
    int          i;

    printf("  %s {\n", odd->classname);
    printf("    attr_cnt: %d %p\n", odd->attr_cnt, (void *)odd->attr_names);
    printf("    attr_names: %p\n", (void *)*odd->attr_names);
    printf("    attr_names: %c\n", **odd->attr_names);
    for (i = odd->attr_cnt, np = odd->attr_names; 0 < i; i--, np++) {
        printf("    %d %s\n", i, *np);
    }
    printf("  }\n");
}

void print_all_odds(const char *label) {
    Odd odd;
    printf("@ %s {\n", label);
    for (odd = odds; NULL != odd; odd = odd->next) {
        print_odd(odd);
    }
    printf("}\n");
}

static Odd odd_create(void) {
    Odd odd = OJ_R_ALLOC(struct _odd);

    memset(odd, 0, sizeof(struct _odd));
    odd->next = odds;
    odds      = odd;

    return odd;
}

void oj_odd_init(void) {
    Odd          odd;
    const char **np;

    sec_id          = rb_intern("sec");
    sec_fraction_id = rb_intern("sec_fraction");
    to_f_id         = rb_intern("to_f");
    numerator_id    = rb_intern("numerator");
    denominator_id  = rb_intern("denominator");
    rational_id     = rb_intern("Rational");

    // Rational
    odd   = odd_create();
    np    = odd->attr_names;
    *np++ = "numerator";
    *np++ = "denominator";
    *np   = 0;
    set_class(odd, "Rational");
    odd->create_obj = rb_cObject;
    odd->create_op  = rational_id;
    odd->attr_cnt   = 2;

    // Date
    odd   = odd_create();
    np    = odd->attr_names;
    *np++ = "year";
    *np++ = "month";
    *np++ = "day";
    *np++ = "start";
    *np++ = 0;
    set_class(odd, "Date");
    odd->attr_cnt = 4;

    // DateTime
    odd   = odd_create();
    np    = odd->attr_names;
    *np++ = "year";
    *np++ = "month";
    *np++ = "day";
    *np++ = "hour";
    *np++ = "min";
    *np++ = "sec";
    *np++ = "offset";
    *np++ = "start";
    *np++ = 0;
    set_class(odd, "DateTime");
    odd->attr_cnt     = 8;
    odd->attrFuncs[5] = get_datetime_secs;

    // Range
    odd   = odd_create();
    np    = odd->attr_names;
    *np++ = "begin";
    *np++ = "end";
    *np++ = "exclude_end?";
    *np++ = 0;
    set_class(odd, "Range");
    odd->attr_cnt = 3;
}

Odd oj_get_odd(VALUE clas) {
    Odd         odd;
    const char *classname = NULL;

    for (odd = odds; NULL != odd; odd = odd->next) {
        if (clas == odd->clas) {
            return odd;
        }
        if (odd->is_module) {
            if (NULL == classname) {
                classname = rb_class2name(clas);
            }
            if (0 == strncmp(odd->classname, classname, odd->clen) && ':' == classname[odd->clen]) {
                return odd;
            }
        }
    }
    return NULL;
}

Odd oj_get_oddc(const char *classname, size_t len) {
    Odd odd;

    for (odd = odds; NULL != odd; odd = odd->next) {
        if (len == odd->clen && 0 == strncmp(classname, odd->classname, len)) {
            return odd;
        }
        if (odd->is_module && 0 == strncmp(odd->classname, classname, odd->clen) && ':' == classname[odd->clen]) {
            return odd;
        }
    }
    return NULL;
}

OddArgs oj_odd_alloc_args(Odd odd) {
    OddArgs oa = OJ_R_ALLOC_N(struct _oddArgs, 1);
    VALUE  *a;
    int     i;

    oa->odd = odd;
    for (i = odd->attr_cnt, a = oa->args; 0 < i; i--, a++) {
        *a = Qnil;
    }
    return oa;
}

void oj_odd_free(OddArgs args) {
    OJ_R_FREE(args);
}

int oj_odd_set_arg(OddArgs args, const char *key, size_t klen, VALUE value) {
    const char **np;
    VALUE       *vp;
    int          i;

    for (i = args->odd->attr_cnt, np = args->odd->attr_names, vp = args->args; 0 < i; i--, np++, vp++) {
        if (0 == strncmp(key, *np, klen) && '\0' == *((*np) + klen)) {
            *vp = value;
            return 0;
        }
    }
    return -1;
}

void oj_reg_odd(VALUE clas, VALUE create_object, VALUE create_method, int mcnt, VALUE *members, bool raw) {
    Odd          odd;
    const char **np;
    ID          *ap;
    AttrGetFunc *fp;

    odd       = odd_create();
    odd->clas = clas;
    rb_gc_register_mark_object(odd->clas);
    if (NULL == (odd->classname = OJ_STRDUP(rb_class2name(clas)))) {
        rb_raise(rb_eNoMemError, "for class name.");
    }
    odd->clen       = strlen(odd->classname);
    odd->create_obj = create_object;
    rb_gc_register_mark_object(odd->create_obj);
    odd->create_op = SYM2ID(create_method);
    odd->attr_cnt  = mcnt;
    odd->is_module = (T_MODULE == rb_type(clas));
    odd->raw       = raw;
    for (ap = odd->attrs, np = odd->attr_names, fp = odd->attrFuncs; 0 < mcnt; mcnt--, ap++, np++, members++, fp++) {
        *fp = 0;
        switch (rb_type(*members)) {
        case T_STRING:
            if (NULL == (*np = OJ_STRDUP(RSTRING_PTR(*members)))) {
                rb_raise(rb_eNoMemError, "for attribute name.");
            }
            break;
        case T_SYMBOL:
            // The symbol can move and invalidate the name so make a copy.
            if (NULL == (*np = OJ_STRDUP(rb_id2name(SYM2ID(*members))))) {
                rb_raise(rb_eNoMemError, "for attribute name.");
            }
            break;
        default: rb_raise(rb_eArgError, "registered member identifiers must be Strings or Symbols."); break;
        }
        *ap = rb_intern(*np);
    }
    *np = 0;
    *ap = 0;
}