/* * This file was generated automatically by ExtUtils::ParseXS version 2.18 from the * contents of Iconv.xs. Do not edit this file, edit Iconv.xs instead. * * ANY CHANGES MADE HERE WILL BE LOST! * */ #line 1 "Iconv.xs" /* $Id: Iconv.xs,v 1.13 2007/08/30 12:52:42 mxp Exp $ */ /* XSUB for Perl module Text::Iconv */ /* Copyright (c) 2007 Michael Piotrowski */ #ifdef __cplusplus extern "C" { #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #ifdef __cplusplus } #endif #include /*****************************************************************************/ /* This struct represents a Text::Iconv object */ struct tiobj { iconv_t handle; /* iconv handle (returned by iconv_open()) */ SV *retval; /* iconv() return value (according to the Single UNIX Specification, "the number of non-identical conversions performed") */ SV *raise_error; /* Per-object flag controlling whether exceptions are to be thrown */ }; /*****************************************************************************/ static int raise_error = 0; /* Macro for checking when to throw an exception for use in the do_conv() function. The logic is: Throw an exception IF obj->raise_error is undef AND raise_error is true OR IF obj->raise_error is true */ #define RAISE_ERROR_P (!SvOK(obj->raise_error) && raise_error) \ || SvTRUE(obj->raise_error) SV *do_conv(struct tiobj *obj, SV *string) { char *ibuf; /* char* to the content of SV *string */ char *obuf; /* temporary output buffer */ size_t inbytesleft; /* no. of bytes left to convert; initially this is the length of the input string, and 0 when the conversion has finished */ size_t outbytesleft; /* no. of bytes in the output buffer */ size_t l_obuf; /* length of the output buffer */ char *icursor; /* current position in the input buffer */ /* The Single UNIX Specification (version 1 and version 2), as well as the HP-UX documentation from which the XPG iconv specs are derived, are unclear about the type of the second argument to iconv() (here called icursor): The manpages say const char **, while the header files say char **. */ char *ocursor; /* current position in the output buffer */ size_t ret; /* iconv() return value */ SV *perl_str; /* Perl return string */ /* Check if the input string is actually `defined'; otherwise simply return undef. This is not considered an error. */ if (! SvOK(string)) { return(&PL_sv_undef); } perl_str = newSVpv("", 0); /* Get length of input string. That's why we take an SV* instead of a char*: This way we can convert UCS-2 strings because we know their length. */ inbytesleft = SvCUR(string); ibuf = SvPV(string, inbytesleft); /* Calculate approximate amount of memory needed for the temporary output buffer and reserve the memory. The idea is to choose it large enough from the beginning to reduce the number of copy operations when converting from a single-byte to a multibyte encoding. */ if(inbytesleft <= MB_LEN_MAX) { outbytesleft = MB_LEN_MAX + 1; } else { outbytesleft = 2 * inbytesleft; } l_obuf = outbytesleft; New(0, obuf, outbytesleft, char); /* Perl malloc */ if (obuf == NULL) { croak("New: %s", strerror(errno)); } /**************************************************************************/ icursor = ibuf; ocursor = obuf; /**************************************************************************/ while(inbytesleft != 0) { #if (defined(__hpux) || defined(__linux) || defined(VMS)) && ! defined(_LIBICONV_VERSION) /* Even in HP-UX 11.00, documentation and header files do not agree */ /* glibc doesn't seem care too much about standards */ ret = iconv(obj->handle, &icursor, &inbytesleft, &ocursor, &outbytesleft); #else ret = iconv(obj->handle, (const char **)&icursor, &inbytesleft, &ocursor, &outbytesleft); #endif if(ret == (size_t) -1) { obj->retval = &PL_sv_undef; switch(errno) { case EILSEQ: /* Stop conversion if input character encountered which does not belong to the input char set */ if (RAISE_ERROR_P) croak("Character not from source char set: %s", strerror(errno)); Safefree(obuf); /* INIT_SHIFT_STATE(obj->handle, ocursor, outbytesleft); */ return(&PL_sv_undef); case EINVAL: /* Stop conversion if we encounter an incomplete character or shift sequence */ if (RAISE_ERROR_P) croak("Incomplete character or shift sequence: %s", strerror(errno)); Safefree(obuf); return(&PL_sv_undef); case E2BIG: /* fprintf(stdout, "%s\n", obuf); */ /* If the output buffer is not large enough, copy the converted bytes to the return string, reset the output buffer and continue */ sv_catpvn(perl_str, obuf, l_obuf - outbytesleft); ocursor = obuf; outbytesleft = l_obuf; break; default: if (RAISE_ERROR_P) croak("iconv error: %s", strerror(errno)); Safefree(obuf); return(&PL_sv_undef); } } else { obj->retval = newSViv(ret); } } /* For state-dependent encodings, place conversion descriptor into initial shift state and place the byte sequence to change the output buffer to its initial shift state. The only (documented) error for this use of iconv() is E2BIG; here it could happen only if the output buffer has no more room for the reset sequence. We can simply prevent this case by copying its content to the return string before calling iconv() (just like when E2BIG happens during the "normal" use of iconv(), see above). This adds the (slight, I'd guess) overhead of an additional call to sv_catpvn(), but it makes the code much cleaner. Note: Since we currently don't return incomplete conversion results in case of EINVAL and EILSEQ, we don't have to care about the shift state there. If we did return the results in these cases, we'd also have to reset the shift state there. */ sv_catpvn(perl_str, obuf, l_obuf - outbytesleft); ocursor = obuf; outbytesleft = l_obuf; if((ret = iconv(obj->handle, NULL, NULL, &ocursor, &outbytesleft)) == (size_t) -1) { croak("iconv error (while trying to reset shift state): %s", strerror(errno)); Safefree(obuf); return(&PL_sv_undef); } /* Copy the converted bytes to the return string, and free the output buffer */ sv_catpvn(perl_str, obuf, l_obuf - outbytesleft); Safefree(obuf); /* Perl malloc */ return perl_str; } typedef struct tiobj Text__Iconv; /*****************************************************************************/ /* Perl interface */ #ifndef PERL_UNUSED_VAR # define PERL_UNUSED_VAR(var) if (0) var = var #endif #line 225 "Iconv.c" XS(XS_Text__Iconv_raise_error); /* prototype to pass -Wmissing-prototypes */ XS(XS_Text__Iconv_raise_error) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif PERL_UNUSED_VAR(cv); /* -W */ { int RETVAL; dXSTARG; #line 218 "Iconv.xs" if (items > 0 && SvIOK(ST(0))) /* if called as function */ raise_error = SvIV(ST(0)); if (items > 1 && SvIOK(ST(1))) /* if called as class method */ raise_error = SvIV(ST(1)); RETVAL = raise_error; #line 245 "Iconv.c" XSprePUSH; PUSHi((IV)RETVAL); } XSRETURN(1); } XS(XS_Text__Iconv_new); /* prototype to pass -Wmissing-prototypes */ XS(XS_Text__Iconv_new) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 3) Perl_croak(aTHX_ "Usage: %s(%s)", "Text::Iconv::new", "self, fromcode, tocode"); PERL_UNUSED_VAR(cv); /* -W */ { char * fromcode = (char *)SvPV_nolen(ST(1)); char * tocode = (char *)SvPV_nolen(ST(2)); Text__Iconv * RETVAL; #line 231 "Iconv.xs" iconv_t handle; Text__Iconv *obj; if ((handle = iconv_open(tocode, fromcode)) == (iconv_t)-1) { switch(errno) { case ENOMEM: croak("Insufficient memory to initialize conversion: %s", strerror(errno)); case EINVAL: croak("Unsupported conversion from %s to %s: %s", fromcode, tocode, strerror(errno)); default: croak("Couldn't initialize conversion: %s", strerror(errno)); } } Newz(0, obj, 1, Text__Iconv); if (obj == NULL) { croak("Newz: %s", strerror(errno)); } obj->handle = handle; obj->retval = &PL_sv_undef; obj->raise_error = newSViv(0); sv_setsv(obj->raise_error, &PL_sv_undef); RETVAL = obj; #line 297 "Iconv.c" ST(0) = sv_newmortal(); sv_setref_pv(ST(0), "Text::IconvPtr", (void*)RETVAL); } XSRETURN(1); } XS(XS_Text__IconvPtr_convert); /* prototype to pass -Wmissing-prototypes */ XS(XS_Text__IconvPtr_convert) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 2) Perl_croak(aTHX_ "Usage: %s(%s)", "Text::IconvPtr::convert", "self, string"); PERL_UNUSED_VAR(cv); /* -W */ { Text__Iconv * self; SV * string = ST(1); SV * RETVAL; if (sv_derived_from(ST(0), "Text::IconvPtr")) { IV tmp = SvIV((SV*)SvRV(ST(0))); self = INT2PTR(Text__Iconv *,tmp); } else Perl_croak(aTHX_ "self is not of type Text::IconvPtr"); #line 270 "Iconv.xs" RETVAL = do_conv(self, string); #line 329 "Iconv.c" ST(0) = RETVAL; sv_2mortal(ST(0)); } XSRETURN(1); } XS(XS_Text__IconvPtr_retval); /* prototype to pass -Wmissing-prototypes */ XS(XS_Text__IconvPtr_retval) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 1) Perl_croak(aTHX_ "Usage: %s(%s)", "Text::IconvPtr::retval", "self"); PERL_UNUSED_VAR(cv); /* -W */ { Text__Iconv * self; SV * RETVAL; if (sv_derived_from(ST(0), "Text::IconvPtr")) { IV tmp = SvIV((SV*)SvRV(ST(0))); self = INT2PTR(Text__Iconv *,tmp); } else Perl_croak(aTHX_ "self is not of type Text::IconvPtr"); #line 278 "Iconv.xs" RETVAL = self->retval; #line 360 "Iconv.c" ST(0) = RETVAL; sv_2mortal(ST(0)); } XSRETURN(1); } XS(XS_Text__IconvPtr_raise_error); /* prototype to pass -Wmissing-prototypes */ XS(XS_Text__IconvPtr_raise_error) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items < 1) Perl_croak(aTHX_ "Usage: %s(%s)", "Text::IconvPtr::raise_error", "self, ..."); PERL_UNUSED_VAR(cv); /* -W */ PERL_UNUSED_VAR(ax); /* -Wall */ SP -= items; { Text__Iconv * self; SV * RETVAL; if (sv_derived_from(ST(0), "Text::IconvPtr")) { IV tmp = SvIV((SV*)SvRV(ST(0))); self = INT2PTR(Text__Iconv *,tmp); } else Perl_croak(aTHX_ "self is not of type Text::IconvPtr"); #line 286 "Iconv.xs" if (items > 1 && SvIOK(ST(1))) { sv_setiv(self->raise_error, SvIV(ST(1))); } XPUSHs(sv_mortalcopy(self->raise_error)); #line 397 "Iconv.c" PUTBACK; return; } } XS(XS_Text__IconvPtr_DESTROY); /* prototype to pass -Wmissing-prototypes */ XS(XS_Text__IconvPtr_DESTROY) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 1) Perl_croak(aTHX_ "Usage: %s(%s)", "Text::IconvPtr::DESTROY", "self"); PERL_UNUSED_VAR(cv); /* -W */ { Text__Iconv * self; if (SvROK(ST(0))) { IV tmp = SvIV((SV*)SvRV(ST(0))); self = INT2PTR(Text__Iconv *,tmp); } else Perl_croak(aTHX_ "self is not a reference"); #line 296 "Iconv.xs" /* printf("Now in Text::Iconv::DESTROY\n"); */ (void) iconv_close(self->handle); Safefree(self); #line 428 "Iconv.c" } XSRETURN_EMPTY; } #ifdef __cplusplus extern "C" #endif XS(boot_Text__Iconv); /* prototype to pass -Wmissing-prototypes */ XS(boot_Text__Iconv) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif char* file = __FILE__; PERL_UNUSED_VAR(cv); /* -W */ PERL_UNUSED_VAR(items); /* -W */ XS_VERSION_BOOTCHECK ; newXSproto("Text::Iconv::raise_error", XS_Text__Iconv_raise_error, file, ";@"); newXSproto("Text::Iconv::new", XS_Text__Iconv_new, file, "$$$"); newXSproto("Text::IconvPtr::convert", XS_Text__IconvPtr_convert, file, "$$"); newXSproto("Text::IconvPtr::retval", XS_Text__IconvPtr_retval, file, "$"); newXSproto("Text::IconvPtr::raise_error", XS_Text__IconvPtr_raise_error, file, "$;@"); newXSproto("Text::IconvPtr::DESTROY", XS_Text__IconvPtr_DESTROY, file, "$"); XSRETURN_YES; }