summaryrefslogtreecommitdiff
path: root/ext/iconv
diff options
context:
space:
mode:
Diffstat (limited to 'ext/iconv')
-rw-r--r--ext/iconv/extconf.rb25
-rw-r--r--ext/iconv/iconv.c76
2 files changed, 91 insertions, 10 deletions
diff --git a/ext/iconv/extconf.rb b/ext/iconv/extconf.rb
index 9e6c266bc7..5e80188013 100644
--- a/ext/iconv/extconf.rb
+++ b/ext/iconv/extconf.rb
@@ -6,21 +6,26 @@ conf = File.exist?(File.join($srcdir, "config.charset"))
conf = with_config("config-charset", enable_config("config-charset", conf))
if have_header("iconv.h")
- if !try_compile("", "-Werror") or checking_for("const of iconv() 2nd argument") do
- !try_compile('
-#include <iconv.h>
-size_t
-test(iconv_t cd, char **inptr, size_t *inlen, char **outptr, size_t *outlen)
-{
- return iconv(cd, inptr, inlen, outptr, outlen);
-}
-', "-Werror")
+ have_library("iconv", "iconv") {|s| s.sub(/(?=\n\/\*top\*\/)/, "#include <iconv.h>")}
+ if checking_for("const of iconv() 2nd argument") do
+ create_tmpsrc(cpp_include("iconv.h") + "---> iconv(cd,0,0,0,0) <---")
+ src = xpopen(cpp_command("")) {|f|f.read}
+ if !(func = src[/^--->\s*(\w+).*\s*<---/, 1])
+ Logging::message "iconv function name not found"
+ false
+ elsif !(second = src[%r"\b#{func}\s*\(.*?,(.*?),.*?\)\s*;"m, 1])
+ Logging::message "prototype for #{func}() not found"
+ false
+ else
+ Logging::message $&+"\n"
+ /\bconst\b/ =~ second
+ end
end
$defs.push('-DICONV_INPTR_CAST=""')
else
$defs.push('-DICONV_INPTR_CAST="(char **)"')
end
- have_library("iconv", "iconv") {|s| s.sub(/(?=\n\/\*top\*\/)/, "#include <iconv.h>")}
+ have_func("iconvlist", "iconv.h")
if conf
prefix = '$(srcdir)'
prefix = $nmake ? "{#{prefix}}" : "#{prefix}/"
diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c
index bceb7ca692..3694f9b9ff 100644
--- a/ext/iconv/iconv.c
+++ b/ext/iconv/iconv.c
@@ -611,6 +611,15 @@ iconv_s_iconv
return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
}
+/*
+=begin
+--- Iconv.conv(to, from, str)
+ Shorthand for
+ Iconv.iconv(to, from, str).join
+ see ((<Iconv.iconv>))
+=end
+*/
+
static VALUE
iconv_s_conv
#ifdef HAVE_PROTOTYPES
@@ -630,6 +639,72 @@ iconv_s_conv
return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
}
+/*
+=begin
+--- Iconv.list {|*aliases| ... }
+ Iterates each alias sets.
+=end
+ */
+
+#ifdef HAVE_ICONVLIST
+struct iconv_name_list {
+ unsigned int namescount;
+ const char *const *names;
+};
+
+static VALUE
+list_iconv_i
+#ifdef HAVE_PROTOTYPES
+ (VALUE ptr)
+#else /* HAVE_PROTOTYPES */
+ (ptr)
+ VALUE ptr;
+#endif /* HAVE_PROTOTYPES */
+{
+ struct iconv_name_list *p = (struct iconv_name_list *)ptr;
+ unsigned int i, namescount = p->namescount;
+ const char *const *names = p->names;
+ VALUE ary = rb_ary_new2(namescount);
+
+ for (i = 0; i < namescount; i++) {
+ rb_ary_push(ary, rb_str_new2(names[i]));
+ }
+ return rb_yield(ary);
+}
+
+static int
+list_iconv
+#ifdef HAVE_PROTOTYPES
+ (unsigned int namescount, const char *const *names, void *data)
+#else /* HAVE_PROTOTYPES */
+ (namescount, names, data)
+ unsigned int namescount;
+ const char *const *names;
+ void *data;
+#endif /* HAVE_PROTOTYPES */
+{
+ int *state = data;
+ struct iconv_name_list list;
+
+ list.namescount = namescount;
+ list.names = names;
+ rb_protect(list_iconv_i, (VALUE)&list, state);
+ return *state;
+}
+#endif
+
+static VALUE
+iconv_s_list _((void))
+{
+#ifdef HAVE_ICONVLIST
+ int state = 0;
+ iconvlist(list_iconv, &state);
+#else
+ rb_notimplement();
+#endif
+ return Qnil;
+}
+
/*
=begin
@@ -843,6 +918,7 @@ Init_iconv _((void))
rb_define_singleton_method(rb_cIconv, "open", iconv_s_open, 2);
rb_define_singleton_method(rb_cIconv, "iconv", iconv_s_iconv, -1);
rb_define_singleton_method(rb_cIconv, "conv", iconv_s_conv, 3);
+ rb_define_singleton_method(rb_cIconv, "list", iconv_s_list, 0);
rb_define_method(rb_cIconv, "initialize", iconv_initialize, 2);
rb_define_method(rb_cIconv, "close", iconv_finish, 0);
rb_define_method(rb_cIconv, "iconv", iconv_iconv, -1);