2 * test.c - general libcharset test/demo program which converts
3 * between two arbitrary charsets.
10 #define lenof(x) ( sizeof((x)) / sizeof(*(x)) )
12 int main(int argc
, char **argv
)
15 charset_state instate
= CHARSET_INIT_STATE
;
16 charset_state outstate
= CHARSET_INIT_STATE
;
17 char inbuf
[256], outbuf
[256];
20 const wchar_t *midptr
;
21 int rdret
, inlen
, midlen
, inret
, midret
;
24 fprintf(stderr
, "usage: convcs <charset> <charset>\n");
28 srcset
= charset_from_localenc(argv
[1]);
29 if (srcset
== CS_NONE
) {
30 fprintf(stderr
, "unknown source charset '%s'\n", argv
[1]);
34 dstset
= charset_from_localenc(argv
[2]);
35 if (dstset
== CS_NONE
) {
36 fprintf(stderr
, "unknown destination charset '%s'\n", argv
[2]);
42 rdret
= fread(inbuf
, 1, sizeof(inbuf
), stdin
);
49 while ( (inret
= charset_to_unicode(&inptr
, &inlen
, midbuf
,
50 lenof(midbuf
), srcset
,
51 &instate
, NULL
, 0)) > 0) {
54 while ( (midret
= charset_from_unicode(&midptr
, &midlen
, outbuf
,
55 lenof(outbuf
), dstset
,
56 &outstate
, NULL
)) > 0) {
57 fwrite(outbuf
, 1, midret
, stdout
);
63 * Reset encoding state.
65 while ( (midret
= charset_from_unicode(NULL
, NULL
, outbuf
,
66 lenof(outbuf
), dstset
,
67 &outstate
, NULL
)) > 0) {
68 fwrite(outbuf
, 1, midret
, stdout
);