summaryrefslogtreecommitdiff
path: root/ext/socket
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-02 08:02:36 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-02 08:02:36 +0000
commit9ce8d4e3b964774294c88d9414fdf4a92c352709 (patch)
treeb08440ab56bef856106fc436700269e3c07016be /ext/socket
parent775d5d8f0368afd3d74f0446928340c693ac028b (diff)
* ext/socket/mkconstants.rb: refactored to gather stringizer
description. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21270 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket')
-rw-r--r--ext/socket/mkconstants.rb47
1 files changed, 37 insertions, 10 deletions
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 574ad25b71..a90171277e 100644
--- a/ext/socket/mkconstants.rb
+++ b/ext/socket/mkconstants.rb
@@ -121,25 +121,56 @@ ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var,
}
EOS
-ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name_hash(hash_var, pat)")
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name_hash(hash_var, pat, prefix_pat)")
<%=hash_var%> = st_init_numtable();
% reverse_each_name(pat) {|n|
#ifdef <%=n%>
st_insert(<%=hash_var%>, (st_data_t)<%=n%>, (st_data_t)<%=c_str n%>);
#endif
% }
+% if prefix_pat
+% reverse_each_name(pat) {|n|
+% next if prefix_pat !~ n
+#ifdef <%=n%>
+ st_insert(<%=hash_var%>, (st_data_t)<%=n%>, (st_data_t)<%=c_str $'%>);
+#endif
+% }
+% end
EOS
-ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name(int_var, hash_var)")
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name_func(func_name, hash_var)")
+static char *
+<%=func_name%>(int val)
+{
st_data_t name;
- if (st_lookup(<%=hash_var%>, (st_data_t)<%=int_var%>, &name))
+ if (st_lookup(<%=hash_var%>, (st_data_t)val, &name))
return (char*)name;
return NULL;
+}
EOS
+STRINGIZER_DEFS = []
+def def_stringizer(func_name, pat, prefix_optional=nil)
+ prefix_pat = nil
+ if prefix_optional
+ if Regexp === prefix_optional
+ prefix_pat = prefix_optional
+ else
+ prefix_pat = /\A#{Regexp.escape prefix_optional}/
+ end
+ end
+ hash_var = "#{func_name}_hash"
+ decl = "static st_table *#{hash_var};"
+ gen_hash = gen_int_to_name_hash(hash_var, pat, prefix_pat)
+ func = gen_int_to_name_func(func_name, hash_var)
+ STRINGIZER_DEFS << [decl, gen_hash, func]
+end
+
+def_stringizer('family_to_str', /\AAF_/)
+
result << ERB.new(<<'EOS', nil, '%').result(binding)
-static st_table *family_to_str_hash;
+<%= STRINGIZER_DEFS.map {|decl, gen_hash, func| decl }.join("\n") %>
static void
init_constants(VALUE mConst)
@@ -159,7 +190,7 @@ init_constants(VALUE mConst)
#endif
% end
% }
-<%= gen_int_to_name_hash('family_to_str_hash', /\AAF_/) %>
+<%= STRINGIZER_DEFS.map {|decl, gen_hash, func| gen_hash }.join("\n") %>
}
static int
@@ -210,11 +241,7 @@ udp_optname_to_int(char *str, int len, int *valp)
<%= gen_name_to_int("str", "len", "valp", /\AUDP_/, "UDP_") %>
}
-static char *
-family_to_str(int val)
-{
-<%= gen_int_to_name("val", "family_to_str_hash") %>
-}
+<%= STRINGIZER_DEFS.map {|decl, gen_hash, func| func }.join("\n") %>
EOS