1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/* $Id: Iconv.xs,v 1.15 2007/10/17 14:06:22 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 <iconv.h>
/*****************************************************************************/
/* 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
not_here(s)
char *s;
{
croak("%s not implemented on this architecture", s);
return -1;
}
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;
}
/* */
#if _LIBICONV_VERSION >= 0x0109
int do_iconvctl(struct tiobj *obj, int request, void *arg)
{
return iconvctl(obj->handle, request, arg);
}
#endif
typedef struct tiobj Text__Iconv;
/*****************************************************************************/
/* Perl interface */
MODULE = Text::Iconv PACKAGE = Text::Iconv
PROTOTYPES: ENABLE
int
raise_error(...)
CODE:
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;
OUTPUT:
RETVAL
Text::Iconv *
new(self, fromcode, tocode)
char *fromcode
char *tocode
CODE:
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;
OUTPUT:
RETVAL
MODULE = Text::Iconv PACKAGE = Text::IconvPtr PREFIX = ti_
SV *
ti_convert(self, string)
Text::Iconv *self
SV *string
CODE:
RETVAL = do_conv(self, string);
OUTPUT:
RETVAL
SV *
ti_retval(self)
Text::Iconv *self
CODE:
RETVAL = self->retval;
OUTPUT:
RETVAL
SV *
ti_raise_error(self, ...)
Text::Iconv *self
PPCODE:
if (items > 1 && SvIOK(ST(1)))
{
sv_setiv(self->raise_error, SvIV(ST(1)));
}
XPUSHs(sv_mortalcopy(self->raise_error));
#if _LIBICONV_VERSION >= 0x0109
int
ti_get_attr(self, request)
Text::Iconv *self
char *request;
PREINIT:
int reqno;
int arg;
int err;
CODE:
if (strEQ(request, "trivialp"))
reqno = ICONV_TRIVIALP;
else if (strEQ(request, "transliterate"))
reqno = ICONV_GET_TRANSLITERATE;
else if (strEQ(request, "discard_ilseq"))
reqno = ICONV_GET_DISCARD_ILSEQ;
else
reqno = -1;
err = do_iconvctl(self, reqno, &arg);
if (err < 0)
RETVAL = err;
else
RETVAL = arg;
OUTPUT:
RETVAL
#else
int
ti_get_attr(self, request)
Text::Iconv *self
char *request;
CODE:
not_here("iconvctl (needed for get_attr())");
RETVAL = -1;
OUTPUT:
RETVAL
#endif
#if _LIBICONV_VERSION >= 0x0109
int
ti_set_attr(self, request, arg)
Text::Iconv *self
char *request;
int arg;
PREINIT:
int reqno;
int err;
CODE:
if (strEQ(request, "transliterate"))
reqno = ICONV_SET_TRANSLITERATE;
else if (strEQ(request, "discard_ilseq"))
reqno = ICONV_SET_DISCARD_ILSEQ;
else
reqno = -1;
err = do_iconvctl(self, reqno, &arg);
if (err < 0)
RETVAL = err;
else
RETVAL = arg;
OUTPUT:
RETVAL
#else
int
ti_set_attr(self, request, arg)
Text::Iconv *self
char *request;
int arg;
CODE:
not_here("iconvctl (needed for set_attr())");
RETVAL = -1;
OUTPUT:
RETVAL
#endif
void
ti_DESTROY(self)
Text::Iconv * self
CODE:
/* printf("Now in Text::Iconv::DESTROY\n"); */
(void) iconv_close(self->handle);
Safefree(self);
|