1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
15 #include <yaz/yaz-util.h>
20 void convert (FILE *inf, yaz_iconv_t cd, int verbose)
22 char inbuf0[CHUNK_IN], *inbuf = inbuf0;
23 char outbuf0[CHUNK_OUT], *outbuf = outbuf0;
24 size_t inbytesleft = CHUNK_IN;
25 size_t outbytesleft = CHUNK_OUT;
33 r = fread (inbuf, 1, inbytesleft, inf);
38 fprintf (stderr, "yaziconv: error reading file\n");
43 if (outbuf != outbuf0)
44 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
46 outbytesleft = CHUNK_OUT;
47 r = yaz_iconv (cd, 0, 0, &outbuf, &outbytesleft);
48 if (outbuf != outbuf0)
49 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
57 fprintf (stderr, "yaz_iconv: inbytesleft=%ld outbytesleft=%ld\n",
58 (long) inbytesleft, (long) outbytesleft);
61 r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
62 if (r == (size_t)(-1))
64 int e = yaz_iconv_error(cd);
65 if (e == YAZ_ICONV_EILSEQ)
67 fprintf (stderr, "invalid sequence\n");
70 else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
73 for (i = 0; i<inbytesleft; i++)
76 r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
77 if (r != CHUNK_IN - i)
81 fprintf (stderr, "yaziconv: error reading file\n");
87 fprintf (stderr, "invalid sequence due to missing input\n");
94 else if (e == YAZ_ICONV_E2BIG) /* no more output space */
96 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
98 outbytesleft = CHUNK_OUT;
103 fprintf (stderr, "yaziconv: unknown error\n");
110 inbytesleft = CHUNK_IN;
112 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
114 outbytesleft = CHUNK_OUT;
121 int main (int argc, char **argv)
131 while ((ret = options ("vf:t:", argv, argc, &arg)) != -2)
136 inf = fopen (arg, "rb");
139 fprintf (stderr, "yaziconv: cannot open %s", arg);
153 fprintf (stderr, "yaziconv: Usage\n"
154 "yaziconv -f encoding -t encoding [-v] [file]\n");
160 fprintf (stderr, "yaziconv: -t encoding missing\n");
165 fprintf (stderr, "yaziconv: -f encoding missing\n");
168 cd = yaz_iconv_open (to, from);
171 fprintf (stderr, "yaziconv: unsupported encoding\n");
178 fprintf (stderr, "yaziconv: using %s\n",
179 yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
182 convert (inf, cd, verbose);
183 yaz_iconv_close (cd);
189 * indent-tabs-mode: nil
191 * vim: shiftwidth=4 tabstop=8 expandtab