|
a/src/utils/transcode.cpp |
|
b/src/utils/transcode.cpp |
1 |
#ifndef lint
|
1 |
#ifndef lint
|
2 |
static char rcsid[] = "@(#$Id: transcode.cpp,v 1.1 2004-12-15 09:43:48 dockes Exp $ (C) 2004 J.F.Dockes";
|
2 |
static char rcsid[] = "@(#$Id: transcode.cpp,v 1.2 2004-12-15 15:00:37 dockes Exp $ (C) 2004 J.F.Dockes";
|
3 |
#endif
|
3 |
#endif
|
4 |
|
4 |
|
5 |
#ifndef TEST_TRANSCODE
|
5 |
#ifndef TEST_TRANSCODE
|
6 |
|
6 |
|
7 |
#include <errno.h>
|
7 |
#include <errno.h>
|
|
... |
|
... |
20 |
{
|
20 |
{
|
21 |
iconv_t ic;
|
21 |
iconv_t ic;
|
22 |
bool ret = false;
|
22 |
bool ret = false;
|
23 |
const int OBSIZ = 8192;
|
23 |
const int OBSIZ = 8192;
|
24 |
char obuf[OBSIZ], *op;
|
24 |
char obuf[OBSIZ], *op;
|
|
|
25 |
bool icopen = false;
|
25 |
|
26 |
|
26 |
out.erase();
|
27 |
out.erase();
|
27 |
size_t isiz = in.length();
|
28 |
size_t isiz = in.length();
|
28 |
out.reserve(isiz);
|
29 |
out.reserve(isiz);
|
29 |
const char *ip = in.c_str();
|
30 |
const char *ip = in.c_str();
|
|
... |
|
... |
31 |
if((ic = iconv_open(ocode.c_str(), icode.c_str())) == (iconv_t)-1) {
|
32 |
if((ic = iconv_open(ocode.c_str(), icode.c_str())) == (iconv_t)-1) {
|
32 |
out = string("iconv_open failed for ") + icode
|
33 |
out = string("iconv_open failed for ") + icode
|
33 |
+ " -> " + ocode;
|
34 |
+ " -> " + ocode;
|
34 |
goto error;
|
35 |
goto error;
|
35 |
}
|
36 |
}
|
36 |
|
37 |
icopen = true;
|
|
|
38 |
|
37 |
while (isiz > 0) {
|
39 |
while (isiz > 0) {
|
38 |
size_t osiz;
|
40 |
size_t osiz;
|
39 |
op = obuf;
|
41 |
op = obuf;
|
40 |
osiz = OBSIZ;
|
42 |
osiz = OBSIZ;
|
41 |
if(iconv(ic, &ip, &isiz, &op, &osiz) == -1 && errno != E2BIG){
|
43 |
if(iconv(ic, &ip, &isiz, &op, &osiz) == (size_t)-1 && errno != E2BIG){
|
42 |
out.erase();
|
44 |
out.erase();
|
43 |
out = string("iconv failed for ") + icode + " -> " + ocode +
|
45 |
out = string("iconv failed for ") + icode + " -> " + ocode +
|
44 |
" : " + strerror(errno);
|
46 |
" : " + strerror(errno);
|
45 |
goto error;
|
47 |
goto error;
|
46 |
}
|
48 |
}
|
|
... |
|
... |
51 |
out.erase();
|
53 |
out.erase();
|
52 |
out = string("iconv_close failed for ") + icode
|
54 |
out = string("iconv_close failed for ") + icode
|
53 |
+ " -> " + ocode;
|
55 |
+ " -> " + ocode;
|
54 |
goto error;
|
56 |
goto error;
|
55 |
}
|
57 |
}
|
|
|
58 |
icopen = false;
|
56 |
ret = true;
|
59 |
ret = true;
|
57 |
error:
|
60 |
error:
|
|
|
61 |
if (icopen)
|
|
|
62 |
iconv_close(ic);
|
58 |
return ret;
|
63 |
return ret;
|
59 |
}
|
64 |
}
|
60 |
|
65 |
|
61 |
|
66 |
|
62 |
#else
|
67 |
#else
|
|
... |
|
... |
98 |
int fd = open(ofilename.c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0666);
|
103 |
int fd = open(ofilename.c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0666);
|
99 |
if (fd < 0) {
|
104 |
if (fd < 0) {
|
100 |
perror("Open/create output");
|
105 |
perror("Open/create output");
|
101 |
exit(1);
|
106 |
exit(1);
|
102 |
}
|
107 |
}
|
103 |
if (write(fd, out.c_str(), out.length()) != out.length()) {
|
108 |
if (write(fd, out.c_str(), out.length()) != (int)out.length()) {
|
104 |
perror("write");
|
109 |
perror("write");
|
105 |
exit(1);
|
110 |
exit(1);
|
106 |
}
|
111 |
}
|
107 |
close(fd);
|
112 |
close(fd);
|
108 |
exit(0);
|
113 |
exit(0);
|