2002-03-14 08:10:57 -05:00
|
|
|
/* -*- mode:c; c-file-style:"ruby" -*- */
|
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
iconv.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Wed Dec 1 20:28:09 JST 1999
|
|
|
|
|
|
|
|
All the files in this distribution are covered under the Ruby's
|
|
|
|
license (see the file COPYING).
|
|
|
|
|
2005-03-04 05:40:09 -05:00
|
|
|
Documentation by Yukihiro Matsumoto and Gavin Sinclair.
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2005-03-04 05:40:09 -05:00
|
|
|
**********************************************************************/
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2007-06-09 23:06:15 -04:00
|
|
|
#include "ruby/ruby.h"
|
2002-03-14 08:10:57 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <assert.h>
|
2007-06-09 23:06:15 -04:00
|
|
|
#include "ruby/st.h"
|
2007-12-04 02:34:49 -05:00
|
|
|
#include "ruby/encoding.h"
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2005-03-04 05:40:09 -05:00
|
|
|
/*
|
|
|
|
* Document-class: Iconv
|
|
|
|
*
|
|
|
|
* == Summary
|
|
|
|
*
|
|
|
|
* Ruby extension for charset conversion.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* == Abstract
|
|
|
|
*
|
|
|
|
* Iconv is a wrapper class for the UNIX 95 <tt>iconv()</tt> function family,
|
|
|
|
* which translates string between various encoding systems.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* See Open Group's on-line documents for more details.
|
|
|
|
* * <tt>iconv.h</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html
|
|
|
|
* * <tt>iconv_open()</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv_open.html
|
|
|
|
* * <tt>iconv()</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.html
|
|
|
|
* * <tt>iconv_close()</tt>: http://www.opengroup.org/onlinepubs/007908799/xsh/iconv_close.html
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Which coding systems are available is platform-dependent.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* == Examples
|
|
|
|
*
|
2007-11-16 03:03:36 -05:00
|
|
|
* 1. Simple conversion between two charsets.
|
|
|
|
*
|
|
|
|
* converted_text = Iconv.conv('iso-8859-15', 'utf-8', text)
|
|
|
|
*
|
|
|
|
* 2. Instantiate a new Iconv and use method Iconv#iconv.
|
2005-03-04 05:40:09 -05:00
|
|
|
*
|
|
|
|
* cd = Iconv.new(to, from)
|
|
|
|
* begin
|
|
|
|
* input.each { |s| output << cd.iconv(s) }
|
|
|
|
* output << cd.iconv(nil) # Don't forget this!
|
|
|
|
* ensure
|
|
|
|
* cd.close
|
|
|
|
* end
|
|
|
|
*
|
2007-11-16 03:03:36 -05:00
|
|
|
* 3. Invoke Iconv.open with a block.
|
2005-03-04 05:40:09 -05:00
|
|
|
*
|
|
|
|
* Iconv.open(to, from) do |cd|
|
|
|
|
* input.each { |s| output << cd.iconv(s) }
|
|
|
|
* output << cd.iconv(nil)
|
|
|
|
* end
|
|
|
|
*
|
2007-11-16 03:03:36 -05:00
|
|
|
* 4. Shorthand for (3).
|
2005-03-04 05:40:09 -05:00
|
|
|
*
|
|
|
|
* Iconv.iconv(to, from, *input.to_a)
|
2008-09-11 06:10:16 -04:00
|
|
|
*
|
|
|
|
* == Attentions
|
|
|
|
*
|
|
|
|
* Even if some extentions of implementation dependent are useful,
|
|
|
|
* DON'T USE those extentions in libraries and scripts to widely distribute.
|
|
|
|
* If you want to use those feature, use String#encode.
|
2005-03-04 05:40:09 -05:00
|
|
|
*/
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
/* Invalid value for iconv_t is -1 but 0 for VALUE, I hope VALUE is
|
|
|
|
big enough to keep iconv_t */
|
|
|
|
#define VALUE2ICONV(v) ((iconv_t)((VALUE)(v) ^ -1))
|
|
|
|
#define ICONV2VALUE(c) ((VALUE)(c) ^ -1)
|
|
|
|
|
|
|
|
struct iconv_env_t
|
|
|
|
{
|
|
|
|
iconv_t cd;
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ret;
|
2007-12-04 02:34:49 -05:00
|
|
|
int toidx;
|
2003-10-02 07:33:52 -04:00
|
|
|
VALUE (*append)_((VALUE, VALUE));
|
2002-03-14 08:10:57 -05:00
|
|
|
};
|
|
|
|
|
2005-11-07 06:55:48 -05:00
|
|
|
struct rb_iconv_opt_t
|
|
|
|
{
|
|
|
|
VALUE transliterate;
|
|
|
|
VALUE discard_ilseq;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ID id_transliterate, id_discard_ilseq;
|
|
|
|
|
2004-01-18 09:59:49 -05:00
|
|
|
static VALUE rb_eIconvInvalidEncoding;
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE rb_eIconvFailure;
|
|
|
|
static VALUE rb_eIconvIllegalSeq;
|
|
|
|
static VALUE rb_eIconvInvalidChar;
|
|
|
|
static VALUE rb_eIconvOutOfRange;
|
2005-11-06 09:41:53 -05:00
|
|
|
static VALUE rb_eIconvBrokenLibrary;
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2003-10-02 07:33:52 -04:00
|
|
|
static ID rb_success, rb_failed;
|
|
|
|
static VALUE iconv_fail _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg));
|
2009-10-15 01:21:00 -04:00
|
|
|
static VALUE iconv_fail_retry _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg));
|
2003-10-02 07:33:52 -04:00
|
|
|
static VALUE iconv_failure_initialize _((VALUE error, VALUE mesg, VALUE success, VALUE failed));
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE iconv_failure_success _((VALUE self));
|
|
|
|
static VALUE iconv_failure_failed _((VALUE self));
|
|
|
|
|
2007-12-04 02:34:49 -05:00
|
|
|
static iconv_t iconv_create _((VALUE to, VALUE from, struct rb_iconv_opt_t *opt, int *idx));
|
2003-07-18 15:51:42 -04:00
|
|
|
static void iconv_dfree _((void *cd));
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE iconv_free _((VALUE cd));
|
|
|
|
static VALUE iconv_try _((iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen));
|
2010-01-17 00:24:36 -05:00
|
|
|
static VALUE rb_str_derive _((VALUE str, const char* ptr, long len));
|
2008-06-04 04:52:25 -04:00
|
|
|
static VALUE iconv_convert _((iconv_t cd, VALUE str, long start, long length, int toidx,
|
2008-02-08 05:19:57 -05:00
|
|
|
struct iconv_env_t* env));
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE iconv_s_allocate _((VALUE klass));
|
2005-11-07 06:55:48 -05:00
|
|
|
static VALUE iconv_initialize _((int argc, VALUE *argv, VALUE self));
|
|
|
|
static VALUE iconv_s_open _((int argc, VALUE *argv, VALUE self));
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE iconv_s_convert _((struct iconv_env_t* env));
|
|
|
|
static VALUE iconv_s_iconv _((int argc, VALUE *argv, VALUE self));
|
|
|
|
static VALUE iconv_init_state _((VALUE cd));
|
|
|
|
static VALUE iconv_finish _((VALUE self));
|
|
|
|
static VALUE iconv_iconv _((int argc, VALUE *argv, VALUE self));
|
2005-11-07 06:55:48 -05:00
|
|
|
static VALUE iconv_conv _((int argc, VALUE *argv, VALUE self));
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2003-07-18 15:51:42 -04:00
|
|
|
static VALUE charset_map;
|
|
|
|
|
2005-03-15 09:50:08 -05:00
|
|
|
/*
|
|
|
|
* Document-method: charset_map
|
|
|
|
* call-seq: Iconv.charset_map
|
|
|
|
*
|
|
|
|
* Returns the map from canonical name to system dependent name.
|
|
|
|
*/
|
2008-02-08 05:19:57 -05:00
|
|
|
static VALUE
|
|
|
|
charset_map_get(void)
|
2003-07-18 15:51:42 -04:00
|
|
|
{
|
|
|
|
return charset_map;
|
|
|
|
}
|
|
|
|
|
2008-09-05 05:16:34 -04:00
|
|
|
static VALUE
|
|
|
|
strip_glibc_option(VALUE *code)
|
|
|
|
{
|
2008-10-20 22:34:07 -04:00
|
|
|
VALUE val = StringValue(*code);
|
2008-09-05 05:16:34 -04:00
|
|
|
const char *ptr = RSTRING_PTR(val), *pend = RSTRING_END(val);
|
|
|
|
const char *slash = memchr(ptr, '/', pend - ptr);
|
2008-10-20 22:34:07 -04:00
|
|
|
|
2008-09-05 05:16:34 -04:00
|
|
|
if (slash && slash < pend - 1 && slash[1] == '/') {
|
|
|
|
VALUE opt = rb_str_subseq(val, slash - ptr, pend - slash);
|
|
|
|
val = rb_str_subseq(val, 0, slash - ptr);
|
|
|
|
*code = val;
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-07-18 15:51:42 -04:00
|
|
|
static char *
|
2005-09-09 03:45:36 -04:00
|
|
|
map_charset(VALUE *code)
|
2003-07-18 15:51:42 -04:00
|
|
|
{
|
2008-10-20 22:34:07 -04:00
|
|
|
VALUE val = StringValue(*code);
|
2003-07-18 15:51:42 -04:00
|
|
|
|
2007-08-29 19:12:21 -04:00
|
|
|
if (RHASH_SIZE(charset_map)) {
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 10:47:23 -04:00
|
|
|
st_data_t data;
|
2003-12-07 06:39:29 -05:00
|
|
|
VALUE key = rb_funcall2(val, rb_intern("downcase"), 0, 0);
|
|
|
|
StringValuePtr(key);
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 10:47:23 -04:00
|
|
|
if (st_lookup(RHASH_TBL(charset_map), key, &data)) {
|
|
|
|
*code = (VALUE)data;
|
2003-07-18 15:51:42 -04:00
|
|
|
}
|
|
|
|
}
|
2003-12-22 07:59:28 -05:00
|
|
|
return StringValuePtr(*code);
|
2003-07-18 15:51:42 -04:00
|
|
|
}
|
|
|
|
|
2010-05-15 04:16:18 -04:00
|
|
|
NORETURN(static void rb_iconv_sys_fail(const char *s));
|
|
|
|
static void
|
|
|
|
rb_iconv_sys_fail(const char *s)
|
|
|
|
{
|
|
|
|
if (errno == 0) {
|
2010-05-15 21:49:49 -04:00
|
|
|
rb_exc_raise(iconv_fail(rb_eIconvBrokenLibrary, Qnil, Qnil, NULL, s));
|
2010-05-15 04:16:18 -04:00
|
|
|
}
|
|
|
|
rb_sys_fail(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define rb_sys_fail(s) rb_iconv_sys_fail(s)
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
static iconv_t
|
2007-12-04 02:34:49 -05:00
|
|
|
iconv_create(VALUE to, VALUE from, struct rb_iconv_opt_t *opt, int *idx)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2008-09-05 05:16:34 -04:00
|
|
|
VALUE toopt = strip_glibc_option(&to);
|
|
|
|
VALUE fromopt = strip_glibc_option(&from);
|
|
|
|
VALUE toenc = 0, fromenc = 0;
|
2003-07-18 15:51:42 -04:00
|
|
|
const char* tocode = map_charset(&to);
|
|
|
|
const char* fromcode = map_charset(&from);
|
2007-12-04 02:34:49 -05:00
|
|
|
iconv_t cd;
|
2008-09-05 05:16:34 -04:00
|
|
|
int retry = 0;
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2008-09-05 05:16:34 -04:00
|
|
|
*idx = rb_enc_find_index(tocode);
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2008-09-05 05:16:34 -04:00
|
|
|
if (toopt) {
|
|
|
|
toenc = rb_str_plus(to, toopt);
|
|
|
|
tocode = RSTRING_PTR(toenc);
|
|
|
|
}
|
|
|
|
if (fromopt) {
|
|
|
|
fromenc = rb_str_plus(from, fromopt);
|
|
|
|
fromcode = RSTRING_PTR(fromenc);
|
|
|
|
}
|
|
|
|
while ((cd = iconv_open(tocode, fromcode)) == (iconv_t)-1) {
|
|
|
|
int inval = 0;
|
2002-03-14 08:10:57 -05:00
|
|
|
switch (errno) {
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
|
|
|
case ENOMEM:
|
2008-09-05 05:16:34 -04:00
|
|
|
if (!retry++) {
|
|
|
|
rb_gc();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EINVAL:
|
|
|
|
retry = 0;
|
|
|
|
inval = 1;
|
|
|
|
if (toenc) {
|
|
|
|
tocode = RSTRING_PTR(to);
|
|
|
|
rb_str_resize(toenc, 0);
|
|
|
|
toenc = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fromenc) {
|
|
|
|
fromcode = RSTRING_PTR(from);
|
|
|
|
rb_str_resize(fromenc, 0);
|
|
|
|
fromenc = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
2008-09-05 05:16:34 -04:00
|
|
|
{
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
const char *s = inval ? "invalid encoding " : "iconv";
|
2006-08-31 06:30:33 -04:00
|
|
|
volatile VALUE msg = rb_str_new(0, strlen(s) + RSTRING_LEN(to) +
|
|
|
|
RSTRING_LEN(from) + 8);
|
2005-03-27 18:40:32 -05:00
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
sprintf(RSTRING_PTR(msg), "%s(\"%s\", \"%s\")",
|
|
|
|
s, RSTRING_PTR(to), RSTRING_PTR(from));
|
|
|
|
s = RSTRING_PTR(msg);
|
|
|
|
rb_str_set_len(msg, strlen(s));
|
2004-01-18 09:59:49 -05:00
|
|
|
if (!inval) rb_sys_fail(s);
|
2009-10-15 01:21:00 -04:00
|
|
|
rb_exc_raise(iconv_fail(rb_eIconvInvalidEncoding, Qnil,
|
|
|
|
rb_ary_new3(2, to, from), NULL, s));
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-05 05:16:34 -04:00
|
|
|
if (toopt || fromopt) {
|
|
|
|
if (toopt && fromopt && RTEST(rb_str_equal(toopt, fromopt))) {
|
|
|
|
fromopt = 0;
|
|
|
|
}
|
|
|
|
if (toopt && fromopt) {
|
|
|
|
rb_warning("encoding option isn't portable: %s, %s",
|
|
|
|
RSTRING_PTR(toopt) + 2, RSTRING_PTR(fromopt) + 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_warning("encoding option isn't portable: %s",
|
|
|
|
(toopt ? RSTRING_PTR(toopt) : RSTRING_PTR(fromopt)) + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-07 06:55:48 -05:00
|
|
|
if (opt) {
|
|
|
|
#ifdef ICONV_SET_TRANSLITERATE
|
|
|
|
if (opt->transliterate != Qundef) {
|
2007-12-04 02:34:49 -05:00
|
|
|
int flag = RTEST(opt->transliterate);
|
2008-09-05 05:16:34 -04:00
|
|
|
rb_warning("encoding option isn't portable: transliterate");
|
2005-11-07 06:55:48 -05:00
|
|
|
if (iconvctl(cd, ICONV_SET_TRANSLITERATE, (void *)&flag))
|
|
|
|
rb_sys_fail("ICONV_SET_TRANSLITERATE");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef ICONV_SET_DISCARD_ILSEQ
|
|
|
|
if (opt->discard_ilseq != Qundef) {
|
2007-12-04 02:34:49 -05:00
|
|
|
int flag = RTEST(opt->discard_ilseq);
|
2008-09-05 05:16:34 -04:00
|
|
|
rb_warning("encoding option isn't portable: discard_ilseq");
|
2005-11-07 06:55:48 -05:00
|
|
|
if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&flag))
|
|
|
|
rb_sys_fail("ICONV_SET_DISCARD_ILSEQ");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
return cd;
|
|
|
|
}
|
|
|
|
|
2003-07-18 15:51:42 -04:00
|
|
|
static void
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_dfree(void *cd)
|
2003-07-18 15:51:42 -04:00
|
|
|
{
|
|
|
|
iconv_close(VALUE2ICONV(cd));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ICONV_FREE iconv_dfree
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_free(VALUE cd)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
if (cd && iconv_close(VALUE2ICONV(cd)) == -1)
|
|
|
|
rb_sys_fail("iconv_close");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-07-22 21:11:20 -04:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
check_iconv(VALUE obj)
|
2003-07-22 21:11:20 -04:00
|
|
|
{
|
|
|
|
Check_Type(obj, T_DATA);
|
|
|
|
if (RDATA(obj)->dfree != ICONV_FREE) {
|
|
|
|
rb_raise(rb_eArgError, "Iconv expected (%s)", rb_class2name(CLASS_OF(obj)));
|
|
|
|
}
|
|
|
|
return (VALUE)DATA_PTR(obj);
|
|
|
|
}
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_try(iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2005-09-09 03:45:36 -04:00
|
|
|
#ifdef ICONV_INPTR_CONST
|
|
|
|
#define ICONV_INPTR_CAST
|
|
|
|
#else
|
|
|
|
#define ICONV_INPTR_CAST (char **)
|
|
|
|
#endif
|
2005-11-06 09:41:53 -05:00
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ret = iconv(cd, ICONV_INPTR_CAST inptr, inlen, outptr, outlen);
|
2003-10-02 01:19:18 -04:00
|
|
|
if (ret == (size_t)-1) {
|
2002-03-14 08:10:57 -05:00
|
|
|
if (!*inlen)
|
|
|
|
return Qfalse;
|
|
|
|
switch (errno) {
|
|
|
|
case E2BIG:
|
|
|
|
/* try the left in next loop */
|
|
|
|
break;
|
|
|
|
case EILSEQ:
|
2003-10-02 07:33:52 -04:00
|
|
|
return rb_eIconvIllegalSeq;
|
2002-03-14 08:10:57 -05:00
|
|
|
case EINVAL:
|
2003-10-02 07:33:52 -04:00
|
|
|
return rb_eIconvInvalidChar;
|
2005-11-06 09:41:53 -05:00
|
|
|
case 0:
|
|
|
|
return rb_eIconvBrokenLibrary;
|
2002-03-14 08:10:57 -05:00
|
|
|
default:
|
|
|
|
rb_sys_fail("iconv");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (*inlen > 0) {
|
|
|
|
/* something goes wrong */
|
2003-10-02 07:33:52 -04:00
|
|
|
return rb_eIconvIllegalSeq;
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
2003-10-02 01:19:18 -04:00
|
|
|
else if (ret) {
|
|
|
|
return Qnil; /* conversion */
|
|
|
|
}
|
2002-03-14 08:10:57 -05:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-07-22 21:11:20 -04:00
|
|
|
#define FAILED_MAXLEN 16
|
|
|
|
|
2008-02-08 05:19:57 -05:00
|
|
|
static VALUE
|
|
|
|
iconv_failure_initialize(VALUE error, VALUE mesg, VALUE success, VALUE failed)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2003-10-02 07:33:52 -04:00
|
|
|
rb_call_super(1, &mesg);
|
2002-03-14 08:10:57 -05:00
|
|
|
rb_ivar_set(error, rb_success, success);
|
|
|
|
rb_ivar_set(error, rb_failed, failed);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2003-09-01 11:41:31 -04:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_fail(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg)
|
2003-09-01 11:41:31 -04:00
|
|
|
{
|
2003-10-02 07:33:52 -04:00
|
|
|
VALUE args[3];
|
|
|
|
|
|
|
|
if (mesg && *mesg) {
|
|
|
|
args[0] = rb_str_new2(mesg);
|
|
|
|
}
|
2006-08-31 06:30:33 -04:00
|
|
|
else if (TYPE(failed) != T_STRING || RSTRING_LEN(failed) < FAILED_MAXLEN) {
|
2003-10-02 07:33:52 -04:00
|
|
|
args[0] = rb_inspect(failed);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
args[0] = rb_inspect(rb_str_substr(failed, 0, FAILED_MAXLEN));
|
|
|
|
rb_str_cat2(args[0], "...");
|
|
|
|
}
|
|
|
|
args[1] = success;
|
|
|
|
args[2] = failed;
|
|
|
|
if (env) {
|
|
|
|
args[1] = env->append(rb_obj_dup(env->ret), success);
|
|
|
|
if (env->argc > 0) {
|
|
|
|
*(env->argv) = failed;
|
|
|
|
args[2] = rb_ary_new4(env->argc, env->argv);
|
|
|
|
}
|
|
|
|
}
|
2009-10-15 01:21:00 -04:00
|
|
|
return rb_class_new_instance(3, args, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
iconv_fail_retry(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg)
|
|
|
|
{
|
|
|
|
error = iconv_fail(error, success, failed, env, mesg);
|
2003-09-01 11:41:31 -04:00
|
|
|
if (!rb_block_given_p()) rb_exc_raise(error);
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_set_errinfo(error);
|
2003-10-02 01:19:18 -04:00
|
|
|
return rb_yield(failed);
|
2003-09-01 11:41:31 -04:00
|
|
|
}
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2010-01-17 00:24:36 -05:00
|
|
|
rb_str_derive(VALUE str, const char* ptr, long len)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
VALUE ret;
|
|
|
|
|
|
|
|
if (NIL_P(str))
|
|
|
|
return rb_str_new(ptr, len);
|
2006-08-31 06:30:33 -04:00
|
|
|
if (RSTRING_PTR(str) + RSTRING_LEN(str) == ptr + len)
|
2008-02-08 05:22:15 -05:00
|
|
|
ret = rb_str_subseq(str, ptr - RSTRING_PTR(str), len);
|
2003-07-22 21:11:20 -04:00
|
|
|
else
|
|
|
|
ret = rb_str_new(ptr, len);
|
2002-03-14 08:10:57 -05:00
|
|
|
OBJ_INFECT(ret, str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-06-04 04:52:25 -04:00
|
|
|
iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct iconv_env_t* env)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
VALUE ret = Qfalse;
|
|
|
|
VALUE error = Qfalse;
|
2003-10-02 01:19:18 -04:00
|
|
|
VALUE rescue;
|
2002-03-14 08:10:57 -05:00
|
|
|
const char *inptr, *instart;
|
|
|
|
size_t inlen;
|
|
|
|
/* I believe ONE CHARACTER never exceed this. */
|
|
|
|
char buffer[BUFSIZ];
|
|
|
|
char *outptr;
|
|
|
|
size_t outlen;
|
|
|
|
|
|
|
|
if (cd == (iconv_t)-1)
|
|
|
|
rb_raise(rb_eArgError, "closed iconv");
|
|
|
|
|
|
|
|
if (NIL_P(str)) {
|
|
|
|
/* Reset output pointer or something. */
|
|
|
|
inptr = "";
|
|
|
|
inlen = 0;
|
|
|
|
outptr = buffer;
|
|
|
|
outlen = sizeof(buffer);
|
|
|
|
error = iconv_try(cd, &inptr, &inlen, &outptr, &outlen);
|
2003-10-02 01:19:18 -04:00
|
|
|
if (RTEST(error)) {
|
2003-09-01 11:41:31 -04:00
|
|
|
unsigned int i;
|
2009-10-15 01:21:00 -04:00
|
|
|
rescue = iconv_fail_retry(error, Qnil, Qnil, env, 0);
|
2003-10-02 01:19:18 -04:00
|
|
|
if (TYPE(rescue) == T_ARRAY) {
|
2006-09-02 10:42:08 -04:00
|
|
|
str = RARRAY_LEN(rescue) > 0 ? RARRAY_PTR(rescue)[0] : Qnil;
|
2003-10-02 01:19:18 -04:00
|
|
|
}
|
2003-09-01 11:41:31 -04:00
|
|
|
if (FIXNUM_P(str) && (i = FIX2INT(str)) <= 0xff) {
|
|
|
|
char c = i;
|
|
|
|
str = rb_str_new(&c, 1);
|
|
|
|
}
|
|
|
|
else if (!NIL_P(str)) {
|
|
|
|
StringValue(str);
|
|
|
|
}
|
|
|
|
}
|
2002-03-14 08:10:57 -05:00
|
|
|
|
|
|
|
inptr = NULL;
|
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
else {
|
2010-01-17 00:24:36 -05:00
|
|
|
long slen;
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2003-07-22 21:11:20 -04:00
|
|
|
StringValue(str);
|
2006-08-31 06:30:33 -04:00
|
|
|
slen = RSTRING_LEN(str);
|
|
|
|
inptr = RSTRING_PTR(str);
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2008-06-04 04:52:25 -04:00
|
|
|
inptr += start;
|
|
|
|
if (length < 0 || length > start + slen)
|
|
|
|
length = slen - start;
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
instart = inptr;
|
|
|
|
inlen = length;
|
|
|
|
|
|
|
|
do {
|
2003-10-02 07:33:52 -04:00
|
|
|
char errmsg[50];
|
2002-03-14 08:10:57 -05:00
|
|
|
const char *tmpstart = inptr;
|
|
|
|
outptr = buffer;
|
|
|
|
outlen = sizeof(buffer);
|
|
|
|
|
2003-10-02 07:33:52 -04:00
|
|
|
errmsg[0] = 0;
|
2002-03-14 08:10:57 -05:00
|
|
|
error = iconv_try(cd, &inptr, &inlen, &outptr, &outlen);
|
|
|
|
|
2010-01-17 00:23:25 -05:00
|
|
|
if (
|
|
|
|
#if SIGNEDNESS_OF_SIZE_T < 0
|
|
|
|
0 <= outlen &&
|
|
|
|
#endif
|
|
|
|
outlen <= sizeof(buffer)) {
|
2002-03-14 08:10:57 -05:00
|
|
|
outlen = sizeof(buffer) - outlen;
|
2003-10-02 01:19:18 -04:00
|
|
|
if (NIL_P(error) || /* something converted */
|
2009-10-16 00:36:31 -04:00
|
|
|
outlen > (size_t)(inptr - tmpstart) || /* input can't contain output */
|
|
|
|
(outlen < (size_t)(inptr - tmpstart) && inlen > 0) || /* something skipped */
|
2002-03-14 08:10:57 -05:00
|
|
|
memcmp(buffer, tmpstart, outlen)) /* something differs */
|
|
|
|
{
|
|
|
|
if (NIL_P(str)) {
|
|
|
|
ret = rb_str_new(buffer, outlen);
|
2007-12-04 05:56:44 -05:00
|
|
|
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ret) {
|
|
|
|
ret = rb_str_buf_cat(ret, instart, tmpstart - instart);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = rb_str_new(instart, tmpstart - instart);
|
2007-12-04 05:56:44 -05:00
|
|
|
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
|
2002-03-14 08:10:57 -05:00
|
|
|
OBJ_INFECT(ret, str);
|
|
|
|
}
|
|
|
|
ret = rb_str_buf_cat(ret, buffer, outlen);
|
|
|
|
instart = inptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!inlen) {
|
|
|
|
inptr = tmpstart + outlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Some iconv() have a bug, return *outlen out of range */
|
2003-12-10 21:39:59 -05:00
|
|
|
sprintf(errmsg, "bug?(output length = %ld)", (long)(sizeof(buffer) - outlen));
|
2003-10-02 07:33:52 -04:00
|
|
|
error = rb_eIconvOutOfRange;
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
2003-10-02 01:19:18 -04:00
|
|
|
if (RTEST(error)) {
|
|
|
|
long len = 0;
|
|
|
|
|
2007-12-04 02:34:49 -05:00
|
|
|
if (!ret) {
|
2002-03-14 08:10:57 -05:00
|
|
|
ret = rb_str_derive(str, instart, inptr - instart);
|
2007-12-04 05:56:44 -05:00
|
|
|
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
|
2007-12-04 02:34:49 -05:00
|
|
|
}
|
|
|
|
else if (inptr > instart) {
|
2003-10-02 07:33:52 -04:00
|
|
|
rb_str_cat(ret, instart, inptr - instart);
|
2007-12-04 02:34:49 -05:00
|
|
|
}
|
2002-03-14 08:10:57 -05:00
|
|
|
str = rb_str_derive(str, inptr, inlen);
|
2009-10-15 01:21:00 -04:00
|
|
|
rescue = iconv_fail_retry(error, ret, str, env, errmsg);
|
2003-10-02 01:19:18 -04:00
|
|
|
if (TYPE(rescue) == T_ARRAY) {
|
2006-09-02 10:42:08 -04:00
|
|
|
if ((len = RARRAY_LEN(rescue)) > 0)
|
|
|
|
rb_str_concat(ret, RARRAY_PTR(rescue)[0]);
|
|
|
|
if (len > 1 && !NIL_P(str = RARRAY_PTR(rescue)[1])) {
|
2003-10-02 01:19:18 -04:00
|
|
|
StringValue(str);
|
2006-08-31 06:30:33 -04:00
|
|
|
inlen = length = RSTRING_LEN(str);
|
|
|
|
instart = inptr = RSTRING_PTR(str);
|
2003-10-02 01:19:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!NIL_P(rescue)) {
|
|
|
|
rb_str_concat(ret, rescue);
|
|
|
|
}
|
|
|
|
break;
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
} while (inlen > 0);
|
|
|
|
|
2007-12-04 02:34:49 -05:00
|
|
|
if (!ret) {
|
2002-03-14 08:10:57 -05:00
|
|
|
ret = rb_str_derive(str, instart, inptr - instart);
|
2007-12-04 05:56:44 -05:00
|
|
|
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
|
2007-12-04 02:34:49 -05:00
|
|
|
}
|
|
|
|
else if (inptr > instart) {
|
2003-07-29 21:31:43 -04:00
|
|
|
rb_str_cat(ret, instart, inptr - instart);
|
2007-12-04 02:34:49 -05:00
|
|
|
}
|
2002-03-14 08:10:57 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_s_allocate(VALUE klass)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
return Data_Wrap_Struct(klass, 0, ICONV_FREE, 0);
|
|
|
|
}
|
|
|
|
|
2005-11-07 06:55:48 -05:00
|
|
|
static VALUE
|
|
|
|
get_iconv_opt_i(VALUE i, VALUE arg)
|
|
|
|
{
|
|
|
|
struct rb_iconv_opt_t *opt = (struct rb_iconv_opt_t *)arg;
|
|
|
|
VALUE name, val;
|
2007-12-04 02:34:49 -05:00
|
|
|
|
|
|
|
(void)opt;
|
2005-11-07 06:55:48 -05:00
|
|
|
i = rb_Array(i);
|
|
|
|
name = rb_ary_entry(i, 0);
|
|
|
|
val = rb_ary_entry(i, 1);
|
|
|
|
do {
|
|
|
|
if (SYMBOL_P(name)) {
|
|
|
|
ID id = SYM2ID(name);
|
|
|
|
if (id == id_transliterate) {
|
|
|
|
#ifdef ICONV_SET_TRANSLITERATE
|
|
|
|
opt->transliterate = val;
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (id == id_discard_ilseq) {
|
|
|
|
#ifdef ICONV_SET_DISCARD_ILSEQ
|
|
|
|
opt->discard_ilseq = val;
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char *s = StringValueCStr(name);
|
|
|
|
if (strcmp(s, "transliterate") == 0) {
|
|
|
|
#ifdef ICONV_SET_TRANSLITERATE
|
|
|
|
opt->transliterate = val;
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp(s, "discard_ilseq") == 0) {
|
|
|
|
#ifdef ICONV_SET_DISCARD_ILSEQ
|
|
|
|
opt->discard_ilseq = val;
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name = rb_inspect(name);
|
|
|
|
rb_raise(rb_eArgError, "unknown option - %s", StringValueCStr(name));
|
|
|
|
} while (0);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_iconv_opt(struct rb_iconv_opt_t *opt, VALUE options)
|
|
|
|
{
|
|
|
|
opt->transliterate = Qundef;
|
|
|
|
opt->discard_ilseq = Qundef;
|
|
|
|
if (!NIL_P(options)) {
|
2006-02-03 04:15:42 -05:00
|
|
|
rb_block_call(options, rb_intern("each"), 0, 0, get_iconv_opt_i, (VALUE)opt);
|
2005-11-07 06:55:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define iconv_ctl(self, func, val) (\
|
|
|
|
iconvctl(VALUE2ICONV(check_iconv(self)), func, (void *)&(val)) ? \
|
|
|
|
rb_sys_fail(#func) : (void)0)
|
|
|
|
|
2005-03-04 05:40:09 -05:00
|
|
|
/*
|
|
|
|
* Document-method: new
|
2005-11-07 06:55:48 -05:00
|
|
|
* call-seq: Iconv.new(to, from, [options])
|
2005-03-04 05:40:09 -05:00
|
|
|
*
|
|
|
|
* Creates new code converter from a coding-system designated with +from+
|
|
|
|
* to another one designated with +to+.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* === Parameters
|
|
|
|
*
|
|
|
|
* +to+:: encoding name for destination
|
|
|
|
* +from+:: encoding name for source
|
2005-11-07 06:55:48 -05:00
|
|
|
* +options+:: options for converter
|
2005-03-04 05:40:09 -05:00
|
|
|
*
|
|
|
|
* === Exceptions
|
|
|
|
*
|
|
|
|
* TypeError:: if +to+ or +from+ aren't String
|
2005-03-15 09:50:08 -05:00
|
|
|
* InvalidEncoding:: if designated converter couldn't find out
|
|
|
|
* SystemCallError:: if <tt>iconv_open(3)</tt> fails
|
2005-03-04 05:40:09 -05:00
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-11-07 06:55:48 -05:00
|
|
|
iconv_initialize(int argc, VALUE *argv, VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2005-11-07 06:55:48 -05:00
|
|
|
VALUE to, from, options;
|
|
|
|
struct rb_iconv_opt_t opt;
|
2007-12-04 02:34:49 -05:00
|
|
|
int idx;
|
2005-11-07 06:55:48 -05:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "21", &to, &from, &options);
|
|
|
|
get_iconv_opt(&opt, options);
|
2003-07-22 21:11:20 -04:00
|
|
|
iconv_free(check_iconv(self));
|
2002-03-14 08:10:57 -05:00
|
|
|
DATA_PTR(self) = NULL;
|
2007-12-04 02:34:49 -05:00
|
|
|
DATA_PTR(self) = (void *)ICONV2VALUE(iconv_create(to, from, &opt, &idx));
|
|
|
|
if (idx >= 0) ENCODING_SET(self, idx);
|
2002-03-14 08:10:57 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2005-03-04 05:40:09 -05:00
|
|
|
/*
|
|
|
|
* Document-method: open
|
|
|
|
* call-seq: Iconv.open(to, from) { |iconv| ... }
|
|
|
|
*
|
|
|
|
* Equivalent to Iconv.new except that when it is called with a block, it
|
|
|
|
* yields with the new instance and closes it, and returns the result which
|
|
|
|
* returned from the block.
|
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-11-07 06:55:48 -05:00
|
|
|
iconv_s_open(int argc, VALUE *argv, VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2005-11-07 06:55:48 -05:00
|
|
|
VALUE to, from, options, cd;
|
|
|
|
struct rb_iconv_opt_t opt;
|
2007-12-04 02:34:49 -05:00
|
|
|
int idx;
|
2005-11-07 06:55:48 -05:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "21", &to, &from, &options);
|
|
|
|
get_iconv_opt(&opt, options);
|
2007-12-04 02:34:49 -05:00
|
|
|
cd = ICONV2VALUE(iconv_create(to, from, &opt, &idx));
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2003-07-22 21:11:20 -04:00
|
|
|
self = Data_Wrap_Struct(self, NULL, ICONV_FREE, (void *)cd);
|
2007-12-04 02:34:49 -05:00
|
|
|
if (idx >= 0) ENCODING_SET(self, idx);
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
return rb_ensure(rb_yield, self, (VALUE(*)())iconv_finish, self);
|
|
|
|
}
|
|
|
|
else {
|
2003-07-22 21:11:20 -04:00
|
|
|
return self;
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_s_convert(struct iconv_env_t* env)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
VALUE last = 0;
|
|
|
|
|
|
|
|
for (; env->argc > 0; --env->argc, ++env->argv) {
|
2008-02-08 05:19:57 -05:00
|
|
|
VALUE s = iconv_convert(env->cd, last = *(env->argv),
|
|
|
|
0, -1, env->toidx, env);
|
2003-10-02 07:33:52 -04:00
|
|
|
env->append(env->ret, s);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(last)) {
|
2008-02-08 05:19:57 -05:00
|
|
|
VALUE s = iconv_convert(env->cd, Qnil, 0, 0, env->toidx, env);
|
2006-08-31 06:30:33 -04:00
|
|
|
if (RSTRING_LEN(s))
|
2003-10-02 07:33:52 -04:00
|
|
|
env->append(env->ret, s);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return env->ret;
|
|
|
|
}
|
|
|
|
|
2005-03-04 05:40:09 -05:00
|
|
|
/*
|
2007-11-16 03:03:36 -05:00
|
|
|
* Document-method: Iconv::iconv
|
2005-03-04 05:40:09 -05:00
|
|
|
* call-seq: Iconv.iconv(to, from, *strs)
|
|
|
|
*
|
|
|
|
* Shorthand for
|
|
|
|
* Iconv.open(to, from) { |cd|
|
|
|
|
* (strs + [nil]).collect { |s| cd.iconv(s) }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* === Parameters
|
|
|
|
*
|
|
|
|
* <tt>to, from</tt>:: see Iconv.new
|
|
|
|
* <tt>strs</tt>:: strings to be converted
|
|
|
|
*
|
|
|
|
* === Exceptions
|
|
|
|
*
|
|
|
|
* Exceptions thrown by Iconv.new, Iconv.open and Iconv#iconv.
|
2005-03-15 09:50:08 -05:00
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_s_iconv(int argc, VALUE *argv, VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
struct iconv_env_t arg;
|
|
|
|
|
|
|
|
if (argc < 2) /* needs `to' and `from' arguments at least */
|
2004-03-29 02:54:38 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, 2);
|
2002-03-14 08:10:57 -05:00
|
|
|
|
|
|
|
arg.argc = argc -= 2;
|
|
|
|
arg.argv = argv + 2;
|
2003-10-02 07:33:52 -04:00
|
|
|
arg.append = rb_ary_push;
|
2002-03-14 08:10:57 -05:00
|
|
|
arg.ret = rb_ary_new2(argc);
|
2007-12-04 02:34:49 -05:00
|
|
|
arg.cd = iconv_create(argv[0], argv[1], NULL, &arg.toidx);
|
2002-03-14 08:10:57 -05:00
|
|
|
return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
|
|
|
|
}
|
|
|
|
|
2004-02-15 22:29:16 -05:00
|
|
|
/*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Document-method: Iconv::conv
|
2007-05-10 02:20:33 -04:00
|
|
|
* call-seq: Iconv.conv(to, from, str)
|
2005-03-04 05:40:09 -05:00
|
|
|
*
|
|
|
|
* Shorthand for
|
|
|
|
* Iconv.iconv(to, from, str).join
|
2005-03-15 09:50:08 -05:00
|
|
|
* See Iconv.iconv.
|
2005-03-04 05:40:09 -05:00
|
|
|
*/
|
2003-07-18 15:51:42 -04:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_s_conv(VALUE self, VALUE to, VALUE from, VALUE str)
|
2003-07-18 15:51:42 -04:00
|
|
|
{
|
|
|
|
struct iconv_env_t arg;
|
|
|
|
|
|
|
|
arg.argc = 1;
|
|
|
|
arg.argv = &str;
|
2003-10-02 07:33:52 -04:00
|
|
|
arg.append = rb_str_append;
|
2003-07-18 15:51:42 -04:00
|
|
|
arg.ret = rb_str_new(0, 0);
|
2007-12-04 02:34:49 -05:00
|
|
|
arg.cd = iconv_create(to, from, NULL, &arg.toidx);
|
2003-07-18 15:51:42 -04:00
|
|
|
return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
|
|
|
|
}
|
|
|
|
|
2004-02-15 22:29:16 -05:00
|
|
|
/*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Document-method: list
|
|
|
|
* call-seq: Iconv.list {|*aliases| ... }
|
|
|
|
*
|
|
|
|
* Iterates each alias sets.
|
2004-02-15 22:29:16 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_ICONVLIST
|
2005-09-09 03:45:36 -04:00
|
|
|
struct iconv_name_list
|
|
|
|
{
|
2004-02-15 22:29:16 -05:00
|
|
|
unsigned int namescount;
|
|
|
|
const char *const *names;
|
2004-04-08 05:22:04 -04:00
|
|
|
VALUE array;
|
2004-02-15 22:29:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
list_iconv_i(VALUE ptr)
|
2004-02-15 22:29:16 -05:00
|
|
|
{
|
|
|
|
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]));
|
|
|
|
}
|
2004-04-08 05:22:04 -04:00
|
|
|
if (p->array) {
|
|
|
|
return rb_ary_push(p->array, ary);
|
|
|
|
}
|
2004-02-15 22:29:16 -05:00
|
|
|
return rb_yield(ary);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-09-09 03:45:36 -04:00
|
|
|
list_iconv(unsigned int namescount, const char *const *names, void *data)
|
2004-02-15 22:29:16 -05:00
|
|
|
{
|
|
|
|
int *state = data;
|
|
|
|
struct iconv_name_list list;
|
|
|
|
|
|
|
|
list.namescount = namescount;
|
|
|
|
list.names = names;
|
2004-04-08 05:22:04 -04:00
|
|
|
list.array = ((VALUE *)data)[1];
|
2004-02-15 22:29:16 -05:00
|
|
|
rb_protect(list_iconv_i, (VALUE)&list, state);
|
|
|
|
return *state;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-18 10:10:06 -04:00
|
|
|
#if defined(HAVE_ICONVLIST) || defined(HAVE___ICONV_FREE_LIST)
|
2004-02-15 22:29:16 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_s_list(void)
|
2004-02-15 22:29:16 -05:00
|
|
|
{
|
|
|
|
#ifdef HAVE_ICONVLIST
|
2004-04-08 05:22:04 -04:00
|
|
|
int state;
|
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[1] = rb_block_given_p() ? 0 : rb_ary_new();
|
|
|
|
iconvlist(list_iconv, args);
|
|
|
|
state = *(int *)args;
|
|
|
|
if (state) rb_jump_tag(state);
|
|
|
|
if (args[1]) return args[1];
|
2007-12-05 11:04:12 -05:00
|
|
|
#elif defined(HAVE___ICONV_FREE_LIST)
|
|
|
|
char **list;
|
|
|
|
size_t sz, i;
|
|
|
|
VALUE ary;
|
|
|
|
|
|
|
|
if (__iconv_get_list(&list, &sz)) return Qnil;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(sz);
|
|
|
|
for (i = 0; i < sz; i++) {
|
|
|
|
rb_ary_push(ary, rb_str_new2(list[i]));
|
|
|
|
}
|
|
|
|
__iconv_free_list(list, sz);
|
|
|
|
|
|
|
|
if (!rb_block_given_p())
|
|
|
|
return ary;
|
|
|
|
for (i = 0; i < RARRAY_LEN(ary); i++) {
|
|
|
|
rb_yield(RARRAY_PTR(ary)[i]);
|
|
|
|
}
|
2004-02-15 22:29:16 -05:00
|
|
|
#endif
|
|
|
|
return Qnil;
|
|
|
|
}
|
2009-04-18 10:10:06 -04:00
|
|
|
#else
|
|
|
|
#define iconv_s_list rb_f_notimplement
|
|
|
|
#endif
|
2004-02-15 22:29:16 -05:00
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
/*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Document-method: close
|
|
|
|
*
|
|
|
|
* Finishes conversion.
|
|
|
|
*
|
|
|
|
* After calling this, calling Iconv#iconv will cause an exception, but
|
|
|
|
* multiple calls of #close are guaranteed to end successfully.
|
|
|
|
*
|
|
|
|
* Returns a string containing the byte sequence to change the output buffer to
|
2005-03-15 09:50:08 -05:00
|
|
|
* its initial shift state.
|
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2008-02-08 05:19:57 -05:00
|
|
|
iconv_init_state(VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2008-02-08 05:19:57 -05:00
|
|
|
iconv_t cd = VALUE2ICONV((VALUE)DATA_PTR(self));
|
|
|
|
DATA_PTR(self) = NULL;
|
|
|
|
return iconv_convert(cd, Qnil, 0, 0, ENCODING_GET(self), NULL);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_finish(VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2003-07-22 21:11:20 -04:00
|
|
|
VALUE cd = check_iconv(self);
|
2002-03-14 08:10:57 -05:00
|
|
|
|
|
|
|
if (!cd) return Qnil;
|
2008-02-08 05:19:57 -05:00
|
|
|
return rb_ensure(iconv_init_state, self, iconv_free, cd);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-11-16 03:41:37 -05:00
|
|
|
* Document-method: Iconv#iconv
|
2005-03-04 05:40:09 -05:00
|
|
|
* call-seq: iconv(str, start=0, length=-1)
|
|
|
|
*
|
|
|
|
* Converts string and returns the result.
|
|
|
|
* * If +str+ is a String, converts <tt>str[start, length]</tt> and returns the converted string.
|
|
|
|
* * If +str+ is +nil+, places converter itself into initial shift state and
|
|
|
|
* just returns a string containing the byte sequence to change the output
|
|
|
|
* buffer to its initial shift state.
|
|
|
|
* * Otherwise, raises an exception.
|
|
|
|
*
|
|
|
|
* === Parameters
|
|
|
|
*
|
|
|
|
* str:: string to be converted, or nil
|
|
|
|
* start:: starting offset
|
|
|
|
* length:: conversion length; nil or -1 means whole the string from start
|
|
|
|
*
|
|
|
|
* === Exceptions
|
|
|
|
*
|
|
|
|
* * IconvIllegalSequence
|
|
|
|
* * IconvInvalidCharacter
|
|
|
|
* * IconvOutOfRange
|
|
|
|
*
|
|
|
|
* === Examples
|
|
|
|
*
|
|
|
|
* See the Iconv documentation.
|
2005-03-15 09:50:08 -05:00
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_iconv(int argc, VALUE *argv, VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
VALUE str, n1, n2;
|
2003-07-22 21:11:20 -04:00
|
|
|
VALUE cd = check_iconv(self);
|
2008-06-04 04:52:25 -04:00
|
|
|
long start = 0, length = 0, slen = 0;
|
2002-03-14 08:10:57 -05:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "12", &str, &n1, &n2);
|
2008-06-04 04:52:25 -04:00
|
|
|
if (!NIL_P(str)) {
|
|
|
|
VALUE n = rb_str_length(StringValue(str));
|
|
|
|
slen = NUM2LONG(n);
|
|
|
|
}
|
|
|
|
if (argc != 2 || !RTEST(rb_range_beg_len(n1, &start, &length, slen, 0))) {
|
|
|
|
if (NIL_P(n1) || ((start = NUM2LONG(n1)) < 0 ? (start += slen) >= 0 : start < slen)) {
|
|
|
|
length = NIL_P(n2) ? -1 : NUM2LONG(n2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start > 0 || length > 0) {
|
|
|
|
rb_encoding *enc = rb_enc_get(str);
|
|
|
|
const char *s = RSTRING_PTR(str), *e = s + RSTRING_LEN(str);
|
|
|
|
const char *ps = s;
|
|
|
|
if (start > 0) {
|
|
|
|
start = (ps = rb_enc_nth(s, e, start, enc)) - s;
|
|
|
|
}
|
|
|
|
if (length > 0) {
|
|
|
|
length = rb_enc_nth(ps, e, length, enc) - ps;
|
|
|
|
}
|
|
|
|
}
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2008-06-04 04:52:25 -04:00
|
|
|
return iconv_convert(VALUE2ICONV(cd), str, start, length, ENCODING_GET(self), NULL);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
2005-11-07 06:55:48 -05:00
|
|
|
/*
|
|
|
|
* Document-method: conv
|
|
|
|
* call-seq: conv(str...)
|
|
|
|
*
|
|
|
|
* Equivalent to
|
|
|
|
*
|
|
|
|
* iconv(nil, str..., nil).join
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
iconv_conv(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
iconv_t cd = VALUE2ICONV(check_iconv(self));
|
|
|
|
VALUE str, s;
|
2008-02-08 05:19:57 -05:00
|
|
|
int toidx = ENCODING_GET(self);
|
2005-11-07 06:55:48 -05:00
|
|
|
|
2008-02-08 05:19:57 -05:00
|
|
|
str = iconv_convert(cd, Qnil, 0, 0, toidx, NULL);
|
2005-11-07 06:55:48 -05:00
|
|
|
if (argc > 0) {
|
|
|
|
do {
|
2008-02-08 05:19:57 -05:00
|
|
|
s = iconv_convert(cd, *argv++, 0, -1, toidx, NULL);
|
2006-08-31 06:30:33 -04:00
|
|
|
if (RSTRING_LEN(s))
|
2005-11-07 06:55:48 -05:00
|
|
|
rb_str_buf_append(str, s);
|
|
|
|
} while (--argc);
|
2008-02-08 05:19:57 -05:00
|
|
|
s = iconv_convert(cd, Qnil, 0, 0, toidx, NULL);
|
2006-08-31 06:30:33 -04:00
|
|
|
if (RSTRING_LEN(s))
|
2005-11-07 06:55:48 -05:00
|
|
|
rb_str_buf_append(str, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-04-18 10:10:06 -04:00
|
|
|
#ifdef ICONV_TRIVIALP
|
2005-11-07 06:55:48 -05:00
|
|
|
/*
|
|
|
|
* Document-method: trivial?
|
|
|
|
* call-seq: trivial?
|
|
|
|
*
|
|
|
|
* Returns trivial flag.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
iconv_trivialp(VALUE self)
|
|
|
|
{
|
|
|
|
int trivial = 0;
|
|
|
|
iconv_ctl(self, ICONV_TRIVIALP, trivial);
|
|
|
|
if (trivial) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2009-04-18 10:10:06 -04:00
|
|
|
#else
|
|
|
|
#define iconv_trivialp rb_f_notimplement
|
|
|
|
#endif
|
2005-11-07 06:55:48 -05:00
|
|
|
|
2009-04-18 10:10:06 -04:00
|
|
|
#ifdef ICONV_GET_TRANSLITERATE
|
2005-11-07 06:55:48 -05:00
|
|
|
/*
|
|
|
|
* Document-method: transliterate?
|
|
|
|
* call-seq: transliterate?
|
|
|
|
*
|
|
|
|
* Returns transliterate flag.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
iconv_get_transliterate(VALUE self)
|
|
|
|
{
|
|
|
|
int trans = 0;
|
|
|
|
iconv_ctl(self, ICONV_GET_TRANSLITERATE, trans);
|
|
|
|
if (trans) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2009-04-18 10:10:06 -04:00
|
|
|
#else
|
|
|
|
#define iconv_get_transliterate rb_f_notimplement
|
|
|
|
#endif
|
2005-11-07 06:55:48 -05:00
|
|
|
|
2009-04-18 10:10:06 -04:00
|
|
|
#ifdef ICONV_SET_TRANSLITERATE
|
2005-11-07 06:55:48 -05:00
|
|
|
/*
|
|
|
|
* Document-method: transliterate=
|
|
|
|
* call-seq: cd.transliterate = flag
|
|
|
|
*
|
|
|
|
* Sets transliterate flag.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
iconv_set_transliterate(VALUE self, VALUE transliterate)
|
|
|
|
{
|
|
|
|
int trans = RTEST(transliterate);
|
|
|
|
iconv_ctl(self, ICONV_SET_TRANSLITERATE, trans);
|
|
|
|
return self;
|
|
|
|
}
|
2009-04-18 10:10:06 -04:00
|
|
|
#else
|
|
|
|
#define iconv_set_transliterate rb_f_notimplement
|
|
|
|
#endif
|
2005-11-07 06:55:48 -05:00
|
|
|
|
2009-04-18 10:10:06 -04:00
|
|
|
#ifdef ICONV_GET_DISCARD_ILSEQ
|
2005-11-07 06:55:48 -05:00
|
|
|
/*
|
|
|
|
* Document-method: discard_ilseq?
|
|
|
|
* call-seq: discard_ilseq?
|
|
|
|
*
|
|
|
|
* Returns discard_ilseq flag.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
iconv_get_discard_ilseq(VALUE self)
|
|
|
|
{
|
|
|
|
int dis = 0;
|
|
|
|
iconv_ctl(self, ICONV_GET_DISCARD_ILSEQ, dis);
|
|
|
|
if (dis) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2009-04-18 10:10:06 -04:00
|
|
|
#else
|
|
|
|
#define iconv_get_discard_ilseq rb_f_notimplement
|
|
|
|
#endif
|
2005-11-07 06:55:48 -05:00
|
|
|
|
2009-04-18 10:10:06 -04:00
|
|
|
#ifdef ICONV_SET_DISCARD_ILSEQ
|
2005-11-07 06:55:48 -05:00
|
|
|
/*
|
|
|
|
* Document-method: discard_ilseq=
|
|
|
|
* call-seq: cd.discard_ilseq = flag
|
|
|
|
*
|
|
|
|
* Sets discard_ilseq flag.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
iconv_set_discard_ilseq(VALUE self, VALUE discard_ilseq)
|
|
|
|
{
|
|
|
|
int dis = RTEST(discard_ilseq);
|
|
|
|
iconv_ctl(self, ICONV_SET_DISCARD_ILSEQ, dis);
|
|
|
|
return self;
|
|
|
|
}
|
2009-04-18 10:10:06 -04:00
|
|
|
#else
|
|
|
|
#define iconv_set_discard_ilseq rb_f_notimplement
|
|
|
|
#endif
|
2005-11-07 06:55:48 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: ctlmethods
|
|
|
|
* call-seq: Iconv.ctlmethods => array
|
|
|
|
*
|
|
|
|
* Returns available iconvctl() method list.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
iconv_s_ctlmethods(VALUE klass)
|
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
#ifdef ICONV_TRIVIALP
|
|
|
|
rb_ary_push(ary, ID2SYM(rb_intern("trivial?")));
|
|
|
|
#endif
|
|
|
|
#ifdef ICONV_GET_TRANSLITERATE
|
|
|
|
rb_ary_push(ary, ID2SYM(rb_intern("transliterate?")));
|
|
|
|
#endif
|
|
|
|
#ifdef ICONV_SET_TRANSLITERATE
|
|
|
|
rb_ary_push(ary, ID2SYM(rb_intern("transliterate=")));
|
|
|
|
#endif
|
|
|
|
#ifdef ICONV_GET_DISCARD_ILSEQ
|
|
|
|
rb_ary_push(ary, ID2SYM(rb_intern("discard_ilseq?")));
|
|
|
|
#endif
|
|
|
|
#ifdef ICONV_SET_DISCARD_ILSEQ
|
|
|
|
rb_ary_push(ary, ID2SYM(rb_intern("discard_ilseq=")));
|
|
|
|
#endif
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
/*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Document-class: Iconv::Failure
|
|
|
|
*
|
|
|
|
* Base attributes for Iconv exceptions.
|
2005-03-15 09:50:08 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: success
|
|
|
|
* call-seq: success
|
2005-03-04 05:40:09 -05:00
|
|
|
*
|
|
|
|
* Returns string(s) translated successfully until the exception occurred.
|
|
|
|
* * In the case of failure occurred within Iconv.iconv, returned
|
|
|
|
* value is an array of strings translated successfully preceding
|
|
|
|
* failure and the last element is string on the way.
|
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_failure_success(VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2003-07-22 21:11:20 -04:00
|
|
|
return rb_attr_get(self, rb_success);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-03-15 09:50:08 -05:00
|
|
|
* Document-method: failed
|
|
|
|
* call-seq: failed
|
|
|
|
*
|
|
|
|
* Returns substring of the original string passed to Iconv that starts at the
|
2010-04-22 04:04:13 -04:00
|
|
|
* character caused the exception.
|
2005-03-04 05:40:09 -05:00
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_failure_failed(VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2003-07-22 21:11:20 -04:00
|
|
|
return rb_attr_get(self, rb_failed);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|
2005-03-15 09:50:08 -05:00
|
|
|
/*
|
|
|
|
* Document-method: inspect
|
|
|
|
* call-seq: inspect
|
|
|
|
*
|
|
|
|
* Returns inspected string like as: #<_class_: _success_, _failed_>
|
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
static VALUE
|
2005-09-09 03:45:36 -04:00
|
|
|
iconv_failure_inspect(VALUE self)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
2008-06-04 04:52:25 -04:00
|
|
|
const char *cname = rb_class2name(CLASS_OF(self));
|
2003-07-22 21:11:20 -04:00
|
|
|
VALUE success = rb_attr_get(self, rb_success);
|
|
|
|
VALUE failed = rb_attr_get(self, rb_failed);
|
2002-03-14 08:10:57 -05:00
|
|
|
VALUE str = rb_str_buf_cat2(rb_str_new2("#<"), cname);
|
|
|
|
str = rb_str_buf_cat(str, ": ", 2);
|
|
|
|
str = rb_str_buf_append(str, rb_inspect(success));
|
|
|
|
str = rb_str_buf_cat(str, ", ", 2);
|
|
|
|
str = rb_str_buf_append(str, rb_inspect(failed));
|
|
|
|
return rb_str_buf_cat(str, ">", 1);
|
|
|
|
}
|
|
|
|
|
2005-03-15 09:50:08 -05:00
|
|
|
/*
|
|
|
|
* Document-class: Iconv::InvalidEncoding
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-15 09:50:08 -05:00
|
|
|
* Requested coding-system is not available on this system.
|
|
|
|
*/
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
/*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Document-class: Iconv::IllegalSequence
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Input conversion stopped due to an input byte that does not belong to
|
|
|
|
* the input codeset, or the output codeset does not contain the
|
|
|
|
* character.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: Iconv::InvalidCharacter
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Input conversion stopped due to an incomplete character or shift
|
|
|
|
* sequence at the end of the input buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: Iconv::OutOfRange
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-03-04 05:40:09 -05:00
|
|
|
* Iconv library internal error. Must not occur.
|
|
|
|
*/
|
2002-03-14 08:10:57 -05:00
|
|
|
|
2005-11-06 09:41:53 -05:00
|
|
|
/*
|
|
|
|
* Document-class: Iconv::BrokenLibrary
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2005-11-06 09:41:53 -05:00
|
|
|
* Detected a bug of underlying iconv(3) libray.
|
|
|
|
* * returns an error without setting errno properly
|
|
|
|
*/
|
|
|
|
|
2002-03-14 08:10:57 -05:00
|
|
|
void
|
2005-09-09 03:45:36 -04:00
|
|
|
Init_iconv(void)
|
2002-03-14 08:10:57 -05:00
|
|
|
{
|
|
|
|
VALUE rb_cIconv = rb_define_class("Iconv", rb_cData);
|
2003-07-18 15:51:42 -04:00
|
|
|
|
* ext/curses/curses.c, ext/digest/digest.c, ext/dl/handle.c,
ext/dl/ptr.c, ext/dl/sym.c, ext/gdbm/gdbm.c, ext/iconv/iconv.c,
ext/stringio/stringio.c, ext/strscan/strscan.c,
ext/tcltklib/tcltklib.c, ext/win32ole/win32ole.c:
use rb_define_alloc_func().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3193 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-20 06:23:40 -05:00
|
|
|
rb_define_alloc_func(rb_cIconv, iconv_s_allocate);
|
2005-11-07 06:55:48 -05:00
|
|
|
rb_define_singleton_method(rb_cIconv, "open", iconv_s_open, -1);
|
2002-03-14 08:10:57 -05:00
|
|
|
rb_define_singleton_method(rb_cIconv, "iconv", iconv_s_iconv, -1);
|
2003-07-18 15:51:42 -04:00
|
|
|
rb_define_singleton_method(rb_cIconv, "conv", iconv_s_conv, 3);
|
2004-02-15 22:29:16 -05:00
|
|
|
rb_define_singleton_method(rb_cIconv, "list", iconv_s_list, 0);
|
2005-11-07 06:55:48 -05:00
|
|
|
rb_define_singleton_method(rb_cIconv, "ctlmethods", iconv_s_ctlmethods, 0);
|
|
|
|
rb_define_method(rb_cIconv, "initialize", iconv_initialize, -1);
|
2002-03-14 08:10:57 -05:00
|
|
|
rb_define_method(rb_cIconv, "close", iconv_finish, 0);
|
|
|
|
rb_define_method(rb_cIconv, "iconv", iconv_iconv, -1);
|
2005-11-07 06:55:48 -05:00
|
|
|
rb_define_method(rb_cIconv, "conv", iconv_conv, -1);
|
|
|
|
rb_define_method(rb_cIconv, "trivial?", iconv_trivialp, 0);
|
|
|
|
rb_define_method(rb_cIconv, "transliterate?", iconv_get_transliterate, 0);
|
|
|
|
rb_define_method(rb_cIconv, "transliterate=", iconv_set_transliterate, 1);
|
|
|
|
rb_define_method(rb_cIconv, "discard_ilseq?", iconv_get_discard_ilseq, 0);
|
|
|
|
rb_define_method(rb_cIconv, "discard_ilseq=", iconv_set_discard_ilseq, 1);
|
2002-03-14 08:10:57 -05:00
|
|
|
|
|
|
|
rb_eIconvFailure = rb_define_module_under(rb_cIconv, "Failure");
|
2003-10-02 07:33:52 -04:00
|
|
|
rb_define_method(rb_eIconvFailure, "initialize", iconv_failure_initialize, 3);
|
2002-03-14 08:10:57 -05:00
|
|
|
rb_define_method(rb_eIconvFailure, "success", iconv_failure_success, 0);
|
|
|
|
rb_define_method(rb_eIconvFailure, "failed", iconv_failure_failed, 0);
|
|
|
|
rb_define_method(rb_eIconvFailure, "inspect", iconv_failure_inspect, 0);
|
|
|
|
|
2004-01-18 09:59:49 -05:00
|
|
|
rb_eIconvInvalidEncoding = rb_define_class_under(rb_cIconv, "InvalidEncoding", rb_eArgError);
|
2002-03-14 08:10:57 -05:00
|
|
|
rb_eIconvIllegalSeq = rb_define_class_under(rb_cIconv, "IllegalSequence", rb_eArgError);
|
|
|
|
rb_eIconvInvalidChar = rb_define_class_under(rb_cIconv, "InvalidCharacter", rb_eArgError);
|
|
|
|
rb_eIconvOutOfRange = rb_define_class_under(rb_cIconv, "OutOfRange", rb_eRuntimeError);
|
2005-11-06 09:41:53 -05:00
|
|
|
rb_eIconvBrokenLibrary = rb_define_class_under(rb_cIconv, "BrokenLibrary", rb_eRuntimeError);
|
2005-03-15 09:50:08 -05:00
|
|
|
rb_include_module(rb_eIconvInvalidEncoding, rb_eIconvFailure);
|
2002-03-14 08:10:57 -05:00
|
|
|
rb_include_module(rb_eIconvIllegalSeq, rb_eIconvFailure);
|
|
|
|
rb_include_module(rb_eIconvInvalidChar, rb_eIconvFailure);
|
|
|
|
rb_include_module(rb_eIconvOutOfRange, rb_eIconvFailure);
|
2005-11-06 09:41:53 -05:00
|
|
|
rb_include_module(rb_eIconvBrokenLibrary, rb_eIconvFailure);
|
2002-03-14 08:10:57 -05:00
|
|
|
|
|
|
|
rb_success = rb_intern("success");
|
|
|
|
rb_failed = rb_intern("failed");
|
2005-11-07 06:55:48 -05:00
|
|
|
id_transliterate = rb_intern("transliterate");
|
|
|
|
id_discard_ilseq = rb_intern("discard_ilseq");
|
2003-07-18 15:51:42 -04:00
|
|
|
|
|
|
|
rb_gc_register_address(&charset_map);
|
2005-12-11 19:36:54 -05:00
|
|
|
charset_map = rb_hash_new();
|
2003-07-18 15:51:42 -04:00
|
|
|
rb_define_singleton_method(rb_cIconv, "charset_map", charset_map_get, 0);
|
2002-03-14 08:10:57 -05:00
|
|
|
}
|
|
|
|
|