ignore more files
[yaz-moved-to-github.git] / util / siconvtst.c
index 088fc93..cbe4ec2 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1997-2002, Index Data
  * See the file LICENSE for details.
  *
- * $Id: siconvtst.c,v 1.2 2002-08-27 14:14:01 adam Exp $
+ * $Id: siconvtst.c,v 1.4 2002-08-28 19:33:53 adam Exp $
  */
 
 #if HAVE_CONFIG_H
 
 #define CHUNK 8
 
-static void convert (FILE *inf, yaz_iconv_t cd)
+void convert (FILE *inf, yaz_iconv_t cd)
 {
     char inbuf0[CHUNK], *inbuf = inbuf0;
     char outbuf0[CHUNK], *outbuf = outbuf0;
     size_t outbytesleft = CHUNK;
     size_t inbytesleft = CHUNK;
+    int mustread = 1;
 
     while (1)
     {
-        size_t r = fread (inbuf, 1, inbytesleft, inf);
-        if (inbytesleft != r)
+        size_t r;
+        if (mustread)
         {
-            if (ferror(inf))
+            r = fread (inbuf, 1, inbytesleft, inf);
+            if (inbytesleft != r)
             {
-                fprintf (stderr, "yaziconv: error reading file\n");
-                exit (6);
-            }
-            if (r == 0)
-            {
-                if (outbuf != outbuf0)
-                    fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
-                break;
+                if (ferror(inf))
+                {
+                    fprintf (stderr, "yaziconv: error reading file\n");
+                    exit (6);
+                }
+                if (r == 0)
+                {
+                    if (outbuf != outbuf0)
+                        fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
+                    break;
+                }
+                inbytesleft = r;
             }
         }
         r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
         if (r == (size_t)(-1))
         {
-            if (yaz_iconv_error(cd) == YAZ_ICONV_EILSEQ)
+            int e = yaz_iconv_error(cd);
+            if (e == YAZ_ICONV_EILSEQ)
             {
                 fprintf (stderr, "invalid sequence\n");
                 return ;
             }
-
-            if (yaz_iconv_error(cd) == EINVAL) /* incomplete input */
+            else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
             { 
                 size_t i;
                 for (i = 0; i<inbytesleft; i++)
                     inbuf0[i] = inbuf[i];
+                inbuf = inbuf0 + i;
                 inbytesleft = CHUNK - inbytesleft;
+                mustread = 1;
             }
-            if (yaz_iconv_error(cd) == E2BIG) /* no more output space */
+            else if (e == YAZ_ICONV_E2BIG) /* no more output space */
             {
                 fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
                 outbuf = outbuf0;
                 outbytesleft = CHUNK;
+                mustread = 0;
             }
             else
             {
@@ -73,6 +82,12 @@ static void convert (FILE *inf, yaz_iconv_t cd)
         {
             inbuf = inbuf0;
             inbytesleft = CHUNK;
+
+            fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
+            outbuf = outbuf0;
+            outbytesleft = CHUNK;
+
+            mustread = 1;
         }
     }
 }
@@ -129,4 +144,5 @@ int main (int argc, char **argv)
 
     convert (inf, cd);
     yaz_iconv_close (cd);
+    return 0;
 }