Start work on ICU based regexp searches
[idzebra-moved-to-github.git] / data1 / d1_grs.c
index 8343063..fe816b4 100644 (file)
@@ -1,21 +1,40 @@
-/*
- * Copyright (c) 1995-2002, Index Data.
- * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Id: d1_grs.c,v 1.1 2002-10-22 12:53:33 adam Exp $
- *
- */
+/* This file is part of the Zebra server.
+   Copyright (C) 1994-2011 Index Data
+
+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 this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+*/
+
+/* converts data1 tree to GRS-1 record */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <assert.h>
 #include <stdlib.h>
 
 #include <yaz/proto.h>
 #include <yaz/log.h>
-#include <data1.h>
+#include <d1_absyn.h>
 
 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
 
+static Z_GenericRecord *data1_nodetogr_r(data1_handle dh, data1_node *n,
+                                         int select, ODR o, int *len,
+                                         data1_tag *wellknown_tag);
+
 static Z_ElementMetaData *get_ElementMetaData(ODR o)
 {
     Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
@@ -62,10 +81,8 @@ static Z_Variant *make_variant(data1_node *n, int num, ODR o)
        assert(p->which == DATA1N_variant);
        t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
        t->variantSetId = 0;
-       t->zclass = (int *)odr_malloc(o, sizeof(int));
-       *t->zclass = p->u.variant.type->zclass->zclass;
-       t->type = (int *)odr_malloc(o, sizeof(int));
-       *t->type = p->u.variant.type->type;
+        t->zclass = odr_intdup(o, p->u.variant.type->zclass->zclass);
+       t->type = odr_intdup(o, p->u.variant.type->type);
 
        switch (p->u.variant.type->datatype)
        {
@@ -75,7 +92,7 @@ static Z_Variant *make_variant(data1_node *n, int num, ODR o)
                     odr_strdup(o, p->u.variant.value);
                break;
            default:
-               yaz_log(LOG_WARN, "Unable to handle value for variant %s",
+               yaz_log(YLOG_WARN, "Unable to handle value for variant %s",
                        p->u.variant.type->name);
                return 0;
        }
@@ -99,7 +116,7 @@ static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
                    D1_VARIANTARRAY);
            else if (m->num_supportedVariants >= D1_VARIANTARRAY)
            {
-               yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
+               yaz_log(YLOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
                        D1_VARIANTARRAY);
                return -1;
            }
@@ -155,7 +172,8 @@ static char *get_data(data1_node *n, int *len)
 
 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
                                        int select, int leaf,
-                                       ODR o, int *len)
+                                       ODR o, int *len,
+                                        data1_tag *wellknown_tag)
 {
     Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
 
@@ -175,8 +193,7 @@ static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
        {
         case DATA1I_num:
             res->which = Z_ElementData_numeric;
-            res->u.numeric = (int *)odr_malloc(o, sizeof(int));
-            *res->u.numeric = atoi_n (cp, toget);
+            res->u.numeric = odr_intdup(o, atoi_n(cp, toget));
             *len += 4;
             break;
         case DATA1I_text:
@@ -198,14 +215,15 @@ static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
             *len += oid_oidlen(res->u.oid) * sizeof(int);
             break;
         default:
-            yaz_log(LOG_WARN, "Can't handle datatype.");
+            yaz_log(YLOG_WARN, "Can't handle datatype.");
             return 0;
        }
     }
     else
     {
        res->which = Z_ElementData_subtree;
-       if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
+       if (!(res->u.subtree = data1_nodetogr_r (dh, n->parent, select, o, len,
+                                                 wellknown_tag )))
            return 0;
     }
     return res;
@@ -218,7 +236,7 @@ static int is_empty_data (data1_node *n)
     {
         int i = n->u.data.len;
         
-        while (i > 0 && strchr("\n ", n->u.data.data[i-1]))
+        while (i > 0 && d1_isspace(n->u.data.data[i-1]))
             i--;
         if (i == 0)
             return 1;
@@ -229,7 +247,8 @@ static int is_empty_data (data1_node *n)
 
 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
                                            int select, ODR o,
-                                           int *len)
+                                           int *len,
+                                            data1_tag *wellknown_tag)
 {
     Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
     data1_tag *tag = 0;
@@ -262,13 +281,9 @@ static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
      */
     else if (n->which == DATA1N_data || n->which == DATA1N_variant)
     {
-       if (n->root->u.root.absyn &&
-            !(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
-                                       "wellKnown")))
-       {
-           yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
-           return 0;
-       }
+        tag = wellknown_tag;
+        if (!tag)
+            return 0;
        data = n;
        leaf = 1;
         if (is_empty_data(data))
@@ -276,18 +291,16 @@ static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
     }
     else
     {
-       yaz_log(LOG_WARN, "Bad data.");
+       yaz_log(YLOG_WARN, "Bad data.");
        return 0;
     }
 
-    res->tagType = (int *)odr_malloc(o, sizeof(int));
-    *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
+    res->tagType = odr_intdup(o, (tag && tag->tagset) ? tag->tagset->type : 3);
     res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
     if (tag && tag->which == DATA1T_numeric)
     {
        res->tagValue->which = Z_StringOrNumeric_numeric;
-       res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
-       *res->tagValue->u.numeric = tag->value.numeric;
+       res->tagValue->u.numeric = odr_intdup(o, tag->value.numeric);
     }
     else
     {
@@ -329,22 +342,20 @@ static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
        res->content->u.noDataRequested = odr_nullval();
     }
     else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
-                                                o, len)))
+                                                o, len, wellknown_tag)))
        return 0;
     *len += 10;
     return res;
 }
 
-Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
-                               int select, ODR o, int *len)
+static Z_GenericRecord *data1_nodetogr_r(data1_handle dh, data1_node *n,
+                                         int select, ODR o, int *len,
+                                         data1_tag *wellknown_tag)
 {
     Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
     data1_node *c;
     int num_children = 0;
 
-    if (n->which == DATA1N_root)
-        n = data1_get_root_tag (dh, n);
-        
     for (c = n->child; c; c = c->next)
        num_children++;
 
@@ -356,8 +367,43 @@ Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
        if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
            continue;
        if ((res->elements[res->num_elements] =
-             nodetotaggedelement (dh, c, select, o, len)))
+             nodetotaggedelement (dh, c, select, o, len, wellknown_tag)))
            res->num_elements++;
     }
     return res;
 }
+
+Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
+                                int select, ODR o, int *len)
+
+{
+    data1_tag *wellknown_tag = 0;
+
+    if (n->which == DATA1N_root)
+        n = data1_get_root_tag (dh, n);
+
+    if (n->root->u.root.absyn &&
+        !(wellknown_tag =
+          data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
+                              "wellKnown")))
+    {
+        yaz_log(YLOG_WARN, "Unable to locate tag for 'wellKnown'");
+        wellknown_tag = odr_malloc(o, sizeof(*wellknown_tag));
+        wellknown_tag->which = DATA1T_numeric;
+        wellknown_tag->value.numeric = 19;
+        wellknown_tag->next = 0;
+        wellknown_tag->tagset = odr_malloc(o, sizeof(*wellknown_tag->tagset));
+        wellknown_tag->tagset->type = 1;
+        wellknown_tag->kind = DATA1K_string;
+    }
+    return data1_nodetogr_r(dh, n, select, o, len, wellknown_tag);
+}
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+