Renamed logf function to yaz_log. Removed VC++ project files.
[yaz-moved-to-github.git] / retrieval / d1_grs.c
index 1a9d313..a222aee 100644 (file)
@@ -1,10 +1,34 @@
 /*
- * Copyright (c) 1995, Index Data.
+ * Copyright (c) 1995-1999, Index Data.
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: d1_grs.c,v $
- * Revision 1.9  1997-05-14 06:54:03  adam
+ * Revision 1.16  1999-08-27 09:40:32  adam
+ * Renamed logf function to yaz_log. Removed VC++ project files.
+ *
+ * Revision 1.15  1999/03/31 11:18:25  adam
+ * Implemented odr_strdup. Added Reference ID to backend server API.
+ *
+ * Revision 1.14  1998/03/16 12:21:15  adam
+ * Fixed problem with tag names that weren't set to the right value
+ * when wildcards were used.
+ *
+ * Revision 1.13  1998/02/11 11:53:35  adam
+ * Changed code so that it compiles as C++.
+ *
+ * Revision 1.12  1997/11/24 11:33:56  adam
+ * Using function odr_nullval() instead of global ODR_NULLVAL when
+ * appropriate.
+ *
+ * Revision 1.11  1997/11/18 09:51:09  adam
+ * Removed element num_children from data1_node. Minor changes in
+ * data1 to Explain.
+ *
+ * Revision 1.10  1997/09/17 12:10:36  adam
+ * YAZ version 1.4.
+ *
+ * Revision 1.9  1997/05/14 06:54:03  adam
  * C++ support.
  *
  * Revision 1.8  1996/12/05 13:17:49  quinn
 
 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
 
-Z_GenericRecord *data1_nodetogr(data1_node *n, int select, ODR o, int *len);
-
 static Z_ElementMetaData *get_ElementMetaData(ODR o)
 {
-    Z_ElementMetaData *r = odr_malloc(o, sizeof(*r));
+    Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
 
     r->seriesOrder = 0;
     r->usageRight = 0;
@@ -74,12 +96,12 @@ static Z_ElementMetaData *get_ElementMetaData(ODR o)
  */
 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
 {
-    Z_Variant *v = odr_malloc(o, sizeof(*v));
+    Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
     data1_node *p;
 
     v->globalVariantSetId = 0;
     v->num_triples = num;
-    v->triples = odr_malloc(o, sizeof(Z_Triple*) * num);
+    v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
 
     /*
      * cycle back up through the tree of variants
@@ -90,24 +112,23 @@ static Z_Variant *make_variant(data1_node *n, int num, ODR o)
        Z_Triple *t;
 
        assert(p->which == DATA1N_variant);
-       t = v->triples[num] = odr_malloc(o, sizeof(*t));
+       t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
        t->variantSetId = 0;
-       t->zclass = odr_malloc(o, sizeof(int));
+       t->zclass = (int *)odr_malloc(o, sizeof(int));
        *t->zclass = p->u.variant.type->zclass->zclass;
-       t->type = odr_malloc(o, sizeof(int));
+       t->type = (int *)odr_malloc(o, sizeof(int));
        *t->type = p->u.variant.type->type;
 
        switch (p->u.variant.type->datatype)
        {
            case DATA1K_string:
                t->which = Z_Triple_internationalString;
-               t->value.internationalString = odr_malloc(o,
-                   strlen(p->u.variant.value)+1);
-               strcpy(t->value.internationalString, p->u.variant.value);
+               t->value.internationalString =
+                    odr_strdup(o, p->u.variant.value);
                break;
            default:
-               logf(LOG_WARN, "Unable to handle value for variant %s",
-                   p->u.variant.type->name);
+               yaz_log(LOG_WARN, "Unable to handle value for variant %s",
+                       p->u.variant.type->name);
                return 0;
        }
     }
@@ -126,12 +147,12 @@ static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
        if (c->which == DATA1N_data && level)
        {
            if (!m->supportedVariants)
-               m->supportedVariants = odr_malloc(o, sizeof(Z_Variant*) *
+               m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
                    D1_VARIANTARRAY);
            else if (m->num_supportedVariants >= D1_VARIANTARRAY)
            {
-               logf(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
-                   D1_VARIANTARRAY);
+               yaz_log(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
+                       D1_VARIANTARRAY);
                return -1;
            }
 
@@ -145,17 +166,18 @@ static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
     return 0;
 }
 
-static Z_ElementData *nodetoelementdata(data1_node *n, int select, int leaf,
-    ODR o, int *len)
+static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
+                                       int select, int leaf,
+                                       ODR o, int *len)
 {
-    Z_ElementData *res = odr_malloc(o, sizeof(*res));
+    Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
 
     if (!n)
     {
        res->which = Z_ElementData_elementNotThere;
-       res->u.elementNotThere = ODR_NULLVAL;
+       res->u.elementNotThere = odr_nullval();
     }
-    else if (n->which == DATA1N_data && (leaf || n->parent->num_children == 1))
+    else if (n->which == DATA1N_data && (leaf || n->next == NULL))
     {
        char str[512];
        int toget;
@@ -168,7 +190,7 @@ static Z_ElementData *nodetoelementdata(data1_node *n, int select, int leaf,
        {
            case DATA1I_num:
                res->which = Z_ElementData_numeric;
-               res->u.numeric = odr_malloc(o, sizeof(int));
+               res->u.numeric = (int *)odr_malloc(o, sizeof(int));
                *res->u.numeric = atoi(n->u.data.data);
                *len += 4;
                break;
@@ -177,7 +199,7 @@ static Z_ElementData *nodetoelementdata(data1_node *n, int select, int leaf,
                if (p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
                    toget = p->u.tag.get_bytes;
                res->which = Z_ElementData_string;
-               res->u.string = odr_malloc(o, toget+1);
+               res->u.string = (char *)odr_malloc(o, toget+1);
                memcpy(res->u.string, n->u.data.data, toget);
                res->u.string[toget] = '\0';
                *len += toget;
@@ -190,23 +212,24 @@ static Z_ElementData *nodetoelementdata(data1_node *n, int select, int leaf,
                *len += n->u.data.len;
                break;
            default:
-               logf(LOG_WARN, "Can't handle datatype.");
+               yaz_log(LOG_WARN, "Can't handle datatype.");
                return 0;
        }
     }
     else
     {
        res->which = Z_ElementData_subtree;
-       if (!(res->u.subtree = data1_nodetogr(n->parent, select, o, len)))
+       if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
            return 0;
     }
     return res;
 }
 
-static Z_TaggedElement *nodetotaggedelement(data1_node *n, int select, ODR o,
-    int *len)
+static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
+                                           int select, ODR o,
+                                           int *len)
 {
-    Z_TaggedElement *res = odr_malloc(o, sizeof(*res));
+    Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
     data1_tag *tag = 0;
     data1_node *data;
     int leaf;
@@ -224,10 +247,10 @@ static Z_TaggedElement *nodetotaggedelement(data1_node *n, int select, ODR o,
      */
     else if (n->which == DATA1N_data || n->which == DATA1N_variant)
     {
-       if (!(tag = data1_gettagbyname(n->root->u.root.absyn->tagset,
-           "wellKnown")))
+       if (!(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
+                                       "wellKnown")))
        {
-           logf(LOG_WARN, "Unable to locate tag for 'wellKnown'");
+           yaz_log(LOG_WARN, "Unable to locate tag for 'wellKnown'");
            return 0;
        }
        data = n;
@@ -235,37 +258,37 @@ static Z_TaggedElement *nodetotaggedelement(data1_node *n, int select, ODR o,
     }
     else
     {
-       logf(LOG_WARN, "Bad data.");
+       yaz_log(LOG_WARN, "Bad data.");
        return 0;
     }
 
-    res->tagType = odr_malloc(o, sizeof(int));
+    res->tagType = (int *)odr_malloc(o, sizeof(int));
     *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
-    res->tagValue = odr_malloc(o, sizeof(Z_StringOrNumeric));
+    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 = odr_malloc(o, sizeof(int));
+       res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
        *res->tagValue->u.numeric = tag->value.numeric;
     }
     else
     {
        char *tagstr;
 
-       if (tag) /* well-known tag */
-           tagstr = tag->value.string;
-       else /* tag local to this file */
-           tagstr = n->u.tag.tag;
-
+       if (n->which == DATA1N_tag)      
+           tagstr = n->u.tag.tag;       /* tag at node */
+       else if (tag)                    
+           tagstr = tag->value.string;  /* no take from well-known */
+       else
+           tagstr = "?";                /* no tag at all! */
        res->tagValue->which = Z_StringOrNumeric_string;
-       res->tagValue->u.string = odr_malloc(o, strlen(tagstr)+1);
-       strcpy(res->tagValue->u.string, tagstr);
+       res->tagValue->u.string = odr_strdup(o, tagstr);
     }
     res->tagOccurrence = 0;
     res->appliedVariant = 0;
     res->metaData = 0;
     if (n->which == DATA1N_variant || (data && data->which ==
-       DATA1N_variant && data->parent->num_children == 1))
+       DATA1N_variant && data->next == NULL))
     {
        int nvars = 0;
 
@@ -283,29 +306,35 @@ static Z_TaggedElement *nodetotaggedelement(data1_node *n, int select, ODR o,
     }
     if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
     {
-       res->content = odr_malloc(o, sizeof(*res->content));
+       res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
        res->content->which = Z_ElementData_noDataRequested;
-       res->content->u.noDataRequested = ODR_NULLVAL;
+       res->content->u.noDataRequested = odr_nullval();
     }
-    else if (!(res->content = nodetoelementdata(data, select, leaf, o, len)))
+    else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
+                                                o, len)))
        return 0;
     *len += 10;
     return res;
 }
 
-Z_GenericRecord *data1_nodetogr(data1_node *n, int select, ODR o, int *len)
+Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
+                               int select, ODR o, int *len)
 {
-    Z_GenericRecord *res = odr_malloc(o, sizeof(*res));
+    Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
     data1_node *c;
+    int num_children = 0;
+    
+    for (c = n->child; c; c = c->next)
+       num_children++;
 
-    res->elements = odr_malloc(o, sizeof(Z_TaggedElement *) * n->num_children);
+    res->elements = (Z_TaggedElement **)odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
     res->num_elements = 0;
     for (c = n->child; c; c = c->next)
     {
        if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
            continue;
        if (!(res->elements[res->num_elements++] =
-           nodetotaggedelement(c, select, o, len)))
+             nodetotaggedelement (dh, c, select, o, len)))
            return 0;
     }
     return res;