X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=util%2Fyaziconv.c;h=0d9d211f7863086a9d8691116b47c0e48ea24bd4;hp=45476145f5ea5deff32f34966b56f54f5e6f4027;hb=38d2e7b4d30b060d3b6dc85e0e3f57c4930c6313;hpb=4e82b831a8ac31e25b246a631acde354d8161688 diff --git a/util/yaziconv.c b/util/yaziconv.c index 4547614..0d9d211 100644 --- a/util/yaziconv.c +++ b/util/yaziconv.c @@ -1,8 +1,6 @@ -/* - * Copyright (C) 1995-2005, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2011 Index Data * See the file LICENSE for details. - * - * $Id: yaziconv.c,v 1.3 2005-01-17 13:09:43 adam Exp $ */ #if HAVE_CONFIG_H @@ -12,14 +10,26 @@ #include #include #include -#include #include #define CHUNK_IN 64 #define CHUNK_OUT 64 -void convert (FILE *inf, yaz_iconv_t cd, int verbose) +void write_out(const char *b0, const char *b1) +{ + size_t sz = b1 - b0; + if (sz) + { + if (fwrite(b0, 1, sz, stdout) != sz) + { + fprintf(stderr, "yaz-iconv: write failed\n"); + exit(8); + } + } +} + +void convert(FILE *inf, yaz_iconv_t cd, int verbose) { char inbuf0[CHUNK_IN], *inbuf = inbuf0; char outbuf0[CHUNK_OUT], *outbuf = outbuf0; @@ -32,18 +42,21 @@ void convert (FILE *inf, yaz_iconv_t cd, int verbose) size_t r; if (mustread) { - r = fread (inbuf, 1, inbytesleft, inf); + r = fread(inbuf, 1, inbytesleft, inf); if (inbytesleft != r) { if (ferror(inf)) { - fprintf (stderr, "yaziconv: error reading file\n"); - exit (6); + fprintf(stderr, "yaz-iconv: error reading file\n"); + exit(6); } if (r == 0) { - if (outbuf != outbuf0) - fwrite (outbuf0, 1, outbuf - outbuf0, stdout); + write_out(outbuf0, outbuf); + outbuf = outbuf0; + outbytesleft = CHUNK_OUT; + r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft); + write_out(outbuf0, outbuf); break; } inbytesleft = r; @@ -51,17 +64,17 @@ void convert (FILE *inf, yaz_iconv_t cd, int verbose) } if (verbose > 1) { - fprintf (stderr, "yaz_iconv: inbytesleft=%d outbytesleft=%d\n", - inbytesleft, outbytesleft); + fprintf(stderr, "yaz_iconv: inbytesleft=%ld outbytesleft=%ld\n", + (long) inbytesleft, (long) outbytesleft); } - r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); if (r == (size_t)(-1)) { int e = yaz_iconv_error(cd); if (e == YAZ_ICONV_EILSEQ) { - fprintf (stderr, "invalid sequence\n"); + fprintf(stderr, "invalid sequence\n"); return ; } else if (e == YAZ_ICONV_EINVAL) /* incomplete input */ @@ -75,13 +88,13 @@ void convert (FILE *inf, yaz_iconv_t cd, int verbose) { if (ferror(inf)) { - fprintf (stderr, "yaziconv: error reading file\n"); + fprintf(stderr, "yaz-iconv: error reading file\n"); exit(6); } } if (r == 0) { - fprintf (stderr, "invalid sequence\n"); + fprintf(stderr, "invalid sequence due to missing input\n"); return ; } inbytesleft += r; @@ -90,15 +103,15 @@ void convert (FILE *inf, yaz_iconv_t cd, int verbose) } else if (e == YAZ_ICONV_E2BIG) /* no more output space */ { - fwrite (outbuf0, 1, outbuf - outbuf0, stdout); + write_out(outbuf0, outbuf); outbuf = outbuf0; outbytesleft = CHUNK_OUT; mustread = 0; } else { - fprintf (stderr, "yaziconv: unknown error\n"); - exit (7); + fprintf(stderr, "yaz-iconv: unknown error\n"); + exit(7); } } else @@ -106,7 +119,7 @@ void convert (FILE *inf, yaz_iconv_t cd, int verbose) inbuf = inbuf0; inbytesleft = CHUNK_IN; - fwrite (outbuf0, 1, outbuf - outbuf0, stdout); + write_out(outbuf0, outbuf); outbuf = outbuf0; outbytesleft = CHUNK_OUT; @@ -115,7 +128,7 @@ void convert (FILE *inf, yaz_iconv_t cd, int verbose) } } -int main (int argc, char **argv) +int main(int argc, char **argv) { int ret; int verbose = 0; @@ -125,16 +138,16 @@ int main (int argc, char **argv) yaz_iconv_t cd; FILE *inf = stdin; - while ((ret = options ("vf:t:", argv, argc, &arg)) != -2) + while ((ret = options("vf:t:", argv, argc, &arg)) != -2) { switch (ret) { case 0: - inf = fopen (arg, "rb"); + inf = fopen(arg, "rb"); if (!inf) { - fprintf (stderr, "yaziconv: cannot open %s", arg); - exit (2); + fprintf(stderr, "yaz-iconv: cannot open %s", arg); + exit(2); } break; case 'f': @@ -147,36 +160,45 @@ int main (int argc, char **argv) verbose++; break; default: - fprintf (stderr, "yaziconv: Usage\n" - "siconv -f encoding -t encoding [-v] [file]\n"); + fprintf(stderr, "yaz-iconv: Usage\n" + "yaziconv -f encoding -t encoding [-v] [file]\n"); exit(1); } } if (!to) { - fprintf (stderr, "yaziconv: -t encoding missing\n"); - exit (3); + fprintf(stderr, "yaz-iconv: -t encoding missing\n"); + exit(3); } if (!from) { - fprintf (stderr, "yaziconv: -f encoding missing\n"); - exit (4); + fprintf(stderr, "yaz-iconv: -f encoding missing\n"); + exit(4); } - cd = yaz_iconv_open (to, from); + cd = yaz_iconv_open(to, from); if (!cd) { - fprintf (stderr, "yaziconv: unsupported encoding\n"); - exit (5); + fprintf(stderr, "yaz-iconv: unsupported encoding\n"); + exit(5); } else { if (verbose) { - fprintf (stderr, "yaziconv: using %s\n", - yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv"); + fprintf(stderr, "yaz-iconv: using %s\n", + yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv"); } } - convert (inf, cd, verbose); - yaz_iconv_close (cd); + convert(inf, cd, verbose); + yaz_iconv_close(cd); return 0; } +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +