Remove isamd. It's not been in use for a long time and isamb is better
[idzebra-moved-to-github.git] / data1 / d1_read.c
index 6597220..742e170 100644 (file)
@@ -1,9 +1,28 @@
+/* $Id: d1_read.c,v 1.9 2004-08-04 08:35:22 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
 /*
- * Copyright (c) 1995-2002, Index Data.
- * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Id: d1_read.c,v 1.1 2002-10-22 12:53:33 adam Exp $
+ * This module reads "loose" SGML and converts it to data1 tree 
  */
 
 #include <assert.h>
@@ -109,13 +128,13 @@ data1_node *data1_append_node (data1_handle dh, NMEM m, int type,
     data1_node *r = (data1_node *)nmem_malloc(m, sizeof(*r));
     r->next = r->child = r->last_child = 0;
     r->destroy = 0;
-    
+
+    r->parent = parent;
     if (!parent)
         r->root = r;
     else
     {
         r->root = parent->root;
-        r->parent = parent;
         if (!parent->child)
             parent->child = parent->last_child = r;
         else
@@ -194,6 +213,24 @@ void data1_set_root(data1_handle dh, data1_node *res,
     res->u.root.absyn = absyn;
 }
 
+void data1_add_attrs(data1_handle dh, NMEM nmem, const char **attr,
+                    data1_xattr **p)
+{
+    while (*p)
+        p = &(*p)->next;
+
+    while (attr && *attr)
+    {
+        *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p));
+        (*p)->name = nmem_strdup (nmem, *attr++);
+        (*p)->value = nmem_strdup (nmem, *attr++);
+        (*p)->what = DATA1I_text;
+
+        p = &(*p)->next;
+    }
+    *p = 0;
+}
+                    
 data1_node *data1_mk_preprocess (data1_handle dh, NMEM nmem,
                                  const char *target,
                                  const char **attr, data1_node *at)
@@ -206,22 +243,31 @@ data1_node *data1_mk_preprocess_n (data1_handle dh, NMEM nmem,
                                    const char *target, size_t len,
                                    const char **attr, data1_node *at)
 {
-    data1_xattr **p;
     data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_preprocess, at);
     res->u.preprocess.target = data1_insert_string_n (dh, res, nmem,
                                                       target, len);
     
-    p = &res->u.preprocess.attributes;
-    while (attr && *attr)
-    {
-        *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p));
-        (*p)->name = nmem_strdup (nmem, *attr++);
-        (*p)->value = nmem_strdup (nmem, *attr++);
-        (*p)->what = DATA1I_text;
+    data1_add_attrs(dh, nmem, attr, &res->u.preprocess.attributes);
+    return res;
+}
 
-        p = &(*p)->next;
-    }
-    *p = 0;
+data1_node *data1_insert_preprocess (data1_handle dh, NMEM nmem,
+                                 const char *target,
+                                 const char **attr, data1_node *at)
+{
+    return data1_insert_preprocess_n (dh, nmem, target, strlen(target),
+                                     attr, at);
+}
+
+data1_node *data1_insert_preprocess_n (data1_handle dh, NMEM nmem,
+                                   const char *target, size_t len,
+                                   const char **attr, data1_node *at)
+{
+    data1_node *res = data1_insert_node (dh, nmem, DATA1N_preprocess, at);
+    res->u.preprocess.target = data1_insert_string_n (dh, res, nmem,
+                                                      target, len);
+    
+    data1_add_attrs(dh, nmem, attr, &res->u.preprocess.attributes);
     return res;
 }
 
@@ -231,7 +277,6 @@ data1_node *data1_mk_tag_n (data1_handle dh, NMEM nmem,
 {
     data1_node *partag = get_parent_tag(dh, at);
     data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_tag, at);
-    data1_xattr **p;
     data1_element *e = 0;
     
     res->u.tag.tag = data1_insert_string_n (dh, res, nmem, tag, len);
@@ -249,40 +294,17 @@ data1_node *data1_mk_tag_n (data1_handle dh, NMEM nmem,
                                            e, res->u.tag.tag);
     }
     res->u.tag.element = e;
-    p = &res->u.tag.attributes;
-    while (attr && *attr)
-    {
-        *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p));
-        (*p)->name = nmem_strdup (nmem, *attr++);
-        (*p)->value = nmem_strdup (nmem, *attr++);
-        (*p)->what = DATA1I_text;
-        p = &(*p)->next;
-    }
-    *p = 0;
+    data1_add_attrs(dh, nmem, attr, &res->u.tag.attributes);
     return res;
 }
 
 void data1_tag_add_attr (data1_handle dh, NMEM nmem,
                          data1_node *res, const char **attr)
 {
-    data1_xattr **p;
-
     if (res->which != DATA1N_tag)
         return;
 
-    p = &res->u.tag.attributes;
-    while (*p)
-        p = &(*p)->next;
-
-    while (attr && *attr)
-    {
-        *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p));
-        (*p)->name = nmem_strdup (nmem, *attr++);
-        (*p)->value = nmem_strdup (nmem, *attr++);
-        (*p)->what = DATA1I_text;
-        p = &(*p)->next;
-    }
-    *p = 0;
+    data1_add_attrs(dh, nmem, attr, &res->u.tag.attributes);
 }
 
 data1_node *data1_mk_tag (data1_handle dh, NMEM nmem,
@@ -303,7 +325,7 @@ data1_node *data1_search_tag (data1_handle dh, data1_node *n,
     }
     for (; n; n = n->next)
        if (n->which == DATA1N_tag && n->u.tag.tag &&
-           !yaz_matchstr (tag, n->u.tag.tag))
+           !yaz_matchstr (n->u.tag.tag, tag))
        {
            return n;
        }
@@ -448,8 +470,8 @@ data1_node *data1_add_taggeddata (data1_handle dh, data1_node *root,
     return data1_add_insert_taggeddata (dh, at, tagname, m, 1, 0);
 }
 
-data1_node *data1_mk_tag_data_int (data1_handle dh, data1_node *at,
-                                   const char *tag, int num,
+data1_node *data1_mk_tag_data_zint (data1_handle dh, data1_node *at,
+                                   const char *tag, zint num,
                                    NMEM nmem)
 {
     data1_node *node_data;
@@ -459,11 +481,18 @@ data1_node *data1_mk_tag_data_int (data1_handle dh, data1_node *at,
        return 0;
     node_data->u.data.what = DATA1I_num;
     node_data->u.data.data = node_data->lbuf;
-    sprintf (node_data->u.data.data, "%d", num);
+    sprintf (node_data->u.data.data, ZINT_FORMAT, num);
     node_data->u.data.len = strlen (node_data->u.data.data);
     return node_data;
 }
 
+data1_node *data1_mk_tag_data_int (data1_handle dh, data1_node *at,
+                                   const char *tag, int num,
+                                   NMEM nmem)
+{
+    return data1_mk_tag_data_zint(dh, at, tag, num, nmem);
+}
+
 data1_node *data1_mk_tag_data_oid (data1_handle dh, data1_node *at,
                                    const char *tag, Odr_oid *oid,
                                    NMEM nmem)
@@ -1078,7 +1107,7 @@ int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
                   const char *tocode, 
                   const char *fromcode)
 {
-    if (strcmp (tocode, fromcode))
+    if (yaz_matchstr (tocode, fromcode))
     {
         WRBUF wrbuf = wrbuf_alloc();
         yaz_iconv_t t = yaz_iconv_open (tocode, fromcode);
@@ -1090,3 +1119,33 @@ int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
     }
     return 0;
 }
+
+void data1_concat_text(data1_handle dh, NMEM m, data1_node *n)
+{
+    for (; n; n = n->next)
+    {
+        if (n->which == DATA1N_data && n->next && 
+            n->next->which == DATA1N_data)
+        {
+            int sz = 0;
+            int off = 0;
+            char *ndata;
+            data1_node *np;
+            for (np = n; np && np->which == DATA1N_data; np=np->next)
+                sz += np->u.data.len;
+            ndata = nmem_malloc(m, sz);
+            for (np = n; np && np->which == DATA1N_data; np=np->next)
+            {
+                memcpy(ndata+off, np->u.data.data, np->u.data.len);
+                off += np->u.data.len;
+            }
+            n->u.data.data = ndata;
+            n->u.data.len = sz;
+            n->next = np;
+           if (!np && n->parent)
+               n->parent->last_child = n;
+               
+        }
+        data1_concat_text(dh, m, n->child);
+    }
+}