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