Class: Iconv

Inherits:
Data show all
Defined in:
ext/iconv/iconv.c

Overview

Document-class: Iconv::BrokenLibrary

Detected a bug of underlying iconv(3) libray.

  • returns an error without setting errno properly

Defined Under Namespace

Classes: BrokenLibrary, IllegalSequence, InvalidCharacter, InvalidEncoding, OutOfRange

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Object) new(to, from, [options])

Creates new code converter from a coding-system designated with from to another one designated with to.

Parameters

to

encoding name for destination

from

encoding name for source

options

options for converter

Exceptions

TypeError

if to or from aren't String

InvalidEncoding

if designated converter couldn't find out

SystemCallError

if iconv_open(3) fails



# File 'ext/iconv/iconv.c'

static VALUE
iconv_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE to, from, options;
    struct rb_iconv_opt_t opt;
    int idx;

    rb_scan_args(argc, argv, "21", &to, &from, &options);
    get_iconv_opt(&opt, options);
    iconv_free(check_iconv(self));
    DATA_PTR(self) = NULL;
    DATA_PTR(self) = (void *)ICONV2VALUE(iconv_create(to, from, &opt, &idx));
    if (idx >= 0) rb_enc_set_index(self, idx);
    return self;
}

Class Method Details

+ (Object) charset_map

Returns the map from canonical name to system dependent name.



# File 'ext/iconv/iconv.c'

/*
 * Document-method: charset_map
 * call-seq: Iconv.charset_map
 *
 * Returns the map from canonical name to system dependent name.
 */
static VALUE
charset_map_get(void)
{
    return charset_map;
}

+ (Object) conv(to, from, str)

Shorthand for

Iconv.iconv(to, from, str).join

See Iconv.iconv.



# File 'ext/iconv/iconv.c'

/*
 * Document-method: Iconv::conv
 * call-seq: Iconv.conv(to, from, str)
 *
 * Shorthand for
 *   Iconv.iconv(to, from, str).join
 * See Iconv.iconv.
 */
static VALUE
iconv_s_conv(VALUE self, VALUE to, VALUE from, VALUE str)
{
    struct iconv_env_t arg;

    arg.argc = 1;
    arg.argv = &str;
    arg.append = rb_str_append;
    arg.ret = rb_str_new(0, 0);
    arg.cd = iconv_create(to, from, NULL, &arg.toidx);
    return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
}

+ (Array) ctlmethods

Returns available iconvctl() method list.

Returns:



# File 'ext/iconv/iconv.c'

/*
 * 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;
}

+ (Object) iconv(to, from, *strs)

Shorthand for

Iconv.open(to, from) { |cd|
  (strs + [nil]).collect { |s| cd.iconv(s) }
}

Parameters

to, from

see Iconv.new

strs

strings to be converted

Exceptions

Exceptions thrown by Iconv.new, Iconv.open and Iconv#iconv.



# File 'ext/iconv/iconv.c'

/*
 * Document-method: Iconv::iconv
 * 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.
 */
static VALUE
iconv_s_iconv(int argc, VALUE *argv, VALUE self)
{
    struct iconv_env_t arg;

    if (argc < 2)       /* needs `to' and `from' arguments at least */
    rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, 2);

    arg.argc = argc -= 2;
    arg.argv = argv + 2;
    arg.append = rb_ary_push;
    arg.ret = rb_ary_new2(argc);
    arg.cd = iconv_create(argv[0], argv[1], NULL, &arg.toidx);
    return rb_ensure(iconv_s_convert, (VALUE)&arg, iconv_free, ICONV2VALUE(arg.cd));
}

+ (Object) list {|*aliases| ... }

Iterates each alias sets.

Yields:

  • (*aliases)

+ (Object) 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.

Yields:



# File 'ext/iconv/iconv.c'

/*
 * 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.
 */
static VALUE
iconv_s_open(int argc, VALUE *argv, VALUE self)
{
VALUE to, from, options, cd;
struct rb_iconv_opt_t opt;
int idx;

rb_scan_args(argc, argv, "21", &to, &from, &options);
get_iconv_opt(&opt, options);
cd = ICONV2VALUE(iconv_create(to, from, &opt, &idx));

self = Data_Wrap_Struct(self, NULL, ICONV_FREE, (void *)cd);
if (idx >= 0) rb_enc_set_index(self, idx);

if (rb_block_given_p()) {
return rb_ensure(rb_yield, self, (VALUE(*)())iconv_finish, self);
}

Instance Method Details

- (Object) 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 its initial shift state.

- (Object) conv(to, from, str)

Shorthand for

Iconv.iconv(to, from, str).join

See Iconv.iconv.



# File 'ext/iconv/iconv.c'

/*
 * 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;
int toidx = rb_enc_get_index(self);

str = iconv_convert(cd, Qnil, 0, 0, toidx, NULL);
if (argc > 0) {
do {
    s = iconv_convert(cd, *argv++, 0, -1, toidx, NULL);
    if (RSTRING_LEN(s))
    rb_str_buf_append(str, s);
}

- (Object) discard_ilseq=(flag)

Sets discard_ilseq flag.



# File 'ext/iconv/iconv.c'

/*
 * 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;
}

- (Object) discard_ilseq?

Returns discard_ilseq flag.



# File 'ext/iconv/iconv.c'

/*
 * 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;
}

- (Object) iconv(to, from, *strs)

Shorthand for

Iconv.open(to, from) { |cd|
  (strs + [nil]).collect { |s| cd.iconv(s) }
}

Parameters

to, from

see Iconv.new

strs

strings to be converted

Exceptions

Exceptions thrown by Iconv.new, Iconv.open and Iconv#iconv.



# File 'ext/iconv/iconv.c'

/*
 * Document-method: Iconv#iconv
 * 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.
 */
static VALUE
iconv_iconv(int argc, VALUE *argv, VALUE self)
{
    VALUE str, n1, n2;
    VALUE cd = check_iconv(self);
    long start = 0, length = 0, slen = 0;

    rb_scan_args(argc, argv, "12", &str, &n1, &n2);
    if (!NIL_P(str)) {
    VALUE n = rb_str_length(StringValue(str));
    slen = NUM2LONG(n);

    if (!NIL_P(n1)) {
start = NUM2LONG(n1);
if (start < 0) {
start += slen;
}

- (Object) transliterate=(flag)

Sets transliterate flag.



# File 'ext/iconv/iconv.c'

/*
 * 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;
}

- (Object) transliterate?

Returns transliterate flag.



# File 'ext/iconv/iconv.c'

/*
 * 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;
}

- (Object) trivial?

Returns trivial flag.



# File 'ext/iconv/iconv.c'

/*
 * 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;
}