summaryrefslogtreecommitdiff
path: root/struct.c
blob: 8ba8f4336e13e2abaa2355e434b4d854d35fdd72 (plain)
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
/************************************************

  struct.c -

  $Author: matz $
  $Date: 1995/01/10 10:43:02 $
  created at: Tue Mar 22 18:44:30 JST 1995

************************************************/

#include "ruby.h"
#include "env.h"

VALUE C_Struct;
extern VALUE M_Enumerable;

char *strdup();

static VALUE
struct_alloc(class, name)
    VALUE class;
    char *name;
{
    NEWOBJ(st, struct RStruct);
    OBJSETUP(st, class, T_STRUCT);

    if (name) st->name = strdup(name);
    else st->name = Qnil;
    st->len = 0;
    st->tbl = Qnil;

    return (VALUE)st;
}

static VALUE
struct_find(s, id)
    struct RStruct *s;
    ID id;
{
    struct kv_pair *t, *tend;

    t = s->tbl;
    tend = t + s->len;
    while (t < tend) {
	if (t->key == id) return t->value;
	t++;
    }
    Fail("struct %s has no member %s", s->name, rb_id2name(id));
}

static VALUE
Fstruct_access(s)
    struct RStruct *s;
{
    return struct_find(s, the_env->last_func);
}

static VALUE
struct_add(s, mem, val)
    struct RStruct *s;
    char *mem;
    VALUE val;
{
    int pos = s->len;

    s->len++;
    if (s->tbl == Qnil) {
	s->tbl = ALLOC_N(struct kv_pair, 1);
    }
    else {
	REALLOC_N(s->tbl, struct kv_pair, s->len);
    }

    s->tbl[pos].key = rb_intern(mem);
    s->tbl[pos].value = val;
    rb_define_single_method(s, mem, Fstruct_access, 0);
}

#include <varargs.h>

VALUE
struct_new(name, va_alist)
    char *name;
    va_dcl
{
    VALUE st;
    va_list args;
    char *mem;

    st = struct_alloc(C_Struct,name);
    va_start(args);
    while (mem = va_arg(args, char*)) {
	struct_add(st, mem, va_arg(args, VALUE));
    }
    va_end(vargs);

    return st;
}

#define ASSOC_KEY(a) RCONS(a)->car
#define ASSOC_VAL(a) RCONS(a)->cdr

static VALUE
Sstruct_new(argc, argv, class)
    int argc;
    VALUE *argv;
    VALUE class;
{
    VALUE name, st;
    struct RArray *tbl;
    int i, max;

    rb_scan_args(argc, argv, "1*", &name, &tbl);
    Check_Type(name, T_STRING);

    st = struct_alloc(class, RSTRING(name)->ptr);
    for (i=0, max=tbl->len; i<max; i++) {
	VALUE assoc = tbl->ptr[i];

	Check_Type(assoc, T_CONS);
	Check_Type(ASSOC_KEY(assoc), T_STRING);
	struct_add(st, RSTRING(ASSOC_KEY(assoc))->ptr, ASSOC_VAL(assoc));
    }

    return st;
}

static VALUE
Fstruct_each(s)
    struct RStruct *s;
{
    struct kv_pair *t, *tend;

    t = s->tbl;
    tend = t + s->len;
    while (t < tend) {
	rb_yield(t->value);
	t++;
    }
}

static VALUE
Fstruct_values(s)
    struct RStruct *s;
{
    VALUE ary;
    struct kv_pair *t, *tend;

    ary = ary_new();
    t = s->tbl;
    tend = t + s->len;
    while (t < tend) {
	ary_push(ary, t->value);
	t++;
    }

    return ary;
}

static VALUE
Fstruct_aref(s, idx)
    struct RStruct *s;
    VALUE idx;
{
    struct RArray *ary;
    int i;

    if (TYPE(idx) == T_STRING)
	return struct_find(rb_intern(RSTRING(idx)->ptr));

    i = NUM2INT(idx);
    if (s->len <= i)
	Fail("offset %d too large for struct(size:%d)", i, s->len);
    return s->tbl[i].value;
}

#define HDR "struct "

static VALUE
Fstruct_to_s(s)
    struct RStruct *s;
{
    char *buf;

    buf = ALLOCA_N(char, strlen(s->name)+sizeof(HDR)+1);
    sprintf(buf, "%s%s", HDR, s->name);
    return str_new2(buf);
}

static VALUE
Fstruct_inspect(s)
    struct RStruct *s;
{
    VALUE str, str2;
    char buf[256], *p;
    int i;
    ID inspect = rb_intern("_inspect");

    sprintf(buf, "#<%s%s: ", HDR, s->name);
    str = str_new2(buf);
    for (i=0; i<s->len; i++) {
	if (i > 0) {
	    str_cat(str, ", ", 2);
	}
	p = rb_id2name(s->tbl[i].key);
	str_cat(str, p, strlen(p));
	str_cat(str, "=", 1);
	str2 = rb_funcall(s->tbl[i].value, inspect, 0, Qnil);
	str_cat(str, RSTRING(str2)->ptr, RSTRING(str2)->len);
    }
    str_cat(str, ">", 1);

    return str;
}

static VALUE
Fstruct_to_a(s)
    struct RStruct *s;
{
    VALUE ary;
    int i;

    ary = ary_new2(s->len);
    for (i=0; i<s->len; i++) {
	ary_push(ary, s->tbl[i].value);
    }

    return ary;
}

static VALUE
Fstruct_clone(s)
    struct RStruct *s;
{
    struct RStruct *st = (struct RStruct*)struct_alloc(s->name);

    CLONESETUP(st, s);
    st->len = s->len;
    st->tbl = ALLOC_N(struct kv_pair, s->len);
    MEMCPY(st->tbl, s->tbl, struct kv_pair, st->len);
    RBASIC(st)->class = single_class_clone(RBASIC(s)->class);
    return (VALUE)st;
}

Init_Struct()
{
    C_Struct = rb_define_class("Struct", C_Object);
    rb_include_module(C_Struct, M_Enumerable);

    rb_define_single_method(C_Struct, "new", Sstruct_new, -1);
    rb_define_method(C_Struct, "clone", Fstruct_clone, 0);

    rb_define_method(C_Struct, "to_s", Fstruct_to_s, 0);
    rb_define_method(C_Struct, "_inspect", Fstruct_inspect, 0);
    rb_define_method(C_Struct, "to_a", Fstruct_to_a, 0);

    rb_define_method(C_Struct, "each", Fstruct_each, 0);
    rb_define_method(C_Struct, "values", Fstruct_values, 0);
    rb_define_method(C_Struct, "[]", Fstruct_aref, 1);
}