1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
14 #include <yaz/yaz-util.h>
19 void write_out(const char *b0, const char *b1)
24 if (fwrite(b0, 1, sz, stdout) != sz)
26 fprintf(stderr, "yaz-iconv: write failed\n");
32 void convert(FILE *inf, yaz_iconv_t cd, int verbose)
34 char inbuf0[CHUNK_IN], *inbuf = inbuf0;
35 char outbuf0[CHUNK_OUT], *outbuf = outbuf0;
36 size_t inbytesleft = CHUNK_IN;
37 size_t outbytesleft = CHUNK_OUT;
45 r = fread(inbuf, 1, inbytesleft, inf);
50 fprintf(stderr, "yaz-iconv: error reading file\n");
55 write_out(outbuf0, outbuf);
57 outbytesleft = CHUNK_OUT;
58 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
59 write_out(outbuf0, outbuf);
67 fprintf(stderr, "yaz_iconv: inbytesleft=%ld outbytesleft=%ld\n",
68 (long) inbytesleft, (long) outbytesleft);
71 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
72 if (r == (size_t)(-1))
74 int e = yaz_iconv_error(cd);
75 if (e == YAZ_ICONV_EILSEQ)
77 fprintf(stderr, "invalid sequence\n");
80 else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
83 for (i = 0; i<inbytesleft; i++)
86 r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
87 if (r != CHUNK_IN - i)
91 fprintf(stderr, "yaz-iconv: error reading file\n");
97 fprintf(stderr, "invalid sequence due to missing input\n");
104 else if (e == YAZ_ICONV_E2BIG) /* no more output space */
106 write_out(outbuf0, outbuf);
108 outbytesleft = CHUNK_OUT;
113 fprintf(stderr, "yaz-iconv: unknown error\n");
120 inbytesleft = CHUNK_IN;
122 write_out(outbuf0, outbuf);
124 outbytesleft = CHUNK_OUT;
131 int main(int argc, char **argv)
141 while ((ret = options("vf:t:", argv, argc, &arg)) != -2)
146 inf = fopen(arg, "rb");
149 fprintf(stderr, "yaz-iconv: cannot open %s", arg);
163 fprintf(stderr, "yaz-iconv: Usage\n"
164 "yaziconv -f encoding -t encoding [-v] [file]\n");
170 fprintf(stderr, "yaz-iconv: -t encoding missing\n");
175 fprintf(stderr, "yaz-iconv: -f encoding missing\n");
178 cd = yaz_iconv_open(to, from);
181 fprintf(stderr, "yaz-iconv: unsupported encoding\n");
188 fprintf(stderr, "yaz-iconv: using %s\n",
189 yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
192 convert(inf, cd, verbose);
199 * c-file-style: "Stroustrup"
200 * indent-tabs-mode: nil
202 * vim: shiftwidth=4 tabstop=8 expandtab