2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: yaziconv.c,v 1.4 2005-06-25 15:46:07 adam Exp $
17 #include <yaz/yaz-util.h>
22 void convert (FILE *inf, yaz_iconv_t cd, int verbose)
24 char inbuf0[CHUNK_IN], *inbuf = inbuf0;
25 char outbuf0[CHUNK_OUT], *outbuf = outbuf0;
26 size_t inbytesleft = CHUNK_IN;
27 size_t outbytesleft = CHUNK_OUT;
35 r = fread (inbuf, 1, inbytesleft, inf);
40 fprintf (stderr, "yaziconv: error reading file\n");
45 if (outbuf != outbuf0)
46 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
54 fprintf (stderr, "yaz_iconv: inbytesleft=%d outbytesleft=%d\n",
55 inbytesleft, outbytesleft);
58 r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
59 if (r == (size_t)(-1))
61 int e = yaz_iconv_error(cd);
62 if (e == YAZ_ICONV_EILSEQ)
64 fprintf (stderr, "invalid sequence\n");
67 else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
70 for (i = 0; i<inbytesleft; i++)
73 r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
74 if (r != CHUNK_IN - i)
78 fprintf (stderr, "yaziconv: error reading file\n");
84 fprintf (stderr, "invalid sequence\n");
91 else if (e == YAZ_ICONV_E2BIG) /* no more output space */
93 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
95 outbytesleft = CHUNK_OUT;
100 fprintf (stderr, "yaziconv: unknown error\n");
107 inbytesleft = CHUNK_IN;
109 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
111 outbytesleft = CHUNK_OUT;
118 int main (int argc, char **argv)
128 while ((ret = options ("vf:t:", argv, argc, &arg)) != -2)
133 inf = fopen (arg, "rb");
136 fprintf (stderr, "yaziconv: cannot open %s", arg);
150 fprintf (stderr, "yaziconv: Usage\n"
151 "siconv -f encoding -t encoding [-v] [file]\n");
157 fprintf (stderr, "yaziconv: -t encoding missing\n");
162 fprintf (stderr, "yaziconv: -f encoding missing\n");
165 cd = yaz_iconv_open (to, from);
168 fprintf (stderr, "yaziconv: unsupported encoding\n");
175 fprintf (stderr, "yaziconv: using %s\n",
176 yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
179 convert (inf, cd, verbose);
180 yaz_iconv_close (cd);
186 * indent-tabs-mode: nil
188 * vim: shiftwidth=4 tabstop=8 expandtab