]> mj.ucw.cz Git - libucw.git/blob - charset/ucw-cs2cs.c
tableprinter: first shot on update of internals
[libucw.git] / charset / ucw-cs2cs.c
1 /*
2  *      Simple character set convertor
3  *
4  *      (c) 1998 Pavel Machek <pavel@ucw.cz>
5  *      (c) 2003 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU General Public License.
9  */
10
11 #include <ucw/lib.h>
12 #include <charset/charconv.h>
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <unistd.h>
17
18 #ifdef TEST
19 #define BUFSIZE 13
20 #else
21 #define BUFSIZE 4096
22 #endif
23
24 int
25 main(int argc, char **argv)
26 {
27   struct conv_context ctxt;
28   int ch_from, ch_to, n, flags;
29   char inbuf[BUFSIZE], outbuf[BUFSIZE];
30
31   if (argc != 3)
32     die("ucw-cs2cs in-charset out-charset");
33   conv_init(&ctxt);
34   ch_from = find_charset_by_name(argv[1]);
35   if (ch_from < 0)
36     die("Unknown charset %s", argv[1]);
37   ch_to = find_charset_by_name(argv[2]);
38   if (ch_to < 0)
39     die("Unknown charset %s", argv[2]);
40
41   conv_set_charset(&ctxt, ch_from, ch_to);
42   while ((n = read(0, inbuf, sizeof(inbuf))) > 0)
43     {
44       ctxt.source = inbuf;
45       ctxt.source_end = inbuf + n;
46       ctxt.dest = ctxt.dest_start = outbuf;
47       ctxt.dest_end = outbuf + sizeof(outbuf);
48       do
49         {
50           flags = conv_run(&ctxt);
51           if (flags & (CONV_SOURCE_END | CONV_DEST_END))
52             {
53               int w = write(1, ctxt.dest_start, ctxt.dest - ctxt.dest_start);
54               if (w < 0)
55                 die("write error: %m");
56               ctxt.dest = outbuf;
57             }
58         }
59       while (! (flags & CONV_SOURCE_END));
60     }
61   if (n < 0)
62     die("read error: %m");
63   return 0;
64 }