Fixed marc8 flush to deal with sub/sup scripts. Refactor marc-8 page
authorAdam Dickmeiss <adam@indexdata.dk>
Sat, 17 Mar 2007 00:10:40 +0000 (00:10 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Sat, 17 Mar 2007 00:10:40 +0000 (00:10 +0000)
switch code to function yaz_write_marc8_page_chr.

src/siconv.c
test/tsticonv.c

index 75fafbf..8f61f4a 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: siconv.c,v 1.35 2007-03-12 10:59:59 adam Exp $
+ * $Id: siconv.c,v 1.36 2007-03-17 00:10:40 adam Exp $
  */
 /**
  * \file siconv.c
@@ -1167,6 +1167,49 @@ static size_t flush_combos(yaz_iconv_t cd,
     return 0;
 }
 
+static size_t yaz_write_marc8_page_chr(yaz_iconv_t cd, 
+                                       char **outbuf, size_t *outbytesleft,
+                                       const char *page_chr)
+{
+    const char *old_page_chr = cd->write_marc8_page_chr;
+    if (strcmp(page_chr, old_page_chr))
+    {
+        size_t plen = 0;
+        const char *page_out = page_chr;
+        
+        if (*outbytesleft < 8)
+        {
+            cd->my_errno = YAZ_ICONV_E2BIG;
+            
+            return (size_t) (-1);
+        }
+        cd->write_marc8_page_chr = page_chr;
+        
+        if (!strcmp(old_page_chr, "\033p") 
+            || !strcmp(old_page_chr, "\033g")
+            || !strcmp(old_page_chr, "\033b"))
+        {
+            /* Technique 1 leave */
+            page_out = "\033s";
+            if (strcmp(page_chr, "\033(B")) /* Not going ASCII page? */
+            {
+                /* Must leave script + enter new page */
+                plen = strlen(page_out);
+                memcpy(*outbuf, page_out, plen);
+                (*outbuf) += plen;
+                (*outbytesleft) -= plen;
+                page_out = page_chr;
+            }
+        }
+        plen = strlen(page_out);
+        memcpy(*outbuf, page_out, plen);
+        (*outbuf) += plen;
+        (*outbytesleft) -= plen;
+    }
+    return 0;
+}
+
+
 static size_t yaz_write_marc8_2(yaz_iconv_t cd, unsigned long x,
                                 char **outbuf, size_t *outbytesleft,
                                 int last)
@@ -1191,43 +1234,12 @@ static size_t yaz_write_marc8_2(yaz_iconv_t cd, unsigned long x,
     else
     {
         size_t r = flush_combos(cd, outbuf, outbytesleft);
-        const char *old_page_chr = cd->write_marc8_page_chr;
         if (r)
             return r;
-        if (strcmp(page_chr, old_page_chr))
-        {
-            size_t plen = 0;
-            const char *page_out = page_chr;
 
-            if (*outbytesleft < 8)
-            {
-                cd->my_errno = YAZ_ICONV_E2BIG;
-                
-                return (size_t) (-1);
-            }
-            cd->write_marc8_page_chr = page_chr;
-
-            if (!strcmp(old_page_chr, "\033p") 
-                || !strcmp(old_page_chr, "\033g")
-                || !strcmp(old_page_chr, "\033b"))
-            {
-                /* Technique 1 leave */
-                page_out = "\033s";
-                if (strcmp(page_chr, "\033(B")) /* Not going ASCII page? */
-                {
-                    /* Must leave script + enter new page */
-                    plen = strlen(page_out);
-                    memcpy(*outbuf, page_out, plen);
-                    (*outbuf) += plen;
-                    (*outbytesleft) -= plen;
-                    page_out = page_chr;
-                }
-            }
-            plen = strlen(page_out);
-            memcpy(*outbuf, page_out, plen);
-            (*outbuf) += plen;
-            (*outbytesleft) -= plen;
-        }
+        r = yaz_write_marc8_page_chr(cd, outbuf, outbytesleft, page_chr);
+        if (r)
+            return r;
         cd->write_marc8_last = y;
     }
     if (last)
@@ -1248,18 +1260,10 @@ static size_t yaz_write_marc8_2(yaz_iconv_t cd, unsigned long x,
 static size_t yaz_flush_marc8(yaz_iconv_t cd,
                               char **outbuf, size_t *outbytesleft)
 {
-    if (strcmp(cd->write_marc8_page_chr, "\033(B"))
-    {
-        if (*outbytesleft < 3)
-        {
-            cd->my_errno = YAZ_ICONV_E2BIG;
-            return (size_t) (-1);
-        }
-        memcpy(*outbuf, "\033(B", 3);
-        (*outbuf) += 3;
-        *outbytesleft -= 3;
-    }
-    return 0;
+    size_t r = flush_combos(cd, outbuf, outbytesleft);
+    if (r)
+        return r;
+    return yaz_write_marc8_page_chr(cd, outbuf, outbytesleft, "\033(B");
 }
 
 static size_t yaz_write_marc8(yaz_iconv_t cd, unsigned long x,
index 7c69c0b..7fac3c5 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: tsticonv.c,v 1.26 2007-03-12 10:59:59 adam Exp $
+ * $Id: tsticonv.c,v 1.27 2007-03-17 00:10:41 adam Exp $
  */
 
 #if HAVE_CONFIG_H
@@ -470,6 +470,41 @@ static void tst_utf8_to_marc8(void)
                           "(\033p0\x1bs)"));
     
  
+    {
+        char *inbuf0 = "\xe2\x81\xb0";
+        char *inbuf = inbuf0;
+        size_t inbytesleft = strlen(inbuf);
+        char outbuf0[64];
+        char *outbuf = outbuf0;
+        size_t outbytesleft = sizeof(outbuf0)-1;
+        size_t r;
+#if 0
+        int i;
+#endif
+        r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
+        YAZ_CHECK(r != (size_t) (-1));
+
+#if 0
+        *outbuf = '\0';  /* so we know when to stop printing */
+        for (i = 0; outbuf0[i]; i++)
+        {
+            int ch = outbuf0[i] & 0xff;
+            yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
+        }
+#endif
+
+        r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
+        YAZ_CHECK(r != (size_t) (-1));
+        *outbuf = '\0';  /* for strcmp test below and printing */
+#if 0
+        for (i = 0; outbuf0[i]; i++)
+        {
+            int ch = outbuf0[i] & 0xff;
+            yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
+        }
+#endif
+        YAZ_CHECK(strcmp("\033p0\x1bs", outbuf0) == 0);
+    }
     yaz_iconv_close(cd);
 }