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

ruby-changes:9699

From: akr <ko1@a...>
Date: Thu, 1 Jan 2009 16:49:50 +0900 (JST)
Subject: [ruby-changes:9699] Ruby:r21240 (trunk): * ext/socket/mkconstants.rb: generate family_to_int().

akr	2009-01-01 16:49:31 +0900 (Thu, 01 Jan 2009)

  New Revision: 21240

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

  Log:
    * ext/socket/mkconstants.rb: generate family_to_int().
    * ext/socket/socket.c (setup_domain_and_type): use family_to_int.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/mkconstants.rb
    trunk/ext/socket/socket.c
    trunk/test/socket/test_socket.rb
    trunk/test/socket/test_unix.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 21239)
+++ ChangeLog	(revision 21240)
@@ -1,3 +1,9 @@
+Thu Jan  1 16:48:07 2009  Tanaka Akira  <akr@f...>
+
+	* ext/socket/mkconstants.rb: generate family_to_int().
+
+	* ext/socket/socket.c (setup_domain_and_type): use family_to_int.
+
 Thu Jan  1 15:08:46 2009  Yuki Sonoda (Yugui)  <yugui@y...>
 
 	* tool/flie2lastrev.rb: supports git repositories which are cloned
Index: ext/socket/mkconstants.rb
===================================================================
--- ext/socket/mkconstants.rb	(revision 21239)
+++ ext/socket/mkconstants.rb	(revision 21240)
@@ -34,10 +34,15 @@
 # workaround for NetBSD, OpenBSD and etc.
 result << "#define pseudo_AF_FTIP pseudo_AF_RTIP\n"
 
-def each_data
-  DATA.each_line {|s|
-    name, default_value = s.scan(/\S+/)
-    next unless name && name[0] != ?#
+DEFS = []
+DATA.each_line {|s|
+  name, default_value = s.scan(/\S+/)
+  next unless name && name[0] != ?#
+  DEFS << [name, default_value]
+}
+
+def each_const
+  DEFS.each {|name, default_value|
     if name =~ /\AINADDR_/
       define = "sock_define_uconst"
     else
@@ -52,11 +57,18 @@
   }
 end
 
+def each_name(pat)
+  DEFS.each {|name, default_value|
+    next if pat !~ name
+    yield name
+  }
+end
+
 result << ERB.new(<<'EOS', nil, '%').result(binding)
 static void
 init_constants(VALUE mConst)
 {
-% each_data {|guard, define, name, default_value|
+% each_const {|guard, define, name, default_value|
 %   if guard
 #if <%=guard%>
 %   end
@@ -73,6 +85,19 @@
 
 % }
 }
+
+static int
+family_to_int(char *str, int len)
+{
+% each_name(/\A[AP]F_/) {|name|
+#ifdef <%=name%>
+%   size = name.bytesize
+    if (len == <%=size%> && memcmp(str, <%=c_str name%>, <%=size%>) == 0) return <%=name%>;
+#endif
+% }
+    return -1;
+}
+
 EOS
 
 if opt_o
Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c	(revision 21239)
+++ ext/socket/socket.c	(revision 21240)
@@ -2280,6 +2280,8 @@
 }
 #endif
 
+static int family_to_int(char *str, int len);
+
 static void
 setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv)
 {
@@ -2288,51 +2290,13 @@
 
     tmp = rb_check_string_type(domain);
     if (!NIL_P(tmp)) {
+        int family;
 	domain = tmp;
 	rb_check_safe_obj(domain);
-	ptr = RSTRING_PTR(domain);
-	if (strcmp(ptr, "AF_INET") == 0)
-	    *dv = AF_INET;
-#ifdef AF_UNIX
-	else if (strcmp(ptr, "AF_UNIX") == 0)
-	    *dv = AF_UNIX;
-#endif
-#ifdef AF_ISO
-	else if (strcmp(ptr, "AF_ISO") == 0)
-	    *dv = AF_ISO;
-#endif
-#ifdef AF_NS
-	else if (strcmp(ptr, "AF_NS") == 0)
-	    *dv = AF_NS;
-#endif
-#ifdef AF_IMPLINK
-	else if (strcmp(ptr, "AF_IMPLINK") == 0)
-	    *dv = AF_IMPLINK;
-#endif
-#ifdef PF_INET
-	else if (strcmp(ptr, "PF_INET") == 0)
-	    *dv = PF_INET;
-#endif
-#ifdef PF_UNIX
-	else if (strcmp(ptr, "PF_UNIX") == 0)
-	    *dv = PF_UNIX;
-#endif
-#ifdef PF_IMPLINK
-	else if (strcmp(ptr, "PF_IMPLINK") == 0)
-	    *dv = PF_IMPLINK;
-	else if (strcmp(ptr, "AF_IMPLINK") == 0)
-	    *dv = AF_IMPLINK;
-#endif
-#ifdef PF_AX25
-	else if (strcmp(ptr, "PF_AX25") == 0)
-	    *dv = PF_AX25;
-#endif
-#ifdef PF_IPX
-	else if (strcmp(ptr, "PF_IPX") == 0)
-	    *dv = PF_IPX;
-#endif
-	else
+        family = family_to_int(RSTRING_PTR(domain), RSTRING_LEN(domain));
+        if (family == -1)
 	    rb_raise(rb_eSocket, "unknown socket domain %s", ptr);
+        *dv = family;
     }
     else {
 	*dv = NUM2INT(domain);
Index: test/socket/test_unix.rb
===================================================================
--- test/socket/test_unix.rb	(revision 21239)
+++ test/socket/test_unix.rb	(revision 21240)
@@ -166,4 +166,18 @@
     assert_kind_of(UNIXSocket, pair[1])
   end
 
+  def test_initialize
+    Socket.open(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) {|s|
+      addr = s.getsockname
+      assert_nothing_raised { Socket.unpack_sockaddr_un(addr) }
+      assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(addr) }
+    }
+    Socket.open("AF_UNIX", "SOCK_STREAM", 0) {|s|
+      addr = s.getsockname
+      assert_nothing_raised { Socket.unpack_sockaddr_un(addr) }
+      assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(addr) }
+    }
+  end
+
+
 end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM
Index: test/socket/test_socket.rb
===================================================================
--- test/socket/test_socket.rb	(revision 21239)
+++ test/socket/test_socket.rb	(revision 21240)
@@ -97,4 +97,17 @@
     c.close if c
     IO.for_fd(fd).close if fd
   end
+
+  def test_initialize
+    Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|s|
+      addr = s.getsockname
+      assert_nothing_raised { Socket.unpack_sockaddr_in(addr) }
+      assert_raise(ArgumentError) { Socket.unpack_sockaddr_un(addr) }
+    }
+    Socket.open("AF_INET", "SOCK_STREAM", 0) {|s|
+      addr = s.getsockname
+      assert_nothing_raised { Socket.unpack_sockaddr_in(addr) }
+      assert_raise(ArgumentError) { Socket.unpack_sockaddr_un(addr) }
+    }
+  end
 end if defined?(Socket)

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

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