Updated version. Data1 compatibility fix for nodetomarc
[yaz-moved-to-github.git] / retrieval / d1_read.c
index e79ad72..539c915 100644 (file)
@@ -3,7 +3,7 @@
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
- * $Id: d1_read.c,v 1.45 2002-07-05 16:04:28 adam Exp $
+ * $Id: d1_read.c,v 1.47 2002-07-29 20:04:08 adam Exp $
  */
 
 #include <assert.h>
@@ -24,9 +24,12 @@ data1_node *data1_get_root_tag (data1_handle dh, data1_node *n)
 {
     if (!n)
         return 0;
-    n = n->child;
-    while (n && n->which != DATA1N_tag)
-        n = n->next;
+    if (data1_is_xmlmode(dh))
+    {
+        n = n->child;
+        while (n && n->which != DATA1N_tag)
+            n = n->next;
+    }
     return n;
 }
         
@@ -622,8 +625,16 @@ data1_node *data1_read_nodex (data1_handle dh, NMEM m,
                        break;
                    }
                }
-               if (level <= 1)
-                   return d1_stack[0];
+                if (data1_is_xmlmode(dh))
+                {
+                    if (level <= 1)
+                        return d1_stack[0];
+                }
+                else
+                {
+                    if (level <= 0)
+                        return d1_stack[0];
+                }
                continue;
            }   
            else if (!strcmp(tag, "var"))
@@ -676,10 +687,20 @@ data1_node *data1_read_nodex (data1_handle dh, NMEM m,
                 if (level == 0)
                 {
                     parent = data1_mk_root (dh, m, tag);
-                    d1_stack[level++] = parent;
+                    res = d1_stack[level] = parent;
+
+                    if (data1_is_xmlmode(dh))
+                    {
+                        level++;
+                        res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
+                        res->u.tag.attributes = xattr;
+                    }
+                }
+                else
+                {
+                    res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
+                    res->u.tag.attributes = xattr;
                 }
-                res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
-                res->u.tag.attributes = xattr;
             }
            d1_stack[level] = res;
            d1_stack[level+1] = 0;
@@ -834,7 +855,7 @@ static int conv_item (NMEM m, iconv_t t,
 }
 
 static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
-                           iconv_t t, WRBUF wrbuf)
+                           iconv_t t, WRBUF wrbuf, const char *tocode)
 {
     for (; n; n = n->next)
     {
@@ -872,24 +893,55 @@ static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
                 }
             }
             break;
+        case DATA1N_preprocess:
+            if (strcmp(n->u.preprocess.target, "xml") == 0)
+            {
+                data1_xattr *p = n->u.preprocess.attributes;
+                for (; p; p = p->next)
+                    if (strcmp (p->name, "encoding") == 0)
+                        p->value = nmem_strdup (m, tocode);
+            }
+            break;
         }
-        data1_iconv_s (dh, m, n->child, t, wrbuf);
+        data1_iconv_s (dh, m, n->child, t, wrbuf, tocode);
     }
 }
 #endif
 
+const char *data1_get_encoding (data1_handle dh, data1_node *n)
+{
+    /* see if we have an xml header that specifies encoding */
+    if (n && n->child && n->child->which == DATA1N_preprocess &&
+        strcmp (n->child->u.preprocess.target, "xml") == 0)
+    {
+        data1_xattr *xp = n->child->u.preprocess.attributes;
+        for (; xp; xp = xp->next)
+            if (!strcmp (xp->name, "encoding") == 0)
+                return xp->value;
+    }
+    /* no encoding in header, so see if "encoding" was specified for abs */
+    if (n && n->which == DATA1N_root &&
+        n->u.root.absyn && n->u.root.absyn->encoding)
+        return n->u.root.absyn->encoding;
+    /* none of above, return a hard coded default */
+    return "ISO-8859-1";
+}
+
 int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
                   const char *tocode, 
                   const char *fromcode)
 {
 #if HAVE_ICONV_H
-    WRBUF wrbuf = wrbuf_alloc();
-    iconv_t t = iconv_open (tocode, fromcode);
-    if (t == (iconv_t) (-1))
-        return -1;
-    data1_iconv_s (dh, m, n, t, wrbuf);
-    iconv_close (t);
-    wrbuf_free (wrbuf, 1);
+    if (strcmp (tocode, fromcode))
+    {
+        WRBUF wrbuf = wrbuf_alloc();
+        iconv_t t = iconv_open (tocode, fromcode);
+        if (t == (iconv_t) (-1))
+            return -1;
+        data1_iconv_s (dh, m, n, t, wrbuf, tocode);
+        iconv_close (t);
+        wrbuf_free (wrbuf, 1);
+    }
     return 0;
 #else
     return -2;