Lines 23-29
Link Here
|
23 |
|
23 |
|
24 |
#include <stdlib.h> |
24 |
#include <stdlib.h> |
25 |
|
25 |
|
|
|
26 |
#include <string.h> |
27 |
|
26 |
#include "cxml.h" |
28 |
#include "cxml.h" |
|
|
29 |
/** |
30 |
* xmlIconvWrapper: |
31 |
* @cd: iconv converter data structure |
32 |
* @out: a pointer to an array of bytes to store the result |
33 |
* @outlen: the length of @out |
34 |
* @in: a pointer to an array of ISO Latin 1 chars |
35 |
* @inlen: the length of @in |
36 |
* |
37 |
* Returns 0 if success, or |
38 |
* -1 by lack of space, or |
39 |
* -2 if the transcoding fails (for *in is not valid utf8 string or |
40 |
* the result of transformation can't fit into the encoding we want), or |
41 |
* -3 if there the last byte can't form a single output char. |
42 |
* |
43 |
* The value of @inlen after return is the number of octets consumed |
44 |
* as the return value is positive, else unpredictable. |
45 |
* The value of @outlen after return is the number of ocetes consumed. |
46 |
*/ |
47 |
static int |
48 |
xmlIconvWrapper(iconv_t cd, |
49 |
unsigned char *out, int *outlen, |
50 |
const unsigned char *in, int *inlen) { |
51 |
|
52 |
size_t icv_inlen = *inlen, icv_outlen = *outlen; |
53 |
const char *icv_in = (const char *) in; |
54 |
char *icv_out = (char *) out; |
55 |
int ret; |
56 |
|
57 |
ret = iconv(cd, (char **) &icv_in, &icv_inlen, &icv_out, &icv_outlen); |
58 |
if (in != NULL) { |
59 |
*inlen -= icv_inlen; |
60 |
*outlen -= icv_outlen; |
61 |
} else { |
62 |
*inlen = 0; |
63 |
*outlen = 0; |
64 |
} |
65 |
if ((icv_inlen != 0) || (ret == -1)) { |
66 |
#ifdef EILSEQ |
67 |
if (errno == EILSEQ) { |
68 |
return -2; |
69 |
} else |
70 |
#endif |
71 |
#ifdef E2BIG |
72 |
if (errno == E2BIG) { |
73 |
return -1; |
74 |
} else |
75 |
#endif |
76 |
#ifdef EINVAL |
77 |
if (errno == EINVAL) { |
78 |
return -3; |
79 |
} else |
80 |
#endif |
81 |
{ |
82 |
return -3; |
83 |
} |
84 |
} |
85 |
return 0; |
86 |
} |
27 |
|
87 |
|
28 |
/** */ |
88 |
/** */ |
29 |
CXml::CXml() |
89 |
CXml::CXml() |
Lines 34-39
Link Here
|
34 |
{ |
94 |
{ |
35 |
xmlInitParser(); |
95 |
xmlInitParser(); |
36 |
} |
96 |
} |
|
|
97 |
|
98 |
enc=strrchr(getenv("LANG"),'.'); |
99 |
enc++; |
37 |
} |
100 |
} |
38 |
|
101 |
|
39 |
/** */ |
102 |
/** */ |
Lines 134-140
Link Here
|
134 |
CString r = ""; |
197 |
CString r = ""; |
135 |
unsigned char *b; |
198 |
unsigned char *b; |
136 |
int inlen,outlen,res; |
199 |
int inlen,outlen,res; |
137 |
|
200 |
iconv_t cd; |
|
|
201 |
|
138 |
if ( (s == 0) || (len <= 0) ) |
202 |
if ( (s == 0) || (len <= 0) ) |
139 |
{ |
203 |
{ |
140 |
return ""; |
204 |
return ""; |
Lines 148-155
Link Here
|
148 |
if ( b == 0 ) |
212 |
if ( b == 0 ) |
149 |
return r; |
213 |
return r; |
150 |
|
214 |
|
151 |
res = UTF8Toisolat1( b, &outlen, (unsigned char*)s, &inlen ); |
215 |
cd = iconv_open(enc,"UTF-8"); |
152 |
|
216 |
res = xmlIconvWrapper(cd, b, &outlen, (unsigned char*)s, &inlen); |
|
|
217 |
iconv_close(cd); |
218 |
|
153 |
if ( res == -2 ) |
219 |
if ( res == -2 ) |
154 |
printf("UTF8Toisolat1 transcoding fail: '%s'\n",s); |
220 |
printf("UTF8Toisolat1 transcoding fail: '%s'\n",s); |
155 |
else if ( res == -1 ) |
221 |
else if ( res == -1 ) |
Lines 170-176
Link Here
|
170 |
CString r = ""; |
236 |
CString r = ""; |
171 |
unsigned char *b; |
237 |
unsigned char *b; |
172 |
int inlen,outlen,res; |
238 |
int inlen,outlen,res; |
173 |
|
239 |
iconv_t cd; |
|
|
240 |
|
174 |
if ( (s == 0) || (len <= 0) ) |
241 |
if ( (s == 0) || (len <= 0) ) |
175 |
{ |
242 |
{ |
176 |
return ""; |
243 |
return ""; |
Lines 183-195
Link Here
|
183 |
|
250 |
|
184 |
if ( b == 0 ) |
251 |
if ( b == 0 ) |
185 |
return r; |
252 |
return r; |
186 |
|
253 |
|
187 |
res = isolat1ToUTF8( b, &outlen, (unsigned char*)s, &inlen ); |
254 |
cd = iconv_open("UTF-8",enc); |
188 |
|
255 |
res = xmlIconvWrapper(cd, b, &outlen, (unsigned char*)s, &inlen); |
|
|
256 |
iconv_close(cd); |
189 |
if ( res >= 0 ) |
257 |
if ( res >= 0 ) |
190 |
r = (char*)b; |
258 |
r = (char*)b; |
191 |
else |
259 |
else |
192 |
printf("isolat1ToUTF8 fail: '%s'\n",s); |
260 |
printf("isolat1ToUTF8 fail: %s with error %d \n",s,res); |
193 |
|
261 |
|
194 |
free(b); |
262 |
free(b); |
195 |
|
263 |
|
Lines 346-348
Link Here
|
346 |
else |
414 |
else |
347 |
return FALSE; |
415 |
return FALSE; |
348 |
} |
416 |
} |
|
|
417 |
|