[前][次][番号順一覧][スレッド一覧]

ruby-changes:10809

From: nobu <ko1@a...>
Date: Wed, 18 Feb 2009 00:56:04 +0900 (JST)
Subject: [ruby-changes:10809] Ruby:r22377 (trunk): * ext/etc/etc.c (Etc::Passwd.each, Etc::Group.each): new methods.

nobu	2009-02-18 00:55:49 +0900 (Wed, 18 Feb 2009)

  New Revision: 22377

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22377

  Log:
    * ext/etc/etc.c (Etc::Passwd.each, Etc::Group.each): new methods.
      [ruby-dev:37999]

  Modified files:
    trunk/ChangeLog
    trunk/ext/etc/etc.c
    trunk/version.h

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 22376)
+++ ChangeLog	(revision 22377)
@@ -1,3 +1,8 @@
+Wed Feb 18 00:55:47 2009  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/etc/etc.c (Etc::Passwd.each, Etc::Group.each): new methods.
+	  [ruby-dev:37999]
+
 Tue Feb 17 23:25:01 2009  Tanaka Akira  <akr@f...>
 
 	* ext/socket/ancdata.c (bsock_sendmsg_internal): make the padding
Index: ext/etc/etc.c
===================================================================
--- ext/etc/etc.c	(revision 22376)
+++ ext/etc/etc.c	(revision 22377)
@@ -186,6 +186,16 @@
     endpwent();
     return Qnil;
 }
+
+static void
+each_passwd(void)
+{
+    if (passwd_blocking) {
+	rb_raise(rb_eRuntimeError, "parallel passwd iteration");
+    }
+    passwd_blocking = Qtrue;
+    rb_ensure(passwd_iterate, 0, passwd_ensure, 0);
+}
 #endif
 
 /* Provides a convenient Ruby iterator which executes a block for each entry 
@@ -211,19 +221,43 @@
 
     rb_secure(4);
     if (rb_block_given_p()) {
-	if (passwd_blocking) {
-	    rb_raise(rb_eRuntimeError, "parallel passwd iteration");
-	}
-	passwd_blocking = Qtrue;
-	rb_ensure(passwd_iterate, 0, passwd_ensure, 0);
+	each_passwd();
     }
-    if (pw = getpwent()) {
+    else if (pw = getpwent()) {
 	return setup_passwd(pw);
     }
 #endif
     return Qnil;
 }
 
+/* Iterates for each entry in the /etc/passwd file if a block is given.
+ * If no block is given, returns the enumerator.
+ *
+ * The code block is passed an Struct::Passwd struct; see getpwent above for 
+ * details.
+ *
+ * Example:
+ *
+ *     require 'etc'
+ *
+ *     Etc::Passwd.each {|u|
+ *       puts u.name + " = " + u.gecos
+ *     }
+ *
+ *     Etc::Passwd.collect {|u| u.gecos}
+ *     Etc::Passwd.collect {|u| u.gecos}
+ *
+ */
+static VALUE
+etc_each_passwd(VALUE obj)
+{
+#ifdef HAVE_GETPWENT
+    RETURN_ENUMERATOR(obj, 0, 0);
+    each_passwd();
+#endif
+    return obj;
+}
+
 /* Resets the process of reading the /etc/passwd file, so that the next call
  * to getpwent will return the first entry again.
  */
@@ -390,6 +424,16 @@
     endgrent();
     return Qnil;
 }
+
+static void
+each_group(void)
+{
+    if (group_blocking) {
+	rb_raise(rb_eRuntimeError, "parallel group iteration");
+    }
+    group_blocking = Qtrue;
+    rb_ensure(group_iterate, 0, group_ensure, 0);
+}
 #endif
 
 /* Provides a convenient Ruby iterator which executes a block for each entry 
@@ -415,19 +459,43 @@
 
     rb_secure(4);
     if (rb_block_given_p()) {
-	if (group_blocking) {
-	    rb_raise(rb_eRuntimeError, "parallel group iteration");
-	}
-	group_blocking = Qtrue;
-	rb_ensure(group_iterate, 0, group_ensure, 0);
+	each_group();
     }
-    if (grp = getgrent()) {
+    else if (grp = getgrent()) {
 	return setup_group(grp);
     }
 #endif
     return Qnil;
 }
 
+/* Iterates for each entry in the /etc/group file if a block is given.
+ * If no block is given, returns the enumerator.
+ *
+ * The code block is passed an Struct::Group struct; see getpwent above for 
+ * details.
+ *
+ * Example:
+ *
+ *     require 'etc'
+ *
+ *     Etc::Group.each {|g|
+ *       puts g.name + ": " + g.mem.join(', ')
+ *     }
+ *
+ *     Etc::Group.collect {|g| g.name}
+ *     Etc::Group.select {|g| !g.mem.empty?}
+ *
+ */
+static VALUE
+etc_each_group(VALUE obj)
+{
+#ifdef HAVE_GETPWENT
+    RETURN_ENUMERATOR(obj, 0, 0);
+    each_group();
+#endif
+    return obj;
+}
+
 /* Resets the process of reading the /etc/group file, so that the next call
  * to getgrent will return the first entry again.
  */
@@ -538,6 +606,8 @@
 #endif
 				NULL);
     rb_define_const(mEtc, "Passwd", sPasswd);
+    rb_extend_object(sPasswd, rb_mEnumerable);
+    rb_define_singleton_method(sPasswd, "each", etc_each_passwd, 0);
 
 #ifdef HAVE_GETGRENT
     sGroup = rb_struct_define("Group", "name",
@@ -547,5 +617,7 @@
 			      "gid", "mem", NULL);
 
     rb_define_const(mEtc, "Group", sGroup);
+    rb_extend_object(sGroup, rb_mEnumerable);
+    rb_define_singleton_method(sGroup, "each", etc_each_group, 0);
 #endif
 }
Index: version.h
===================================================================
--- version.h	(revision 22376)
+++ version.h	(revision 22377)
@@ -1,5 +1,5 @@
 #define RUBY_VERSION "1.9.2"
-#define RUBY_RELEASE_DATE "2009-02-17"
+#define RUBY_RELEASE_DATE "2009-02-18"
 #define RUBY_PATCHLEVEL -1
 #define RUBY_BRANCH_NAME "trunk"
 
@@ -8,7 +8,7 @@
 #define RUBY_VERSION_TEENY 1
 #define RUBY_RELEASE_YEAR 2009
 #define RUBY_RELEASE_MONTH 2
-#define RUBY_RELEASE_DAY 17
+#define RUBY_RELEASE_DAY 18
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]