Skip to content
Newer
Older
100644 531 lines (453 sloc) 11.5 KB
3db12e8 Initial revision
matz authored Jan 16, 1998
1 /************************************************
2
3 struct.c -
4
5 $Author$
6 $Date$
7 created at: Tue Mar 22 18:44:30 JST 1995
8
9 ************************************************/
10
11 #include "ruby.h"
12
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
13 #ifdef USE_CWGUSI
14 #include <stdio.h>
15 #endif
16
17 VALUE rb_cStruct;
3db12e8 Initial revision
matz authored Jan 16, 1998
18
19 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
20 class_of(obj)
3db12e8 Initial revision
matz authored Jan 16, 1998
21 VALUE obj;
22 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
23 obj = CLASS_OF(obj);
24 if (FL_TEST(obj, FL_SINGLETON))
25 return RCLASS(obj)->super;
26 return obj;
27 }
3db12e8 Initial revision
matz authored Jan 16, 1998
28
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
29 static VALUE
30 rb_struct_s_members(obj)
31 VALUE obj;
32 {
33 VALUE member, ary;
34 VALUE *p, *pend;
35
36 member = rb_iv_get(obj, "__member__");
3db12e8 Initial revision
matz authored Jan 16, 1998
37 if (NIL_P(member)) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
38 rb_bug("non-initialized struct");
3db12e8 Initial revision
matz authored Jan 16, 1998
39 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
40 ary = rb_ary_new2(RARRAY(member)->len);
41 p = RARRAY(member)->ptr; pend = p + RARRAY(member)->len;
3db12e8 Initial revision
matz authored Jan 16, 1998
42 while (p < pend) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
43 rb_ary_push(ary, rb_str_new2(rb_id2name(FIX2INT(*p))));
3db12e8 Initial revision
matz authored Jan 16, 1998
44 p++;
45 }
46
47 return ary;
48 }
49
50 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
51 rb_struct_members(obj)
3db12e8 Initial revision
matz authored Jan 16, 1998
52 VALUE obj;
53 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
54 return rb_struct_s_members(class_of(obj));
3db12e8 Initial revision
matz authored Jan 16, 1998
55 }
56
57 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
58 rb_struct_getmember(obj, id)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
59 VALUE obj;
3db12e8 Initial revision
matz authored Jan 16, 1998
60 ID id;
61 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
62 VALUE member, slot;
3db12e8 Initial revision
matz authored Jan 16, 1998
63 int i;
64
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
65 member = rb_iv_get(class_of(obj), "__member__");
3db12e8 Initial revision
matz authored Jan 16, 1998
66 if (NIL_P(member)) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
67 rb_bug("non-initialized struct");
3db12e8 Initial revision
matz authored Jan 16, 1998
68 }
69 slot = INT2FIX(id);
70 for (i=0; i<RARRAY(member)->len; i++) {
71 if (RARRAY(member)->ptr[i] == slot) {
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
72 return RSTRUCT(obj)->ptr[i];
3db12e8 Initial revision
matz authored Jan 16, 1998
73 }
74 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
75 rb_raise(rb_eNameError, "%s is not struct member", rb_id2name(id));
76 return Qnil; /* not reached */
3db12e8 Initial revision
matz authored Jan 16, 1998
77 }
78
79 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
80 rb_struct_ref(obj)
3db12e8 Initial revision
matz authored Jan 16, 1998
81 VALUE obj;
82 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
83 return rb_struct_getmember(obj, rb_frame_last_func());
3db12e8 Initial revision
matz authored Jan 16, 1998
84 }
85
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
86 static VALUE rb_struct_ref0(obj) VALUE obj; {return RSTRUCT(obj)->ptr[0];}
87 static VALUE rb_struct_ref1(obj) VALUE obj; {return RSTRUCT(obj)->ptr[1];}
88 static VALUE rb_struct_ref2(obj) VALUE obj; {return RSTRUCT(obj)->ptr[2];}
89 static VALUE rb_struct_ref3(obj) VALUE obj; {return RSTRUCT(obj)->ptr[3];}
90 static VALUE rb_struct_ref4(obj) VALUE obj; {return RSTRUCT(obj)->ptr[4];}
91 static VALUE rb_struct_ref5(obj) VALUE obj; {return RSTRUCT(obj)->ptr[5];}
92 static VALUE rb_struct_ref6(obj) VALUE obj; {return RSTRUCT(obj)->ptr[6];}
93 static VALUE rb_struct_ref7(obj) VALUE obj; {return RSTRUCT(obj)->ptr[7];}
94 static VALUE rb_struct_ref8(obj) VALUE obj; {return RSTRUCT(obj)->ptr[8];}
95 static VALUE rb_struct_ref9(obj) VALUE obj; {return RSTRUCT(obj)->ptr[9];}
3db12e8 Initial revision
matz authored Jan 16, 1998
96
97 VALUE (*ref_func[10])() = {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
98 rb_struct_ref0,
99 rb_struct_ref1,
100 rb_struct_ref2,
101 rb_struct_ref3,
102 rb_struct_ref4,
103 rb_struct_ref5,
104 rb_struct_ref6,
105 rb_struct_ref7,
106 rb_struct_ref8,
107 rb_struct_ref9,
3db12e8 Initial revision
matz authored Jan 16, 1998
108 };
109
110 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
111 rb_struct_set(obj, val)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
112 VALUE obj, val;
3db12e8 Initial revision
matz authored Jan 16, 1998
113 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
114 VALUE member, slot;
3db12e8 Initial revision
matz authored Jan 16, 1998
115 int i;
116
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
117 member = rb_iv_get(class_of(obj), "__member__");
3db12e8 Initial revision
matz authored Jan 16, 1998
118 if (NIL_P(member)) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
119 rb_bug("non-initialized struct");
3db12e8 Initial revision
matz authored Jan 16, 1998
120 }
121 for (i=0; i<RARRAY(member)->len; i++) {
122 slot = RARRAY(member)->ptr[i];
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
123 if (rb_id_attrset(FIX2INT(slot)) == rb_frame_last_func()) {
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
124 return RSTRUCT(obj)->ptr[i] = val;
3db12e8 Initial revision
matz authored Jan 16, 1998
125 }
126 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
127 rb_raise(rb_eNameError, "not struct member");
128 return Qnil; /* not reached */
3db12e8 Initial revision
matz authored Jan 16, 1998
129 }
130
131 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
132 make_struct(name, member, klass)
133 VALUE name, member, klass;
3db12e8 Initial revision
matz authored Jan 16, 1998
134 {
135 VALUE nstr;
136 ID id;
137 int i;
138
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
139 if (NIL_P(name)) {
140 nstr = rb_class_new(klass);
141 }
142 else {
143 char *cname = STR2CSTR(name);
144 id = rb_intern(cname);
145 if (!rb_is_const_id(id)) {
146 rb_raise(rb_eNameError, "identifier %s needs to be constant", cname);
147 }
148 nstr = rb_define_class_under(klass, cname, klass);
3db12e8 Initial revision
matz authored Jan 16, 1998
149 }
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
150 rb_iv_set(nstr, "__size__", INT2FIX(RARRAY(member)->len));
3db12e8 Initial revision
matz authored Jan 16, 1998
151 rb_iv_set(nstr, "__member__", member);
152
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
153 rb_define_singleton_method(nstr, "new", rb_struct_alloc, -2);
154 rb_define_singleton_method(nstr, "[]", rb_struct_alloc, -2);
155 rb_define_singleton_method(nstr, "members", rb_struct_s_members, 0);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
156 for (i=0; i< RARRAY(member)->len; i++) {
157 ID id = FIX2INT(RARRAY(member)->ptr[i]);
3db12e8 Initial revision
matz authored Jan 16, 1998
158 if (i<10) {
159 rb_define_method_id(nstr, id, ref_func[i], 0);
160 }
161 else {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
162 rb_define_method_id(nstr, id, rb_struct_ref, 0);
3db12e8 Initial revision
matz authored Jan 16, 1998
163 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
164 rb_define_method_id(nstr, rb_id_attrset(id), rb_struct_set, 1);
3db12e8 Initial revision
matz authored Jan 16, 1998
165 }
166
167 return nstr;
168 }
169
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
170 #ifdef HAVE_STDARG_PROTOTYPES
171 #include <stdarg.h>
172 #define va_init_list(a,b) va_start(a,b)
173 #else
3db12e8 Initial revision
matz authored Jan 16, 1998
174 #include <varargs.h>
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
175 #define va_init_list(a,b) va_start(a)
176 #endif
3db12e8 Initial revision
matz authored Jan 16, 1998
177
178 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
179 #ifdef HAVE_STDARG_PROTOTYPES
180 rb_struct_define(char *name, ...)
181 #else
182 rb_struct_define(name, va_alist)
3db12e8 Initial revision
matz authored Jan 16, 1998
183 char *name;
184 va_dcl
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
185 #endif
3db12e8 Initial revision
matz authored Jan 16, 1998
186 {
187 va_list ar;
188 VALUE nm, ary;
189 char *mem;
190
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
191 nm = rb_str_new2(name);
192 ary = rb_ary_new();
3db12e8 Initial revision
matz authored Jan 16, 1998
193
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
194 va_init_list(ar, name);
3db12e8 Initial revision
matz authored Jan 16, 1998
195 while (mem = va_arg(ar, char*)) {
196 ID slot = rb_intern(mem);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
197 rb_ary_push(ary, INT2FIX(slot));
3db12e8 Initial revision
matz authored Jan 16, 1998
198 }
199 va_end(ar);
200
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
201 return make_struct(nm, ary, rb_cStruct);
3db12e8 Initial revision
matz authored Jan 16, 1998
202 }
203
204 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
205 rb_struct_s_def(argc, argv, klass)
3db12e8 Initial revision
matz authored Jan 16, 1998
206 int argc;
207 VALUE *argv;
208 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
209 VALUE name, rest;
3db12e8 Initial revision
matz authored Jan 16, 1998
210 int i;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
211 VALUE st;
3db12e8 Initial revision
matz authored Jan 16, 1998
212
213 rb_scan_args(argc, argv, "1*", &name, &rest);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
214 for (i=0; i<RARRAY(rest)->len; i++) {
215 ID id = rb_to_id(RARRAY(rest)->ptr[i]);
216 RARRAY(rest)->ptr[i] = INT2FIX(id);
3db12e8 Initial revision
matz authored Jan 16, 1998
217 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
218 st = make_struct(name, rest, klass);
219 rb_obj_call_init(st);
220
221 return st;
3db12e8 Initial revision
matz authored Jan 16, 1998
222 }
223
224 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
225 rb_struct_alloc(klass, values)
226 VALUE klass, values;
3db12e8 Initial revision
matz authored Jan 16, 1998
227 {
228 VALUE size;
229 int n;
230
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
231 size = rb_iv_get(klass, "__size__");
3db12e8 Initial revision
matz authored Jan 16, 1998
232 n = FIX2INT(size);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
233 if (n != RARRAY(values)->len) {
234 rb_raise(rb_eArgError, "struct size differs");
3db12e8 Initial revision
matz authored Jan 16, 1998
235 }
236 else {
237 NEWOBJ(st, struct RStruct);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
238 OBJSETUP(st, klass, T_STRUCT);
239 st->len = 0; /* avoid GC crashing */
3db12e8 Initial revision
matz authored Jan 16, 1998
240 st->ptr = ALLOC_N(VALUE, n);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
241 st->len = n;
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
242 MEMCPY(st->ptr, RARRAY(values)->ptr, VALUE, RARRAY(values)->len);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
243 rb_obj_call_init((VALUE)st);
3db12e8 Initial revision
matz authored Jan 16, 1998
244
245 return (VALUE)st;
246 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
247 return Qnil; /* not reached */
3db12e8 Initial revision
matz authored Jan 16, 1998
248 }
249
250 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
251 #ifdef HAVE_STDARG_PROTOTYPES
252 rb_struct_new(VALUE klass, ...)
253 #else
254 rb_struct_new(klass, va_alist)
255 VALUE klass;
3db12e8 Initial revision
matz authored Jan 16, 1998
256 va_dcl
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
257 #endif
3db12e8 Initial revision
matz authored Jan 16, 1998
258 {
259 VALUE val, mem;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
260 int size, i;
3db12e8 Initial revision
matz authored Jan 16, 1998
261 va_list args;
262
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
263 val = rb_iv_get(klass, "__size__");
3db12e8 Initial revision
matz authored Jan 16, 1998
264 size = FIX2INT(val);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
265 mem = rb_ary_new2(size);
266 va_init_list(args, klass);
267 for (i=0; i<size; i++) {
3db12e8 Initial revision
matz authored Jan 16, 1998
268 val = va_arg(args, VALUE);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
269 rb_ary_store(mem, i, val);
3db12e8 Initial revision
matz authored Jan 16, 1998
270 }
271 va_end(args);
272
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
273 return rb_struct_alloc(klass, mem);
3db12e8 Initial revision
matz authored Jan 16, 1998
274 }
275
276 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
277 rb_struct_each(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
278 VALUE s;
3db12e8 Initial revision
matz authored Jan 16, 1998
279 {
280 int i;
281
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
282 for (i=0; i<RSTRUCT(s)->len; i++) {
283 rb_yield(RSTRUCT(s)->ptr[i]);
3db12e8 Initial revision
matz authored Jan 16, 1998
284 }
285 return Qnil;
286 }
287
288 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
289 rb_struct_to_s(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
290 VALUE s;
3db12e8 Initial revision
matz authored Jan 16, 1998
291 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
292 char *cname = rb_class2name(CLASS_OF(s));
293 char *buf = ALLOCA_N(char, strlen(cname) + 4);
3db12e8 Initial revision
matz authored Jan 16, 1998
294
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
295 sprintf(buf, "#<%s>", cname);
296 return rb_str_new2(buf);
3db12e8 Initial revision
matz authored Jan 16, 1998
297 }
298
299 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
300 inspect_struct(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
301 VALUE s;
3db12e8 Initial revision
matz authored Jan 16, 1998
302 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
303 char *cname = rb_class2name(CLASS_OF(s));
3db12e8 Initial revision
matz authored Jan 16, 1998
304 VALUE str, member;
305 int i;
306
307 member = rb_iv_get(CLASS_OF(s), "__member__");
308 if (NIL_P(member)) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
309 rb_bug("non-initialized struct");
3db12e8 Initial revision
matz authored Jan 16, 1998
310 }
311
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
312 str = rb_str_new2("#<");
313 rb_str_cat(str, cname, strlen(cname));
314 rb_str_cat(str, " ", 1);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
315 for (i=0; i<RSTRUCT(s)->len; i++) {
3db12e8 Initial revision
matz authored Jan 16, 1998
316 VALUE str2, slot;
317 char *p;
318
319 if (i > 0) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
320 rb_str_cat(str, ", ", 2);
3db12e8 Initial revision
matz authored Jan 16, 1998
321 }
322 slot = RARRAY(member)->ptr[i];
323 p = rb_id2name(FIX2INT(slot));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
324 rb_str_cat(str, p, strlen(p));
325 rb_str_cat(str, "=", 1);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
326 str2 = rb_inspect(RSTRUCT(s)->ptr[i]);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
327 rb_str_cat(str, RSTRING(str2)->ptr, RSTRING(str2)->len);
3db12e8 Initial revision
matz authored Jan 16, 1998
328 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
329 rb_str_cat(str, ">", 1);
3db12e8 Initial revision
matz authored Jan 16, 1998
330
331 return str;
332 }
333
334 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
335 rb_struct_inspect(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
336 VALUE s;
3db12e8 Initial revision
matz authored Jan 16, 1998
337 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
338 if (rb_inspecting_p(s)) {
339 char *cname = rb_class2name(CLASS_OF(s));
340 char *buf = ALLOCA_N(char, strlen(cname) + 8);
341
342 sprintf(buf, "#<%s:...>", cname);
343 return rb_str_new2(buf);
344 }
345 return rb_protect_inspect(inspect_struct, s, 0);
3db12e8 Initial revision
matz authored Jan 16, 1998
346 }
347
348 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
349 rb_struct_to_a(s)
350 VALUE s;
351 {
352 return rb_ary_new4(RSTRUCT(s)->len, RSTRUCT(s)->ptr);
353 }
354
355 static VALUE
356 rb_struct_clone(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
357 VALUE s;
3db12e8 Initial revision
matz authored Jan 16, 1998
358 {
359 NEWOBJ(st, struct RStruct);
360 CLONESETUP(st, s);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
361 st->len = 0; /* avoid GC crashing */
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
362 st->ptr = ALLOC_N(VALUE, RSTRUCT(s)->len);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
363 st->len = RSTRUCT(s)->len;
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
364 MEMCPY(st->ptr, RSTRUCT(s)->ptr, VALUE, st->len);
3db12e8 Initial revision
matz authored Jan 16, 1998
365
366 return (VALUE)st;
367 }
368
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
369 static VALUE
370 rb_struct_aref_id(s, id)
371 VALUE s;
372 ID id;
373 {
374 VALUE member;
375 int i, len;
376
377 member = rb_iv_get(CLASS_OF(s), "__member__");
378 if (NIL_P(member)) {
379 rb_bug("non-initialized struct");
380 }
381
382 len = RARRAY(member)->len;
383 for (i=0; i<len; i++) {
384 if (FIX2UINT(RARRAY(member)->ptr[i]) == id) {
385 return RSTRUCT(s)->ptr[i];
386 }
387 }
388 rb_raise(rb_eNameError, "no member '%s' in struct", rb_id2name(id));
389 return Qnil; /* not reached */
390 }
391
3db12e8 Initial revision
matz authored Jan 16, 1998
392 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
393 rb_struct_aref(s, idx)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
394 VALUE s, idx;
3db12e8 Initial revision
matz authored Jan 16, 1998
395 {
396 int i;
397
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
398 if (TYPE(idx) == T_STRING) {
399 return rb_struct_aref_id(s, rb_to_id(idx));
400 }
401
3db12e8 Initial revision
matz authored Jan 16, 1998
402 i = NUM2INT(idx);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
403 if (i < 0) i = RSTRUCT(s)->len + i;
3db12e8 Initial revision
matz authored Jan 16, 1998
404 if (i < 0)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
405 rb_raise(rb_eIndexError, "offset %d too small for struct(size:%d)",
406 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
407 if (RSTRUCT(s)->len <= i)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
408 rb_raise(rb_eIndexError, "offset %d too large for struct(size:%d)",
409 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
410 return RSTRUCT(s)->ptr[i];
3db12e8 Initial revision
matz authored Jan 16, 1998
411 }
412
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
413 static VALUE
414 rb_struct_aset_id(s, id, val)
415 VALUE s, val;
416 ID id;
417 {
418 VALUE member;
419 int i, len;
420
421 member = rb_iv_get(CLASS_OF(s), "__member__");
422 if (NIL_P(member)) {
423 rb_bug("non-initialized struct");
424 }
425
426 len = RARRAY(member)->len;
427 for (i=0; i<len; i++) {
428 if (FIX2UINT(RARRAY(member)->ptr[i]) == id) {
429 RSTRUCT(s)->ptr[i] = val;
430 return val;
431 }
432 }
433 rb_raise(rb_eNameError, "no member '%s' in struct", rb_id2name(id));
434 }
435
3db12e8 Initial revision
matz authored Jan 16, 1998
436 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
437 rb_struct_aset(s, idx, val)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
438 VALUE s, idx, val;
3db12e8 Initial revision
matz authored Jan 16, 1998
439 {
440 int i;
441
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
442 if (TYPE(idx) == T_STRING) {
443 return rb_struct_aset_id(s, rb_to_id(idx), val);
444 }
445
3db12e8 Initial revision
matz authored Jan 16, 1998
446 i = NUM2INT(idx);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
447 if (i < 0) i = RSTRUCT(s)->len + i;
3db12e8 Initial revision
matz authored Jan 16, 1998
448 if (i < 0)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
449 rb_raise(rb_eIndexError, "offset %d too small for struct(size:%d)",
450 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
451 if (RSTRUCT(s)->len <= i)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
452 rb_raise(rb_eIndexError, "offset %d too large for struct(size:%d)",
453 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
454 return RSTRUCT(s)->ptr[i] = val;
3db12e8 Initial revision
matz authored Jan 16, 1998
455 }
456
457 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
458 rb_struct_equal(s, s2)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
459 VALUE s, s2;
3db12e8 Initial revision
matz authored Jan 16, 1998
460 {
461 int i;
462
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
463 if (TYPE(s2) != T_STRUCT) return Qfalse;
464 if (CLASS_OF(s) != CLASS_OF(s2)) return Qfalse;
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
465 if (RSTRUCT(s)->len != RSTRUCT(s2)->len) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
466 rb_bug("inconsistent struct"); /* should never happen */
3db12e8 Initial revision
matz authored Jan 16, 1998
467 }
468
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
469 for (i=0; i<RSTRUCT(s)->len; i++) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
470 if (!rb_equal(RSTRUCT(s)->ptr[i], RSTRUCT(s2)->ptr[i])) return Qfalse;
3db12e8 Initial revision
matz authored Jan 16, 1998
471 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
472 return Qtrue;
3db12e8 Initial revision
matz authored Jan 16, 1998
473 }
474
475 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
476 rb_struct_eql(s, s2)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
477 VALUE s, s2;
3db12e8 Initial revision
matz authored Jan 16, 1998
478 {
479 int i;
480
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
481 if (TYPE(s2) != T_STRUCT) return Qfalse;
482 if (CLASS_OF(s) != CLASS_OF(s2)) return Qfalse;
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
483 if (RSTRUCT(s)->len != RSTRUCT(s2)->len) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
484 rb_bug("inconsistent struct"); /* should never happen */
3db12e8 Initial revision
matz authored Jan 16, 1998
485 }
486
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
487 for (i=0; i<RSTRUCT(s)->len; i++) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
488 if (!rb_eql(RSTRUCT(s)->ptr[i], RSTRUCT(s2)->ptr[i])) return Qfalse;
3db12e8 Initial revision
matz authored Jan 16, 1998
489 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
490 return Qtrue;
3db12e8 Initial revision
matz authored Jan 16, 1998
491 }
492
493 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
494 rb_struct_hash(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
495 VALUE s;
3db12e8 Initial revision
matz authored Jan 16, 1998
496 {
497 int i, h;
498
499 h = CLASS_OF(s);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored Jan 16, 1998
500 for (i=0; i<RSTRUCT(s)->len; i++) {
501 h ^= rb_hash(RSTRUCT(s)->ptr[i]);
3db12e8 Initial revision
matz authored Jan 16, 1998
502 }
503 return INT2FIX(h);
504 }
505
506 void
507 Init_Struct()
508 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
509 rb_cStruct = rb_define_class("Struct", rb_cObject);
510 rb_include_module(rb_cStruct, rb_mEnumerable);
3db12e8 Initial revision
matz authored Jan 16, 1998
511
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
512 rb_define_singleton_method(rb_cStruct, "new", rb_struct_s_def, -1);
3db12e8 Initial revision
matz authored Jan 16, 1998
513
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
514 rb_define_method(rb_cStruct, "clone", rb_struct_clone, 0);
3db12e8 Initial revision
matz authored Jan 16, 1998
515
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
516 rb_define_method(rb_cStruct, "==", rb_struct_equal, 1);
517 rb_define_method(rb_cStruct, "eql?", rb_struct_eql, 1);
518 rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);
3db12e8 Initial revision
matz authored Jan 16, 1998
519
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
520 rb_define_method(rb_cStruct, "to_s", rb_struct_to_s, 0);
521 rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
522 rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0);
523 rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0);
3db12e8 Initial revision
matz authored Jan 16, 1998
524
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
525 rb_define_method(rb_cStruct, "each", rb_struct_each, 0);
526 rb_define_method(rb_cStruct, "[]", rb_struct_aref, 1);
527 rb_define_method(rb_cStruct, "[]=", rb_struct_aset, 2);
3db12e8 Initial revision
matz authored Jan 16, 1998
528
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored Jan 20, 1999
529 rb_define_method(rb_cStruct, "members", rb_struct_members, 0);
3db12e8 Initial revision
matz authored Jan 16, 1998
530 }
Something went wrong with that request. Please try again.