2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
struct.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Tue Mar 22 18:44:30 JST 1995
|
|
|
|
|
2003-01-16 02:34:03 -05:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
|
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_cStruct;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-03 03:19:19 -04:00
|
|
|
static VALUE struct_alloc _((VALUE));
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
VALUE
|
|
|
|
rb_struct_iv_get(c, name)
|
|
|
|
VALUE c;
|
1999-08-13 01:45:20 -04:00
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
ID id;
|
|
|
|
|
|
|
|
id = rb_intern(name);
|
|
|
|
for (;;) {
|
2001-08-29 02:28:51 -04:00
|
|
|
if (rb_ivar_defined(c, id))
|
|
|
|
return rb_ivar_get(c, id);
|
|
|
|
c = RCLASS(c)->super;
|
|
|
|
if (c == 0 || c == rb_cStruct)
|
1999-08-13 01:45:20 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_struct_s_members(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE member, ary;
|
|
|
|
VALUE *p, *pend;
|
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
member = rb_struct_iv_get(obj, "__member__");
|
1998-01-16 07:13:05 -05:00
|
|
|
if (NIL_P(member)) {
|
2000-05-24 00:34:26 -04:00
|
|
|
rb_bug("uninitialized struct");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new2(RARRAY(member)->len);
|
|
|
|
p = RARRAY(member)->ptr; pend = p + RARRAY(member)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_ary_push(ary, rb_str_new2(rb_id2name(SYM2ID(*p))));
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_members(obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE obj;
|
|
|
|
{
|
2001-08-20 00:29:58 -04:00
|
|
|
return rb_struct_s_members(rb_obj_class(obj));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_getmember(obj, id)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE obj;
|
1998-01-16 07:13:05 -05:00
|
|
|
ID id;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE member, slot;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
member = rb_struct_iv_get(rb_obj_class(obj), "__member__");
|
1998-01-16 07:13:05 -05:00
|
|
|
if (NIL_P(member)) {
|
2000-05-24 00:34:26 -04:00
|
|
|
rb_bug("uninitialized struct");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
slot = ID2SYM(id);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (i=0; i<RARRAY(member)->len; i++) {
|
|
|
|
if (RARRAY(member)->ptr[i] == slot) {
|
1998-01-16 07:19:22 -05:00
|
|
|
return RSTRUCT(obj)->ptr[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2001-07-02 04:46:28 -04:00
|
|
|
rb_name_error(id, "%s is not struct member", rb_id2name(id));
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_ref(obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE obj;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_struct_getmember(obj, rb_frame_last_func());
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE rb_struct_ref0(obj) VALUE obj; {return RSTRUCT(obj)->ptr[0];}
|
|
|
|
static VALUE rb_struct_ref1(obj) VALUE obj; {return RSTRUCT(obj)->ptr[1];}
|
|
|
|
static VALUE rb_struct_ref2(obj) VALUE obj; {return RSTRUCT(obj)->ptr[2];}
|
|
|
|
static VALUE rb_struct_ref3(obj) VALUE obj; {return RSTRUCT(obj)->ptr[3];}
|
|
|
|
static VALUE rb_struct_ref4(obj) VALUE obj; {return RSTRUCT(obj)->ptr[4];}
|
|
|
|
static VALUE rb_struct_ref5(obj) VALUE obj; {return RSTRUCT(obj)->ptr[5];}
|
|
|
|
static VALUE rb_struct_ref6(obj) VALUE obj; {return RSTRUCT(obj)->ptr[6];}
|
|
|
|
static VALUE rb_struct_ref7(obj) VALUE obj; {return RSTRUCT(obj)->ptr[7];}
|
|
|
|
static VALUE rb_struct_ref8(obj) VALUE obj; {return RSTRUCT(obj)->ptr[8];}
|
|
|
|
static VALUE rb_struct_ref9(obj) VALUE obj; {return RSTRUCT(obj)->ptr[9];}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE (*ref_func[10])() = {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_ref0,
|
|
|
|
rb_struct_ref1,
|
|
|
|
rb_struct_ref2,
|
|
|
|
rb_struct_ref3,
|
|
|
|
rb_struct_ref4,
|
|
|
|
rb_struct_ref5,
|
|
|
|
rb_struct_ref6,
|
|
|
|
rb_struct_ref7,
|
|
|
|
rb_struct_ref8,
|
|
|
|
rb_struct_ref9,
|
1998-01-16 07:13:05 -05:00
|
|
|
};
|
|
|
|
|
2001-08-05 23:08:57 -04:00
|
|
|
static void
|
|
|
|
rb_struct_modify(s)
|
|
|
|
VALUE s;
|
|
|
|
{
|
|
|
|
if (OBJ_FROZEN(s)) rb_error_frozen("Struct");
|
|
|
|
if (!OBJ_TAINTED(s) && rb_safe_level() >= 4)
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify Struct");
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_set(obj, val)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE obj, val;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE member, slot;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
member = rb_struct_iv_get(rb_obj_class(obj), "__member__");
|
1998-01-16 07:13:05 -05:00
|
|
|
if (NIL_P(member)) {
|
2002-02-14 02:49:27 -05:00
|
|
|
rb_bug("uninitialized struct");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-08-05 23:08:57 -04:00
|
|
|
rb_struct_modify(obj);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (i=0; i<RARRAY(member)->len; i++) {
|
|
|
|
slot = RARRAY(member)->ptr[i];
|
2000-04-10 01:48:43 -04:00
|
|
|
if (rb_id_attrset(SYM2ID(slot)) == rb_frame_last_func()) {
|
1998-01-16 07:19:22 -05:00
|
|
|
return RSTRUCT(obj)->ptr[i] = val;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2001-07-02 04:46:28 -04:00
|
|
|
rb_name_error(rb_frame_last_func(), "`%s' is not a struct member",
|
|
|
|
rb_id2name(rb_frame_last_func()));
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
make_struct(name, member, klass)
|
|
|
|
VALUE name, member, klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE nstr;
|
|
|
|
ID id;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(name)) {
|
|
|
|
nstr = rb_class_new(klass);
|
2003-01-05 12:40:29 -05:00
|
|
|
rb_make_metaclass(nstr, RBASIC(klass)->klass);
|
2002-01-10 15:18:39 -05:00
|
|
|
rb_class_inherited(klass, nstr);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
else {
|
2001-05-02 00:22:21 -04:00
|
|
|
char *cname = StringValuePtr(name);
|
1999-01-19 23:59:39 -05:00
|
|
|
id = rb_intern(cname);
|
1999-12-14 01:50:43 -05:00
|
|
|
if (!rb_is_const_id(id)) {
|
2001-07-02 04:46:28 -04:00
|
|
|
rb_name_error(id, "identifier %s needs to be constant", cname);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
nstr = rb_define_class_under(klass, cname, klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-08-21 11:47:54 -04:00
|
|
|
rb_iv_set(nstr, "__size__", LONG2NUM(RARRAY(member)->len));
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_iv_set(nstr, "__member__", member);
|
|
|
|
|
2002-12-20 03:33:17 -05:00
|
|
|
rb_define_alloc_func(nstr, struct_alloc);
|
2001-10-03 03:19:19 -04:00
|
|
|
rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
|
|
|
|
rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_singleton_method(nstr, "members", rb_struct_s_members, 0);
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i< RARRAY(member)->len; i++) {
|
2000-04-10 01:48:43 -04:00
|
|
|
ID id = SYM2ID(RARRAY(member)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (i<10) {
|
|
|
|
rb_define_method_id(nstr, id, ref_func[i], 0);
|
|
|
|
}
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method_id(nstr, id, rb_struct_ref, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method_id(nstr, rb_id_attrset(id), rb_struct_set, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nstr;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
#include <stdarg.h>
|
|
|
|
#define va_init_list(a,b) va_start(a,b)
|
|
|
|
#else
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <varargs.h>
|
1999-01-19 23:59:39 -05:00
|
|
|
#define va_init_list(a,b) va_start(a)
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_struct_define(const char *name, ...)
|
1999-01-19 23:59:39 -05:00
|
|
|
#else
|
|
|
|
rb_struct_define(name, va_alist)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
va_dcl
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
va_list ar;
|
|
|
|
VALUE nm, ary;
|
|
|
|
char *mem;
|
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
if (!name) nm = Qnil;
|
|
|
|
else nm = rb_str_new2(name);
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
va_init_list(ar, name);
|
1998-01-16 07:13:05 -05:00
|
|
|
while (mem = va_arg(ar, char*)) {
|
|
|
|
ID slot = rb_intern(mem);
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_ary_push(ary, ID2SYM(slot));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
va_end(ar);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return make_struct(nm, ary, rb_cStruct);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_s_def(argc, argv, klass)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-12-14 01:50:43 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE name, rest;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE st;
|
1999-12-14 01:50:43 -05:00
|
|
|
ID id;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1*", &name, &rest);
|
1999-01-19 23:59:39 -05:00
|
|
|
for (i=0; i<RARRAY(rest)->len; i++) {
|
1999-12-14 01:50:43 -05:00
|
|
|
id = rb_to_id(RARRAY(rest)->ptr[i]);
|
2000-04-10 01:48:43 -04:00
|
|
|
RARRAY(rest)->ptr[i] = ID2SYM(id);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
if (!NIL_P(name) && TYPE(name) != T_STRING) {
|
1999-12-14 01:50:43 -05:00
|
|
|
id = rb_to_id(name);
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_ary_unshift(rest, ID2SYM(id));
|
1999-12-14 01:50:43 -05:00
|
|
|
name = Qnil;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
st = make_struct(name, rest, klass);
|
|
|
|
|
|
|
|
return st;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE
|
|
|
|
rb_struct_initialize(self, values)
|
|
|
|
VALUE self, values;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-08-20 00:29:58 -04:00
|
|
|
VALUE klass = rb_obj_class(self);
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE size;
|
1999-08-13 01:45:20 -04:00
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-05 23:08:57 -04:00
|
|
|
rb_struct_modify(self);
|
2001-08-29 02:28:51 -04:00
|
|
|
size = rb_struct_iv_get(klass, "__size__");
|
2000-04-10 01:48:43 -04:00
|
|
|
n = FIX2LONG(size);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (n < RARRAY(values)->len) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "struct size differs");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
MEMCPY(RSTRUCT(self)->ptr, RARRAY(values)->ptr, VALUE, RARRAY(values)->len);
|
|
|
|
if (n > RARRAY(values)->len) {
|
|
|
|
rb_mem_clear(RSTRUCT(self)->ptr+RARRAY(values)->len,
|
|
|
|
n-RARRAY(values)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2001-10-03 03:19:19 -04:00
|
|
|
struct_alloc(klass)
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE klass;
|
|
|
|
{
|
|
|
|
VALUE size;
|
|
|
|
long n;
|
|
|
|
NEWOBJ(st, struct RStruct);
|
|
|
|
OBJSETUP(st, klass, T_STRUCT);
|
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
size = rb_struct_iv_get(klass, "__size__");
|
1999-08-13 01:45:20 -04:00
|
|
|
n = FIX2LONG(size);
|
|
|
|
|
|
|
|
st->ptr = ALLOC_N(VALUE, n);
|
|
|
|
rb_mem_clear(st->ptr, n);
|
|
|
|
st->len = n;
|
|
|
|
|
|
|
|
return (VALUE)st;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_struct_alloc(klass, values)
|
|
|
|
VALUE klass, values;
|
|
|
|
{
|
2001-10-03 03:19:19 -04:00
|
|
|
return rb_class_new_instance(RARRAY(values)->len, RARRAY(values)->ptr, klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
rb_struct_new(VALUE klass, ...)
|
|
|
|
#else
|
|
|
|
rb_struct_new(klass, va_alist)
|
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
va_dcl
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE sz, *mem;
|
|
|
|
long size, i;
|
1998-01-16 07:13:05 -05:00
|
|
|
va_list args;
|
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
sz = rb_struct_iv_get(klass, "__size__");
|
1999-08-13 01:45:20 -04:00
|
|
|
size = FIX2LONG(sz);
|
|
|
|
mem = ALLOCA_N(VALUE, size);
|
1999-01-19 23:59:39 -05:00
|
|
|
va_init_list(args, klass);
|
|
|
|
for (i=0; i<size; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
mem[i] = va_arg(args, VALUE);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
2001-10-03 03:19:19 -04:00
|
|
|
return rb_class_new_instance(size, mem, klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_each(s)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE s;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RSTRUCT(s)->len; i++) {
|
|
|
|
rb_yield(RSTRUCT(s)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return s;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-04-10 04:45:26 -04:00
|
|
|
static VALUE
|
|
|
|
rb_struct_each_pair(s)
|
|
|
|
VALUE s;
|
|
|
|
{
|
|
|
|
VALUE member;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
member = rb_struct_iv_get(rb_obj_class(s), "__member__");
|
|
|
|
if (NIL_P(member)) {
|
|
|
|
rb_bug("non-initialized struct");
|
|
|
|
}
|
|
|
|
for (i=0; i<RSTRUCT(s)->len; i++) {
|
2003-05-20 02:29:23 -04:00
|
|
|
rb_yield_values(2, RARRAY(member)->ptr[i], RSTRUCT(s)->ptr[i]);
|
2002-04-10 04:45:26 -04:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_to_s(s)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE s;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-08-20 00:29:58 -04:00
|
|
|
char *cname = rb_class2name(rb_obj_class(s));
|
2000-10-10 03:03:36 -04:00
|
|
|
VALUE str = rb_str_new(0, strlen(cname) + 4);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-10 03:03:36 -04:00
|
|
|
sprintf(RSTRING(str)->ptr, "#<%s>", cname);
|
2000-10-13 05:01:58 -04:00
|
|
|
RSTRING(str)->len = strlen(RSTRING(str)->ptr);
|
2000-10-10 03:03:36 -04:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
inspect_struct(s)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE s;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-08-20 00:29:58 -04:00
|
|
|
char *cname = rb_class2name(rb_obj_class(s));
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str, member;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
member = rb_struct_iv_get(rb_obj_class(s), "__member__");
|
1998-01-16 07:13:05 -05:00
|
|
|
if (NIL_P(member)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_bug("non-initialized struct");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-05-30 05:12:34 -04:00
|
|
|
str = rb_str_buf_new2("#<");
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_str_cat2(str, cname);
|
|
|
|
rb_str_cat2(str, " ");
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RSTRUCT(s)->len; i++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE str2, slot;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (i > 0) {
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_str_cat2(str, ", ");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
slot = RARRAY(member)->ptr[i];
|
2000-04-10 01:48:43 -04:00
|
|
|
p = rb_id2name(SYM2ID(slot));
|
|
|
|
rb_str_cat2(str, p);
|
|
|
|
rb_str_cat2(str, "=");
|
1998-01-16 07:19:22 -05:00
|
|
|
str2 = rb_inspect(RSTRUCT(s)->ptr[i]);
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_str_append(str, str2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_str_cat2(str, ">");
|
2000-02-23 00:23:12 -05:00
|
|
|
OBJ_INFECT(str, s);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_inspect(s)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE s;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_inspecting_p(s)) {
|
2001-08-20 00:29:58 -04:00
|
|
|
char *cname = rb_class2name(rb_obj_class(s));
|
2000-10-10 03:03:36 -04:00
|
|
|
VALUE str = rb_str_new(0, strlen(cname) + 8);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-10-10 03:03:36 -04:00
|
|
|
sprintf(RSTRING(str)->ptr, "#<%s:...>", cname);
|
2000-10-13 05:01:58 -04:00
|
|
|
RSTRING(str)->len = strlen(RSTRING(str)->ptr);
|
2000-10-10 03:03:36 -04:00
|
|
|
return str;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
return rb_protect_inspect(inspect_struct, s, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_to_a(s)
|
|
|
|
VALUE s;
|
|
|
|
{
|
|
|
|
return rb_ary_new4(RSTRUCT(s)->len, RSTRUCT(s)->ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2003-05-19 01:41:08 -04:00
|
|
|
rb_struct_init_copy(copy, s)
|
2002-09-03 01:20:14 -04:00
|
|
|
VALUE copy, s;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-09-03 01:20:14 -04:00
|
|
|
if (copy == s) return copy;
|
|
|
|
rb_check_frozen(copy);
|
|
|
|
if (!rb_obj_is_instance_of(s, rb_obj_class(copy))) {
|
2002-08-29 05:08:18 -04:00
|
|
|
rb_raise(rb_eTypeError, "wrong argument class");
|
2002-08-27 04:31:08 -04:00
|
|
|
}
|
2002-09-03 01:20:14 -04:00
|
|
|
RSTRUCT(copy)->ptr = ALLOC_N(VALUE, RSTRUCT(s)->len);
|
|
|
|
RSTRUCT(copy)->len = RSTRUCT(s)->len;
|
|
|
|
MEMCPY(RSTRUCT(copy)->ptr, RSTRUCT(s)->ptr, VALUE, RSTRUCT(copy)->len);
|
2000-01-04 23:41:21 -05:00
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
return copy;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_struct_aref_id(s, id)
|
|
|
|
VALUE s;
|
|
|
|
ID id;
|
|
|
|
{
|
|
|
|
VALUE member;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, len;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
member = rb_struct_iv_get(rb_obj_class(s), "__member__");
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(member)) {
|
|
|
|
rb_bug("non-initialized struct");
|
|
|
|
}
|
|
|
|
|
|
|
|
len = RARRAY(member)->len;
|
|
|
|
for (i=0; i<len; i++) {
|
2000-04-10 01:48:43 -04:00
|
|
|
if (SYM2ID(RARRAY(member)->ptr[i]) == id) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return RSTRUCT(s)->ptr[i];
|
|
|
|
}
|
|
|
|
}
|
2001-07-02 04:46:28 -04:00
|
|
|
rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_aref(s, idx)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE s, idx;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-13 02:18:45 -05:00
|
|
|
if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_struct_aref_id(s, rb_to_id(idx));
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
i = NUM2LONG(idx);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (i < 0) i = RSTRUCT(s)->len + i;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (i < 0)
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
|
1999-01-19 23:59:39 -05:00
|
|
|
i, RSTRUCT(s)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRUCT(s)->len <= i)
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
|
1999-01-19 23:59:39 -05:00
|
|
|
i, RSTRUCT(s)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
return RSTRUCT(s)->ptr[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_struct_aset_id(s, id, val)
|
|
|
|
VALUE s, val;
|
|
|
|
ID id;
|
|
|
|
{
|
|
|
|
VALUE member;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, len;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
member = rb_struct_iv_get(rb_obj_class(s), "__member__");
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(member)) {
|
|
|
|
rb_bug("non-initialized struct");
|
|
|
|
}
|
|
|
|
|
2001-08-05 23:08:57 -04:00
|
|
|
rb_struct_modify(s);
|
1999-01-19 23:59:39 -05:00
|
|
|
len = RARRAY(member)->len;
|
|
|
|
for (i=0; i<len; i++) {
|
2000-04-10 01:48:43 -04:00
|
|
|
if (SYM2ID(RARRAY(member)->ptr[i]) == id) {
|
1999-01-19 23:59:39 -05:00
|
|
|
RSTRUCT(s)->ptr[i] = val;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
2001-07-02 04:46:28 -04:00
|
|
|
rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_aset(s, idx, val)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE s, idx, val;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-13 02:18:45 -05:00
|
|
|
if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_struct_aset_id(s, rb_to_id(idx), val);
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
i = NUM2LONG(idx);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (i < 0) i = RSTRUCT(s)->len + i;
|
2000-06-14 01:30:29 -04:00
|
|
|
if (i < 0) {
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
|
1999-01-19 23:59:39 -05:00
|
|
|
i, RSTRUCT(s)->len);
|
2000-06-14 01:30:29 -04:00
|
|
|
}
|
|
|
|
if (RSTRUCT(s)->len <= i) {
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
|
1999-01-19 23:59:39 -05:00
|
|
|
i, RSTRUCT(s)->len);
|
2000-06-14 01:30:29 -04:00
|
|
|
}
|
2001-08-05 23:08:57 -04:00
|
|
|
rb_struct_modify(s);
|
1998-01-16 07:19:22 -05:00
|
|
|
return RSTRUCT(s)->ptr[i] = val;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2003-05-04 12:03:24 -04:00
|
|
|
static VALUE
|
|
|
|
rb_struct_values_at(argc, argv, s)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE s;
|
|
|
|
{
|
|
|
|
VALUE result = rb_ary_new();
|
|
|
|
long i;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_ary_push(result, rb_struct_aref(s, argv[i]));
|
|
|
|
}
|
2003-05-06 02:51:31 -04:00
|
|
|
|
|
|
|
return result;
|
2003-05-04 12:03:24 -04:00
|
|
|
}
|
2001-12-10 22:48:08 -05:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_struct_select(argc, argv, s)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE s;
|
|
|
|
{
|
2003-05-04 12:03:24 -04:00
|
|
|
VALUE result;
|
2001-12-10 22:48:08 -05:00
|
|
|
long i;
|
|
|
|
|
2003-05-04 12:03:24 -04:00
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
rb_warn("Struct#select(index..) is deprecated; use Struct#values_at");
|
|
|
|
return rb_struct_values_at(argc, argv, s);
|
2001-12-10 22:48:08 -05:00
|
|
|
}
|
2003-05-04 12:03:24 -04:00
|
|
|
if (argc > 0) {
|
|
|
|
rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
|
|
|
|
}
|
|
|
|
result = rb_ary_new();
|
|
|
|
for (i = 0; i < RSTRUCT(s)->len; i++) {
|
|
|
|
if (RTEST(rb_yield(RSTRUCT(s)->ptr[i]))) {
|
|
|
|
rb_ary_push(result, RSTRUCT(s)->ptr[i]);
|
2001-12-10 22:48:08 -05:00
|
|
|
}
|
|
|
|
}
|
2003-05-04 12:03:24 -04:00
|
|
|
|
2001-12-10 22:48:08 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_struct_equal(s, s2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE s, s2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-29 02:28:51 -04:00
|
|
|
if (s == s2) return Qtrue;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (TYPE(s2) != T_STRUCT) return Qfalse;
|
2001-08-20 00:29:58 -04:00
|
|
|
if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRUCT(s)->len != RSTRUCT(s2)->len) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_bug("inconsistent struct"); /* should never happen */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RSTRUCT(s)->len; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!rb_equal(RSTRUCT(s)->ptr[i], RSTRUCT(s2)->ptr[i])) return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2003-04-15 06:12:25 -04:00
|
|
|
static VALUE
|
|
|
|
rb_struct_hash(s)
|
|
|
|
VALUE s;
|
|
|
|
{
|
|
|
|
long i, h;
|
|
|
|
VALUE n;
|
|
|
|
|
|
|
|
h = rb_hash(rb_obj_class(s));
|
|
|
|
for (i = 0; i < RSTRUCT(s)->len; i++) {
|
|
|
|
h = (h << 1) | (h<0 ? 1 : 0);
|
|
|
|
n = rb_hash(RSTRUCT(s)->ptr[i]);
|
|
|
|
h ^= NUM2LONG(n);
|
|
|
|
}
|
|
|
|
return LONG2FIX(h);
|
|
|
|
}
|
|
|
|
|
2003-04-18 14:05:11 -04:00
|
|
|
static VALUE
|
|
|
|
rb_struct_eql(s, s2)
|
|
|
|
VALUE s, s2;
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
|
|
|
|
if (s == s2) return Qtrue;
|
|
|
|
if (TYPE(s2) != T_STRUCT) return Qfalse;
|
|
|
|
if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
|
|
|
|
if (RSTRUCT(s)->len != RSTRUCT(s2)->len) {
|
|
|
|
rb_bug("inconsistent struct"); /* should never happen */
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<RSTRUCT(s)->len; i++) {
|
|
|
|
if (!rb_eql(RSTRUCT(s)->ptr[i], RSTRUCT(s2)->ptr[i])) return Qfalse;
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
static VALUE
|
|
|
|
rb_struct_size(s)
|
|
|
|
VALUE s;
|
|
|
|
{
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(RSTRUCT(s)->len);
|
1999-12-07 04:25:55 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
|
|
|
Init_Struct()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_cStruct = rb_define_class("Struct", rb_cObject);
|
|
|
|
rb_include_module(rb_cStruct, rb_mEnumerable);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-12-20 03:33:17 -05:00
|
|
|
rb_undef_alloc_func(rb_cStruct);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_singleton_method(rb_cStruct, "new", rb_struct_s_def, -1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_method(rb_cStruct, "initialize", rb_struct_initialize, -2);
|
2003-05-19 01:41:08 -04:00
|
|
|
rb_define_method(rb_cStruct, "initialize_copy", rb_struct_init_copy, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cStruct, "==", rb_struct_equal, 1);
|
2003-04-18 14:05:11 -04:00
|
|
|
rb_define_method(rb_cStruct, "eql?", rb_struct_eql, 1);
|
2003-04-15 06:12:25 -04:00
|
|
|
rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cStruct, "to_s", rb_struct_to_s, 0);
|
|
|
|
rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
|
|
|
|
rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0);
|
|
|
|
rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0);
|
1999-12-07 04:25:55 -05:00
|
|
|
rb_define_method(rb_cStruct, "size", rb_struct_size, 0);
|
|
|
|
rb_define_method(rb_cStruct, "length", rb_struct_size, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cStruct, "each", rb_struct_each, 0);
|
2002-04-10 04:45:26 -04:00
|
|
|
rb_define_method(rb_cStruct, "each_pair", rb_struct_each_pair, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cStruct, "[]", rb_struct_aref, 1);
|
|
|
|
rb_define_method(rb_cStruct, "[]=", rb_struct_aset, 2);
|
2001-12-10 22:48:08 -05:00
|
|
|
rb_define_method(rb_cStruct, "select", rb_struct_select, -1);
|
2003-05-04 12:03:24 -04:00
|
|
|
rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cStruct, "members", rb_struct_members, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|