2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: yaziconv.c,v 1.9 2007-05-25 16:19:46 marc 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);
48 outbytesleft = CHUNK_OUT;
49 r = yaz_iconv (cd, 0, 0, &outbuf, &outbytesleft);
50 if (outbuf != outbuf0)
51 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
59 fprintf (stderr, "yaz_iconv: inbytesleft=%ld outbytesleft=%ld\n",
60 (long) inbytesleft, (long) outbytesleft);
63 r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
64 if (r == (size_t)(-1))
66 int e = yaz_iconv_error(cd);
67 if (e == YAZ_ICONV_EILSEQ)
69 fprintf (stderr, "invalid sequence\n");
72 else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
75 for (i = 0; i<inbytesleft; i++)
78 r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
79 if (r != CHUNK_IN - i)
83 fprintf (stderr, "yaziconv: error reading file\n");
89 fprintf (stderr, "invalid sequence due to missing input\n");
96 else if (e == YAZ_ICONV_E2BIG) /* no more output space */
98 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
100 outbytesleft = CHUNK_OUT;
105 fprintf (stderr, "yaziconv: unknown error\n");
112 inbytesleft = CHUNK_IN;
114 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
116 outbytesleft = CHUNK_OUT;
123 int main (int argc, char **argv)
133 while ((ret = options ("vf:t:", argv, argc, &arg)) != -2)
138 inf = fopen (arg, "rb");
141 fprintf (stderr, "yaziconv: cannot open %s", arg);
155 fprintf (stderr, "yaziconv: Usage\n"
156 "yaziconv -f encoding -t encoding [-v] [file]\n");
162 fprintf (stderr, "yaziconv: -t encoding missing\n");
167 fprintf (stderr, "yaziconv: -f encoding missing\n");
170 cd = yaz_iconv_open (to, from);
173 fprintf (stderr, "yaziconv: unsupported encoding\n");
180 fprintf (stderr, "yaziconv: using %s\n",
181 yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
184 convert (inf, cd, verbose);
185 yaz_iconv_close (cd);
191 * indent-tabs-mode: nil
193 * vim: shiftwidth=4 tabstop=8 expandtab