2 * Copyright (c) 1997-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: siconvtst.c,v 1.6 2002-12-10 10:59:28 adam Exp $
16 #include <yaz/yaz-util.h>
21 void convert (FILE *inf, yaz_iconv_t cd)
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);
51 r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
52 if (r == (size_t)(-1))
54 int e = yaz_iconv_error(cd);
55 if (e == YAZ_ICONV_EILSEQ)
57 fprintf (stderr, "invalid sequence\n");
60 else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
63 for (i = 0; i<inbytesleft; i++)
66 r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
67 if (r != CHUNK_IN - i)
71 fprintf (stderr, "yaziconv: error reading file\n");
77 fprintf (stderr, "invalid sequence\n");
84 else if (e == YAZ_ICONV_E2BIG) /* no more output space */
86 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
88 outbytesleft = CHUNK_OUT;
93 fprintf (stderr, "yaziconv: unknown error\n");
100 inbytesleft = CHUNK_IN;
102 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
104 outbytesleft = CHUNK_OUT;
111 int main (int argc, char **argv)
121 while ((ret = options ("vf:t:", argv, argc, &arg)) != -2)
126 inf = fopen (arg, "rb");
129 fprintf (stderr, "yaziconv: cannot open %s", arg);
143 fprintf (stderr, "yaziconv: Usage\n"
144 "siconv -f encoding -t encoding [-v] [file]\n");
150 fprintf (stderr, "yaziconv: -t encoding missing\n");
155 fprintf (stderr, "yaziconv: -f encoding missing\n");
158 cd = yaz_iconv_open (to, from);
161 fprintf (stderr, "yaziconv: unsupported encoding\n");
168 fprintf (stderr, "yaziconv: using %s\n",
169 yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
173 yaz_iconv_close (cd);