1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/socket/mkconstants.rb: generate family_to_str.

* ext/socket/socket.c (ipaddr): use family_to_str.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-01-01 10:37:41 +00:00
parent 9787d4faae
commit 175561f8dd
3 changed files with 56 additions and 23 deletions

View file

@ -1,3 +1,9 @@
Thu Jan 1 19:36:57 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/mkconstants.rb: generate family_to_str.
* ext/socket/socket.c (ipaddr): use family_to_str.
Thu Jan 1 17:37:12 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/mkconstants.rb: generate socktype_to_int.

View file

@ -93,6 +93,43 @@ ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var,
}
EOS
MAYBE_ALIAS = [
["AF_UNIX", "AF_LOCAL"]
]
def each_alias(pat)
h = {}
each_name(pat) {|name|
h[name] = [name]
}
MAYBE_ALIAS.each {|names|
a = []
names.each {|n|
a << n if h.delete n
}
h[a.first] = a
}
h.each_value {|names|
yield names
}
end
ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name(int_var, pat)")
switch (<%=int_var%>) {
% each_alias(pat) {|names|
% names.each_with_index {|n, i|
% cond = ["defined(#{n})"]
% (0...i).each {|j| cond << "(!defined(#{names[j]}) || #{n} != #{names[j]})" }
#if <%=cond.join(" && ")%>
case <%=n%>: return <%=c_str n%>;
#endif
% }
% }
default:
return NULL;
}
EOS
result << ERB.new(<<'EOS', nil, '%').result(binding)
static void
init_constants(VALUE mConst)
@ -127,6 +164,12 @@ socktype_to_int(char *str, int len)
<%= gen_name_to_int("str", "len", /\ASOCK_/) %>
}
static char *
family_to_str(int val)
{
<%= gen_int_to_name("val", /\AAF_/) %>
}
EOS
if opt_o

View file

@ -981,6 +981,8 @@ sock_addrinfo(VALUE host, VALUE port, int socktype, int flags)
return sock_getaddrinfo(host, port, &hints);
}
static char *family_to_str(int val);
static VALUE
ipaddr(struct sockaddr *sockaddr, int norevlookup)
{
@ -988,32 +990,14 @@ ipaddr(struct sockaddr *sockaddr, int norevlookup)
VALUE ary;
int error;
char hbuf[1024], pbuf[1024];
char *name;
switch (sockaddr->sa_family) {
case AF_UNSPEC:
family = rb_str_new2("AF_UNSPEC");
break;
case AF_INET:
family = rb_str_new2("AF_INET");
break;
#ifdef INET6
case AF_INET6:
family = rb_str_new2("AF_INET6");
break;
#endif
#ifdef AF_LOCAL
case AF_LOCAL:
family = rb_str_new2("AF_LOCAL");
break;
#elif AF_UNIX
case AF_UNIX:
family = rb_str_new2("AF_UNIX");
break;
#endif
default:
name = family_to_str(sockaddr->sa_family);
if (name)
family = rb_str_new2(name);
else {
sprintf(pbuf, "unknown:%d", sockaddr->sa_family);
family = rb_str_new2(pbuf);
break;
}
addr1 = Qnil;