X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=retrieval%2Fd1_grs.c;h=873549ca7f18df43ad2f39b2d4822bfc74087239;hb=f3fa2f64b69427aef4dbb191fc870865bb87c9b5;hp=46b34aef4475489cda371c00eccf6b73301a99d6;hpb=6517fa53d35512887780fd07de5667940da18a9e;p=yaz-moved-to-github.git diff --git a/retrieval/d1_grs.c b/retrieval/d1_grs.c index 46b34ae..873549c 100644 --- a/retrieval/d1_grs.c +++ b/retrieval/d1_grs.c @@ -1,55 +1,24 @@ /* - * Copyright (c) 1995, Index Data. + * Copyright (c) 1995-2002, Index Data. * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Log: d1_grs.c,v $ - * 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 - * Fixed GRS-1 null-ref - * - * Revision 1.7 1996/10/11 11:57:23 quinn - * Smallish - * - * Revision 1.6 1996/07/06 19:58:34 quinn - * System headerfiles gathered in yconfig - * - * Revision 1.5 1996/06/03 09:46:42 quinn - * Added OID data type. - * - * Revision 1.4 1996/05/01 12:45:30 quinn - * Support use of local tag names in abs file. - * - * Revision 1.3 1995/11/13 09:27:35 quinn - * Fiddling with the variant stuff. - * - * Revision 1.2 1995/11/01 13:54:46 quinn - * Minor adjustments - * - * Revision 1.1 1995/11/01 11:56:07 quinn - * Added Retrieval (data management) functions en masse. - * + * $Id: d1_grs.c,v 1.25 2002-08-23 14:27:18 adam Exp $ * */ #include #include -#include -#include - -#include +#include +#include +#include #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */ 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; @@ -75,12 +44,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 @@ -91,24 +60,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; } } @@ -127,12 +95,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; } @@ -146,54 +114,91 @@ static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m, return 0; } +/* + * Locate some data under this node. This routine should handle variants + * prettily. + */ +static char *get_data(data1_node *n, int *len) +{ + char *r; + data1_node *np = 0; + + while (n) + { + if (n->which == DATA1N_data) + { + int i; + *len = n->u.data.len; + + for (i = 0; i<*len; i++) + if (!d1_isspace(n->u.data.data[i])) + break; + while (*len && d1_isspace(n->u.data.data[*len - 1])) + (*len)--; + *len = *len - i; + if (*len > 0) + return n->u.data.data + i; + } + if (n->which == DATA1N_tag) + np = n->child; + n = n->next; + if (!n) + { + n = np; + np = 0; + } + } + r = ""; + *len = strlen(r); + return r; +} + 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) { - char str[512]; - int toget; - data1_node *p; + char str[64], *cp; + int toget = n->u.data.len; - for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent) - ; + cp = get_data (n, &toget); switch (n->u.data.what) { - case DATA1I_num: - res->which = Z_ElementData_numeric; - res->u.numeric = odr_malloc(o, sizeof(int)); - *res->u.numeric = atoi(n->u.data.data); - *len += 4; - break; - case DATA1I_text: - toget = n->u.data.len; - 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); - memcpy(res->u.string, n->u.data.data, toget); - res->u.string[toget] = '\0'; - *len += toget; - break; - case DATA1I_oid: - res->which = Z_ElementData_oid; - strncpy(str, n->u.data.data, n->u.data.len); - str[n->u.data.len] = '\0'; - res->u.oid = odr_getoidbystr(o, str); - *len += n->u.data.len; - break; - default: - logf(LOG_WARN, "Can't handle datatype."); - return 0; + case DATA1I_num: + res->which = Z_ElementData_numeric; + res->u.numeric = (int *)odr_malloc(o, sizeof(int)); + *res->u.numeric = atoi_n (cp, toget); + *len += 4; + break; + case DATA1I_text: + res->which = Z_ElementData_string; + res->u.string = (char *)odr_malloc(o, toget+1); + if (toget) + memcpy(res->u.string, cp, toget); + res->u.string[toget] = '\0'; + *len += toget; + break; + case DATA1I_oid: + res->which = Z_ElementData_oid; + if (toget > 63) + toget = 63; + memcpy (str, cp, toget); + str[toget] = '\0'; + res->u.oid = odr_getoidbystr(o, str); + *len += oid_oidlen(res->u.oid) * sizeof(int); + break; + default: + yaz_log(LOG_WARN, "Can't handle datatype."); + return 0; } } else @@ -205,21 +210,49 @@ static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n, return res; } +static int is_empty_data (data1_node *n) +{ + if (n && n->which == DATA1N_data && n->u.data.what == DATA1I_text) + { + int i = n->u.data.len; + + while (i > 0 && n->u.data.data[i-1] == '\n') + i--; + if (i == 0) + return 1; + } + return 0; +} + + 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; + int leaf = 0; if (n->which == DATA1N_tag) { if (n->u.tag.element) tag = n->u.tag.element->tag; data = n->child; - leaf = 0; + + /* skip empty data children */ + while (is_empty_data(data)) + data = data->next; + if (!data) + data = n->child; + else + { /* got one. see if this is the only non-empty one */ + data1_node *sub = data->next; + while (sub && is_empty_data(sub)) + sub = sub->next; + if (!sub) + leaf = 1; /* all empty. 'data' is the only child */ + } } /* * If we're a data element at this point, we need to insert a @@ -227,48 +260,51 @@ static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n, */ else if (n->which == DATA1N_data || n->which == DATA1N_variant) { - if (!(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset, + if (n->root->u.root.absyn && + !(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; leaf = 1; + if (is_empty_data(data)) + return 0; } 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 + return 0; 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; @@ -286,9 +322,9 @@ static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n, } 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 (dh, data, select, leaf, o, len))) @@ -300,18 +336,26 @@ static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n, 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; + + if (n->which == DATA1N_root) + n = data1_get_root_tag (dh, n); + + 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 (dh, c, select, o, len))) - return 0; + if ((res->elements[res->num_elements] = + nodetotaggedelement (dh, c, select, o, len))) + res->num_elements++; } return res; }