Iconv.xs

filter: Statementstmt 46.9% 39 / 83 Branchbran 25.9% 14 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 38.6% 53 / 137 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Text/Iconv.pm »
1 /* $Id: Iconv.xs,v 1.15 2007/10/17 14:06:22 mxp Exp $ */
2 /* XSUB for Perl module Text::Iconv */
3 /* Copyright (c) 2007 Michael Piotrowski */
4  
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 #include "EXTERN.h"
9 #include "perl.h"
10 #include "XSUB.h"
11 #ifdef __cplusplus
12 }
13 #endif
14  
15 #include <iconv.h>
16  
17 /*****************************************************************************/
18 /* This struct represents a Text::Iconv object */
19  
20 struct tiobj
21 {
22 iconv_t handle; /* iconv handle (returned by iconv_open()) */
23 SV *retval; /* iconv() return value (according to the Single UNIX
24 Specification, "the number of non-identical
25 conversions performed") */
26 SV *raise_error; /* Per-object flag controlling whether exceptions
27 are to be thrown */
28 };
29  
30 /*****************************************************************************/
31  
32 static int
33 0 not_here(s)
34 char *s;
35 {
36 0 croak("%s not implemented on this architecture", s);
37 return -1;
38 }
39  
40 static int raise_error = 0;
41  
42 /* Macro for checking when to throw an exception for use in the
43 do_conv() function. The logic is: Throw an exception IF
44 obj->raise_error is undef AND raise_error is true OR IF
45 obj->raise_error is true */
46 #define RAISE_ERROR_P (!SvOK(obj->raise_error) && raise_error) \
47 || SvTRUE(obj->raise_error)
48  
49 24 SV *do_conv(struct tiobj *obj, SV *string)
50 {
51 char *ibuf; /* char* to the content of SV *string */
52 char *obuf; /* temporary output buffer */
53 size_t inbytesleft; /* no. of bytes left to convert; initially
54 this is the length of the input string,
55 and 0 when the conversion has finished */
56 size_t outbytesleft; /* no. of bytes in the output buffer */
57 size_t l_obuf; /* length of the output buffer */
58 char *icursor; /* current position in the input buffer */
59 /* The Single UNIX Specification (version 1 and version 2), as well
60 as the HP-UX documentation from which the XPG iconv specs are
61 derived, are unclear about the type of the second argument to
62 iconv() (here called icursor): The manpages say const char **,
63 while the header files say char **. */
64 char *ocursor; /* current position in the output buffer */
65 size_t ret; /* iconv() return value */
66 SV *perl_str; /* Perl return string */
67  
68 /* Check if the input string is actually `defined'; otherwise
69 simply return undef. This is not considered an error. */
70  
71 24 ▶ if (! SvOK(string))
Branch: 1 branch
BranchTrueFalseTotal
if (! SvOK(string)) 0 24 24
72 {
73 0 return(&PL_sv_undef);
74 }
75
76 24 perl_str = newSVpv("", 0);
77  
78 /* Get length of input string. That's why we take an SV* instead
79 of a char*: This way we can convert UCS-2 strings because we
80 know their length. */
81  
82 24 inbytesleft = SvCUR(string);
83 24 ibuf = SvPV(string, inbytesleft);
84
85 /* Calculate approximate amount of memory needed for the temporary
86 output buffer and reserve the memory. The idea is to choose it
87 large enough from the beginning to reduce the number of copy
88 operations when converting from a single-byte to a multibyte
89 encoding. */
90
91 24 ▶ if(inbytesleft <= MB_LEN_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if(inbytesleft <= MB_LEN_MAX) 24 0 24
92 {
93 24 outbytesleft = MB_LEN_MAX + 1;
94 }
95 else
96 {
97 0 outbytesleft = 2 * inbytesleft;
98 }
99  
100 24 l_obuf = outbytesleft;
101  
102 24 New(0, obuf, outbytesleft, char); /* Perl malloc */
103 24 ▶ if (obuf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (obuf == NULL) 0 24 24
104 {
105 0 croak("New: %s", strerror(errno));
106 }
107  
108 /**************************************************************************/
109  
110 24 icursor = ibuf;
111 24 ocursor = obuf;
112  
113 /**************************************************************************/
114
115 48 ▶ while(inbytesleft != 0)
Branch: 1 branch
BranchTrueFalseTotal
while(inbytesleft != 0) 24 24 48
116 {
117 #if (defined(__hpux) || defined(__linux) || defined(VMS)) && ! defined(_LIBICONV_VERSION)
118 /* Even in HP-UX 11.00, documentation and header files do not agree */
119 /* glibc doesn't seem care too much about standards */
120 24 ret = iconv(obj->handle, &icursor, &inbytesleft,
121 &ocursor, &outbytesleft);
122 #else
123 ret = iconv(obj->handle, (const char **)&icursor, &inbytesleft,
124 &ocursor, &outbytesleft);
125 #endif
126  
127 24 ▶ if(ret == (size_t) -1)
Branch: 1 branch
BranchTrueFalseTotal
if(ret == (size_t) -1) 0 24 24
128 {
129 0 obj->retval = &PL_sv_undef;
130  
131 0 switch(errno)
132 {
133 0 case EILSEQ:
134 /* Stop conversion if input character encountered which
135 does not belong to the input char set */
136 0 ▶ if (RAISE_ERROR_P)
Branch: 3 branches
BranchTrueFalseTotal
if (RAISE_ERROR_P) 0 0 0
if (RAISE_ERROR_P) 0 0 0
if (RAISE_ERROR_P) 0 0 0
137 0 croak("Character not from source char set: %s",
138 strerror(errno));
139 0 Safefree(obuf);
140 /* INIT_SHIFT_STATE(obj->handle, ocursor, outbytesleft); */
141 0 return(&PL_sv_undef);
142 0 case EINVAL:
143 /* Stop conversion if we encounter an incomplete
144 character or shift sequence */
145 0 ▶ if (RAISE_ERROR_P)
Branch: 3 branches
BranchTrueFalseTotal
if (RAISE_ERROR_P) 0 0 0
if (RAISE_ERROR_P) 0 0 0
if (RAISE_ERROR_P) 0 0 0
146 0 croak("Incomplete character or shift sequence: %s",
147 strerror(errno));
148 0 Safefree(obuf);
149 0 return(&PL_sv_undef);
150 0 case E2BIG:
151 /* fprintf(stdout, "%s\n", obuf); */
152  
153 /* If the output buffer is not large enough, copy the
154 converted bytes to the return string, reset the
155 output buffer and continue */
156 0 sv_catpvn(perl_str, obuf, l_obuf - outbytesleft);
157 0 ocursor = obuf;
158 0 outbytesleft = l_obuf;
159 0 break;
160 0 default:
161 0 ▶ if (RAISE_ERROR_P)
Branch: 3 branches
BranchTrueFalseTotal
if (RAISE_ERROR_P) 0 0 0
if (RAISE_ERROR_P) 0 0 0
if (RAISE_ERROR_P) 0 0 0
162 0 croak("iconv error: %s", strerror(errno));
163 0 Safefree(obuf);
164 0 return(&PL_sv_undef);
165 }
166 }
167 else
168 {
169 24 obj->retval = newSViv(ret);
170 }
171 }
172  
173 /* For state-dependent encodings, place conversion descriptor into
174 initial shift state and place the byte sequence to change the
175 output buffer to its initial shift state.
176  
177 The only (documented) error for this use of iconv() is E2BIG;
178 here it could happen only if the output buffer has no more room
179 for the reset sequence. We can simply prevent this case by
180 copying its content to the return string before calling iconv()
181 (just like when E2BIG happens during the "normal" use of
182 iconv(), see above). This adds the (slight, I'd guess) overhead
183 of an additional call to sv_catpvn(), but it makes the code much
184 cleaner.
185  
186 Note: Since we currently don't return incomplete conversion
187 results in case of EINVAL and EILSEQ, we don't have to care
188 about the shift state there. If we did return the results in
189 these cases, we'd also have to reset the shift state there.
190 */
191  
192 24 sv_catpvn(perl_str, obuf, l_obuf - outbytesleft);
193 24 ocursor = obuf;
194 24 outbytesleft = l_obuf;
195  
196 24 ▶ if((ret = iconv(obj->handle, NULL, NULL, &ocursor, &outbytesleft))
Branch: 1 branch
BranchTrueFalseTotal
if((ret = iconv(obj->handle, NULL, NULL, &ocursor, &outbytesleft)) 0 24 24
197 == (size_t) -1)
198 {
199 0 croak("iconv error (while trying to reset shift state): %s",
200 strerror(errno));
201 Safefree(obuf);
202 return(&PL_sv_undef);
203 }
204  
205 /* Copy the converted bytes to the return string, and free the
206 output buffer */
207
208 24 sv_catpvn(perl_str, obuf, l_obuf - outbytesleft);
209 24 Safefree(obuf); /* Perl malloc */
210  
211 24 return perl_str;
212 }
213  
214 /* */
215  
216 #if _LIBICONV_VERSION >= 0x0109
217 int do_iconvctl(struct tiobj *obj, int request, void *arg)
218 {
219 return iconvctl(obj->handle, request, arg);
220 }
221 #endif
222  
223 typedef struct tiobj Text__Iconv;
224  
225 /*****************************************************************************/
226 /* Perl interface */
227  
228 MODULE = Text::Iconv PACKAGE = Text::Iconv
229  
230 PROTOTYPES: ENABLE
231  
232 int
233 raise_error(...)
234 CODE:
235 1 ▶ if (items > 0 && SvIOK(ST(0))) /* if called as function */
Branch: 2 branches
BranchTrueFalseTotal
if (items > 0 && SvIOK(ST(0))) /* if called as function */ 1 0 1
if (items > 0 && SvIOK(ST(0))) /* if called as function */ 0 1 1
236 0 raise_error = SvIV(ST(0));
237 1 ▶ if (items > 1 && SvIOK(ST(1))) /* if called as class method */
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvIOK(ST(1))) /* if called as class method */ 1 0 1
if (items > 1 && SvIOK(ST(1))) /* if called as class method */ 1 0 1
238 1 raise_error = SvIV(ST(1));
239 1 ▶ RETVAL = raise_error;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = raise_error; 0 1 1
240 OUTPUT:
241 RETVAL
242  
243 Text::Iconv *
244 new(self, fromcode, tocode)
245 char *fromcode
246 char *tocode
247 CODE:
248 iconv_t handle;
249 Text__Iconv *obj;
250  
251 24 ▶ if ((handle = iconv_open(tocode, fromcode)) == (iconv_t)-1)
Branch: 1 branch
BranchTrueFalseTotal
if ((handle = iconv_open(tocode, fromcode)) == (iconv_t)-1) 0 24 24
252 {
253 0 switch(errno)
254 {
255 0 case ENOMEM:
256 0 croak("Insufficient memory to initialize conversion: %s",
257 strerror(errno));
258 0 case EINVAL:
259 0 croak("Unsupported conversion from %s to %s: %s",
260 fromcode, tocode, strerror(errno));
261 0 default:
262 0 croak("Couldn't initialize conversion: %s", strerror(errno));
263 }
264 }
265  
266 24 Newz(0, obj, 1, Text__Iconv);
267 24 ▶ if (obj == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (obj == NULL) 0 24 24
268 {
269 0 croak("Newz: %s", strerror(errno));
270 }
271  
272 24 obj->handle = handle;
273 24 obj->retval = &PL_sv_undef;
274 24 obj->raise_error = newSViv(0);
275 24 sv_setsv(obj->raise_error, &PL_sv_undef);
276 24 RETVAL = obj;
277 OUTPUT:
278 RETVAL
279  
280 MODULE = Text::Iconv PACKAGE = Text::IconvPtr PREFIX = ti_
281  
282 SV *
283 ti_convert(self, string)
284 Text::Iconv *self
285 SV *string
286 CODE:
287 24 RETVAL = do_conv(self, string);
288 OUTPUT:
289 RETVAL
290  
291 SV *
292 ti_retval(self)
293 Text::Iconv *self
294 CODE:
295 24 RETVAL = self->retval;
296 OUTPUT:
297 RETVAL
298  
299 SV *
300 ti_raise_error(self, ...)
301 Text::Iconv *self
302 PPCODE:
303 0 ▶ if (items > 1 && SvIOK(ST(1)))
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvIOK(ST(1))) 0 0 0
if (items > 1 && SvIOK(ST(1))) 0 0 0
304 {
305 0 sv_setiv(self->raise_error, SvIV(ST(1)));
306 }
307 0 ▶ XPUSHs(sv_mortalcopy(self->raise_error));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_mortalcopy(self->raise_error)); 0 0 0
308  
309 #if _LIBICONV_VERSION >= 0x0109
310  
311 int
312 ti_get_attr(self, request)
313 Text::Iconv *self
314 char *request;
315 PREINIT:
316 int reqno;
317 int arg;
318 int err;
319 CODE:
320 if (strEQ(request, "trivialp"))
321 reqno = ICONV_TRIVIALP;
322 else if (strEQ(request, "transliterate"))
323 reqno = ICONV_GET_TRANSLITERATE;
324 else if (strEQ(request, "discard_ilseq"))
325 reqno = ICONV_GET_DISCARD_ILSEQ;
326 else
327 reqno = -1;
328  
329 err = do_iconvctl(self, reqno, &arg);
330  
331 if (err < 0)
332 RETVAL = err;
333 else
334 RETVAL = arg;
335 OUTPUT:
336 RETVAL
337  
338 #else
339  
340 int
341 ti_get_attr(self, request)
342 Text::Iconv *self
343 char *request;
344 CODE:
345 0 not_here("iconvctl (needed for get_attr())");
346 0 ▶ RETVAL = -1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = -1; 0 0 0
347 OUTPUT:
348 RETVAL
349  
350 #endif
351  
352 #if _LIBICONV_VERSION >= 0x0109
353  
354 int
355 ti_set_attr(self, request, arg)
356 Text::Iconv *self
357 char *request;
358 int arg;
359 PREINIT:
360 int reqno;
361 int err;
362 CODE:
363 if (strEQ(request, "transliterate"))
364 reqno = ICONV_SET_TRANSLITERATE;
365 else if (strEQ(request, "discard_ilseq"))
366 reqno = ICONV_SET_DISCARD_ILSEQ;
367 else
368 reqno = -1;
369  
370 err = do_iconvctl(self, reqno, &arg);
371  
372 if (err < 0)
373 RETVAL = err;
374 else
375 RETVAL = arg;
376 OUTPUT:
377 RETVAL
378  
379 #else
380  
381 int
382 ti_set_attr(self, request, arg)
383 Text::Iconv *self
384 char *request;
385 int arg;
386 CODE:
387 0 not_here("iconvctl (needed for set_attr())");
388 0 ▶ RETVAL = -1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = -1; 0 0 0
389 OUTPUT:
390 RETVAL
391  
392 #endif
393  
394 void
395 ti_DESTROY(self)
396 Text::Iconv * self
397 CODE:
398 /* printf("Now in Text::Iconv::DESTROY\n"); */
399 24 (void) iconv_close(self->handle);
400 24 Safefree(self);
↑ Summary blib/lib/Text/Iconv.pm »