X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=util%2Fyaziconv.c;h=57de6e1f4a7671001117450341fd638763f8814d;hp=a755a7d85818f6ae4eeb7c4a13c000804bc3df2a;hb=3d757077b85e1f9d6227773f9ac716edeee18911;hpb=d57ade59211707b9bcfacc61039e446c9fa3f36f diff --git a/util/yaziconv.c b/util/yaziconv.c index a755a7d..57de6e1 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) Index Data * See the file LICENSE for details. - * - * $Id: yaziconv.c,v 1.5 2006-04-21 10:28:07 adam Exp $ */ #if HAVE_CONFIG_H @@ -12,14 +10,27 @@ #include #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 +43,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,21 +65,21 @@ void convert (FILE *inf, yaz_iconv_t cd, int verbose) } if (verbose > 1) { - fprintf (stderr, "yaz_iconv: inbytesleft=%ld outbytesleft=%ld\n", - (long) inbytesleft, (long) 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 */ - { + { size_t i; for (i = 0; i