2 * Copyright (c) 1997-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: yaziconv.c,v 1.1 2003-04-23 20:34:08 adam Exp $
16 #include <yaz/yaz-util.h>
21 void convert (FILE *inf, yaz_iconv_t cd, int verbose)
23 char inbuf0[CHUNK_IN], *inbuf = inbuf0;
24 char outbuf0[CHUNK_OUT], *outbuf = outbuf0;
25 size_t inbytesleft = CHUNK_IN;
26 size_t outbytesleft = CHUNK_OUT;
34 r = fread (inbuf, 1, inbytesleft, inf);
39 fprintf (stderr, "yaziconv: error reading file\n");
44 if (outbuf != outbuf0)
45 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
53 fprintf (stderr, "yaz_iconv: inbytesleft=%d outbytesleft=%d\n",
54 inbytesleft, outbytesleft);
57 r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
58 if (r == (size_t)(-1))
60 int e = yaz_iconv_error(cd);
61 if (e == YAZ_ICONV_EILSEQ)
63 fprintf (stderr, "invalid sequence\n");
66 else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
69 for (i = 0; i<inbytesleft; i++)
72 r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
73 if (r != CHUNK_IN - i)
77 fprintf (stderr, "yaziconv: error reading file\n");
83 fprintf (stderr, "invalid sequence\n");
90 else if (e == YAZ_ICONV_E2BIG) /* no more output space */
92 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
94 outbytesleft = CHUNK_OUT;
99 fprintf (stderr, "yaziconv: unknown error\n");
106 inbytesleft = CHUNK_IN;
108 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
110 outbytesleft = CHUNK_OUT;
117 int main (int argc, char **argv)
127 while ((ret = options ("vf:t:", argv, argc, &arg)) != -2)
132 inf = fopen (arg, "rb");
135 fprintf (stderr, "yaziconv: cannot open %s", arg);
149 fprintf (stderr, "yaziconv: Usage\n"
150 "siconv -f encoding -t encoding [-v] [file]\n");
156 fprintf (stderr, "yaziconv: -t encoding missing\n");
161 fprintf (stderr, "yaziconv: -f encoding missing\n");
164 cd = yaz_iconv_open (to, from);
167 fprintf (stderr, "yaziconv: unsupported encoding\n");
174 fprintf (stderr, "yaziconv: using %s\n",
175 yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
178 convert (inf, cd, verbose);
179 yaz_iconv_close (cd);