From 042e5013a3bc42a3637aa1646e9fc002436b852f Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 3 Aug 2013 00:31:02 +0000 Subject: [PATCH] struct.c: rb_struct_define_under * struct.c (rb_struct_define_under): new function to define Struct under the given namespace, not under Struct. [Feature #8264] * ext/etc/etc.c: use rb_struct_define_under. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42348 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ ext/etc/etc.c | 42 ++++++++++++++++++++++-------------------- include/ruby/intern.h | 1 + struct.c | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 000c468926..8d206876a5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat Aug 3 09:30:57 2013 Nobuyoshi Nakada + + * struct.c (rb_struct_define_under): new function to define Struct + under the given namespace, not under Struct. [Feature #8264] + + * ext/etc/etc.c: use rb_struct_define_under. + Sat Aug 3 06:55:29 2013 NAKAMURA Usaku * parse.y (value_expr_gen): now NODE_DEFN and NODE_DEFS are not void diff --git a/ext/etc/etc.c b/ext/etc/etc.c index 474f3ebde0..3f14669748 100644 --- a/ext/etc/etc.c +++ b/ext/etc/etc.c @@ -672,37 +672,38 @@ Init_etc(void) rb_define_module_function(mEtc, "sysconfdir", etc_sysconfdir, 0); rb_define_module_function(mEtc, "systmpdir", etc_systmpdir, 0); - sPasswd = rb_struct_define(NULL, - "name", + sPasswd = rb_struct_define_under(mEtc, "Passwd", + "name", #ifdef HAVE_STRUCT_PASSWD_PW_PASSWD - "passwd", + "passwd", #endif - "uid", - "gid", + "uid", + "gid", #ifdef HAVE_STRUCT_PASSWD_PW_GECOS - "gecos", + "gecos", #endif - "dir", - "shell", + "dir", + "shell", #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE - "change", + "change", #endif #ifdef HAVE_STRUCT_PASSWD_PW_QUOTA - "quota", + "quota", #endif #ifdef HAVE_STRUCT_PASSWD_PW_AGE - "age", + "age", #endif #ifdef HAVE_STRUCT_PASSWD_PW_CLASS - "uclass", + "uclass", #endif #ifdef HAVE_STRUCT_PASSWD_PW_COMMENT - "comment", + "comment", #endif #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE - "expire", + "expire", #endif - NULL); + NULL); +#if 0 /* Define-const: Passwd * * Passwd is a Struct that contains the following members: @@ -743,18 +744,19 @@ Init_etc(void) * account expiration time(integer) must be compiled with +HAVE_STRUCT_PASSWD_PW_EXPIRE+ */ rb_define_const(mEtc, "Passwd", sPasswd); - rb_set_class_path(sPasswd, mEtc, "Passwd"); +#endif rb_define_const(rb_cStruct, "Passwd", sPasswd); /* deprecated name */ rb_extend_object(sPasswd, rb_mEnumerable); rb_define_singleton_method(sPasswd, "each", etc_each_passwd, 0); #ifdef HAVE_GETGRENT - sGroup = rb_struct_define(NULL, "name", + sGroup = rb_struct_define_under(mEtc, "Group", "name", #ifdef HAVE_STRUCT_GROUP_GR_PASSWD - "passwd", + "passwd", #endif - "gid", "mem", NULL); + "gid", "mem", NULL); +#if 0 /* Define-const: Group * * Group is a Struct that is only available when compiled with +HAVE_GETGRENT+. @@ -777,7 +779,7 @@ Init_etc(void) * members of the group. */ rb_define_const(mEtc, "Group", sGroup); - rb_set_class_path(sGroup, mEtc, "Group"); +#endif rb_define_const(rb_cStruct, "Group", sGroup); /* deprecated name */ rb_extend_object(sGroup, rb_mEnumerable); rb_define_singleton_method(sGroup, "each", etc_each_group, 0); diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 02213c9072..9f71f46707 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -837,6 +837,7 @@ VALUE rb_str_ellipsize(VALUE, long); /* struct.c */ VALUE rb_struct_new(VALUE, ...); VALUE rb_struct_define(const char*, ...); +VALUE rb_struct_define_under(VALUE, const char*, ...); VALUE rb_struct_alloc(VALUE, VALUE); VALUE rb_struct_initialize(VALUE, VALUE); VALUE rb_struct_aref(VALUE, VALUE); diff --git a/struct.c b/struct.c index 90af2d0568..ebb301701c 100644 --- a/struct.c +++ b/struct.c @@ -283,6 +283,25 @@ rb_struct_define(const char *name, ...) return setup_struct(st, ary); } +VALUE +rb_struct_define_under(VALUE outer, const char *name, ...) +{ + va_list ar; + VALUE ary; + char *mem; + + ary = rb_ary_tmp_new(0); + + va_start(ar, name); + while ((mem = va_arg(ar, char*)) != 0) { + ID slot = rb_intern(mem); + rb_ary_push(ary, ID2SYM(slot)); + } + va_end(ar); + + return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary); +} + /* * call-seq: * Struct.new([class_name] [, member_name]+>) -> StructClass