diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-01 08:38:14 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-01 08:38:14 +0000 |
commit | 9787d4faaeff4fff798fff6803758f467cb72e7e (patch) | |
tree | 745475bca7b2cc50145b76fcc418031567a010d8 /ext/socket | |
parent | 009ad25fdd60c3d3067f881dbce706735bb5b6fd (diff) | |
download | ruby-9787d4faaeff4fff798fff6803758f467cb72e7e.tar.gz |
* ext/socket/mkconstants.rb: generate socktype_to_int.
* ext/socket/socket.c (setup_domain_and_type): use socktype_to_int.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21243 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket')
-rw-r--r-- | ext/socket/mkconstants.rb | 38 | ||||
-rw-r--r-- | ext/socket/socket.c | 26 |
2 files changed, 29 insertions, 35 deletions
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb index e11c9c344a..6c6be0f6e2 100644 --- a/ext/socket/mkconstants.rb +++ b/ext/socket/mkconstants.rb @@ -75,6 +75,24 @@ def each_names_with_len(pat) } end +ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var, pat)") + switch (<%=len_var%>) { +% each_names_with_len(pat) {|names, len| + case <%=len%>: +% names.each {|name| +#ifdef <%=name%> +% size = name.bytesize + if (memcmp(<%=str_var%>, <%=c_str name%>, <%=size%>) == 0) return <%=name%>; +#endif +% } + return -1; + +% } + default: + return -1; + } +EOS + result << ERB.new(<<'EOS', nil, '%').result(binding) static void init_constants(VALUE mConst) @@ -100,21 +118,13 @@ init_constants(VALUE mConst) static int family_to_int(char *str, int len) { - switch (len) { -% each_names_with_len(/\A[AP]F_/) {|names, len| - case <%=len%>: -% names.each {|name| -#ifdef <%=name%> -% size = name.bytesize - if (memcmp(str, <%=c_str name%>, <%=size%>) == 0) return <%=name%>; -#endif -% } - return -1; +<%= gen_name_to_int("str", "len", /\A[AP]F_/) %> +} -% } - default: - return -1; - } +static int +socktype_to_int(char *str, int len) +{ +<%= gen_name_to_int("str", "len", /\ASOCK_/) %> } EOS diff --git a/ext/socket/socket.c b/ext/socket/socket.c index 4bf7fc0217..61f72e7ea6 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -2281,6 +2281,7 @@ unix_peeraddr(VALUE sock) #endif static int family_to_int(char *str, int len); +static int socktype_to_int(char *str, int len); static void setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv) @@ -2304,31 +2305,14 @@ setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv) } tmp = rb_check_string_type(type); if (!NIL_P(tmp)) { + int socktype; type = tmp; rb_check_safe_obj(type); ptr = RSTRING_PTR(type); - if (strcmp(ptr, "SOCK_STREAM") == 0) - *tv = SOCK_STREAM; - else if (strcmp(ptr, "SOCK_DGRAM") == 0) - *tv = SOCK_DGRAM; -#ifdef SOCK_RAW - else if (strcmp(ptr, "SOCK_RAW") == 0) - *tv = SOCK_RAW; -#endif -#ifdef SOCK_SEQPACKET - else if (strcmp(ptr, "SOCK_SEQPACKET") == 0) - *tv = SOCK_SEQPACKET; -#endif -#ifdef SOCK_RDM - else if (strcmp(ptr, "SOCK_RDM") == 0) - *tv = SOCK_RDM; -#endif -#ifdef SOCK_PACKET - else if (strcmp(ptr, "SOCK_PACKET") == 0) - *tv = SOCK_PACKET; -#endif - else + socktype = socktype_to_int(ptr, RSTRING_LEN(type)); + if (socktype == -1) rb_raise(rb_eSocket, "unknown socket type %s", ptr); + *tv = socktype; } else { *tv = NUM2INT(type); |