mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/socket/mkconstants.rb: make common prefix optional.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21269 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d68b60741c
commit
775d5d8f03
3 changed files with 46 additions and 16 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Fri Jan 2 15:30:57 2009 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* ext/socket/mkconstants.rb: make common prefix optional.
|
||||||
|
|
||||||
Fri Jan 2 14:59:52 2009 Tanaka Akira <akr@fsij.org>
|
Fri Jan 2 14:59:52 2009 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* ext/socket/mkconstants.rb: use hash for family_to_str to avoid
|
* ext/socket/mkconstants.rb: use hash for family_to_str to avoid
|
||||||
|
|
|
@ -73,25 +73,44 @@ def reverse_each_name(pat)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def each_names_with_len(pat)
|
def each_names_with_len(pat, prefix_optional=nil)
|
||||||
h = {}
|
h = {}
|
||||||
DEFS.each {|name, default_value|
|
DEFS.each {|name, default_value|
|
||||||
next if pat !~ name
|
next if pat !~ name
|
||||||
(h[name.length] ||= []) << name
|
(h[name.length] ||= []) << [name, name]
|
||||||
|
}
|
||||||
|
if prefix_optional
|
||||||
|
if Regexp === prefix_optional
|
||||||
|
prefix_pat = prefix_optional
|
||||||
|
else
|
||||||
|
prefix_pat = /\A#{Regexp.escape prefix_optional}/
|
||||||
|
end
|
||||||
|
DEFS.each {|const, default_value|
|
||||||
|
next if pat !~ const
|
||||||
|
next if prefix_pat !~ const
|
||||||
|
name = $'
|
||||||
|
(h[name.length] ||= []) << [name, const]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
hh = {}
|
||||||
|
h.each {|len, pairs|
|
||||||
|
pairs.each {|name, const|
|
||||||
|
raise "name crash: #{name}" if hh[name]
|
||||||
|
hh[name] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
h.keys.sort.each {|len|
|
h.keys.sort.each {|len|
|
||||||
yield h[len], len
|
yield h[len], len
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var, retp_var, pat)")
|
ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var, retp_var, pat, prefix_optional=nil)")
|
||||||
switch (<%=len_var%>) {
|
switch (<%=len_var%>) {
|
||||||
% each_names_with_len(pat) {|names, len|
|
% each_names_with_len(pat, prefix_optional) {|pairs, len|
|
||||||
case <%=len%>:
|
case <%=len%>:
|
||||||
% names.each {|name|
|
% pairs.each {|name, const|
|
||||||
#ifdef <%=name%>
|
#ifdef <%=const%>
|
||||||
% size = name.bytesize
|
if (memcmp(<%=str_var%>, <%=c_str name%>, <%=len%>) == 0) { *<%=retp_var%> = <%=const%>; return 0; }
|
||||||
if (memcmp(<%=str_var%>, <%=c_str name%>, <%=size%>) == 0) { *<%=retp_var%> = <%=name%>; return 0; }
|
|
||||||
#endif
|
#endif
|
||||||
% }
|
% }
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -146,49 +165,49 @@ init_constants(VALUE mConst)
|
||||||
static int
|
static int
|
||||||
family_to_int(char *str, int len, int *valp)
|
family_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\A[AP]F_/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\A[AP]F_/, "AF_") %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
socktype_to_int(char *str, int len, int *valp)
|
socktype_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\ASOCK_/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\ASOCK_/, "SOCK_") %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
level_to_int(char *str, int len, int *valp)
|
level_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\A(SOL_SOCKET\z|IPPROTO_)/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\A(SOL_SOCKET\z|IPPROTO_)/, /\A(SOL_|IPPROTO_)/) %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
so_optname_to_int(char *str, int len, int *valp)
|
so_optname_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\ASO_/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\ASO_/, "SO_") %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ip_optname_to_int(char *str, int len, int *valp)
|
ip_optname_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\AIP_/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\AIP_/, "IP_") %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ipv6_optname_to_int(char *str, int len, int *valp)
|
ipv6_optname_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\AIPV6_/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\AIPV6_/, "IPV6_") %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
tcp_optname_to_int(char *str, int len, int *valp)
|
tcp_optname_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\ATCP_/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\ATCP_/, "TCP_") %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
udp_optname_to_int(char *str, int len, int *valp)
|
udp_optname_to_int(char *str, int len, int *valp)
|
||||||
{
|
{
|
||||||
<%= gen_name_to_int("str", "len", "valp", /\AUDP_/) %>
|
<%= gen_name_to_int("str", "len", "valp", /\AUDP_/, "UDP_") %>
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
|
|
|
@ -16,12 +16,19 @@ class TestBasicSocket < Test::Unit::TestCase
|
||||||
inet_stream do |s|
|
inet_stream do |s|
|
||||||
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_TYPE)
|
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_TYPE)
|
||||||
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
||||||
|
|
||||||
n = s.getsockopt("SOL_SOCKET", "SO_TYPE")
|
n = s.getsockopt("SOL_SOCKET", "SO_TYPE")
|
||||||
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
||||||
|
|
||||||
n = s.getsockopt(:SOL_SOCKET, :SO_TYPE)
|
n = s.getsockopt(:SOL_SOCKET, :SO_TYPE)
|
||||||
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
||||||
|
|
||||||
|
n = s.getsockopt(:SOCKET, :TYPE)
|
||||||
|
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
||||||
|
|
||||||
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR)
|
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR)
|
||||||
assert_equal([0].pack("i"), n)
|
assert_equal([0].pack("i"), n)
|
||||||
|
|
||||||
val = Object.new
|
val = Object.new
|
||||||
class << val; self end.send(:define_method, :to_int) {
|
class << val; self end.send(:define_method, :to_int) {
|
||||||
s.close
|
s.close
|
||||||
|
|
Loading…
Add table
Reference in a new issue