2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1998-03-16 12:21:15 adam
8 * Fixed problem with tag names that weren't set to the right value
9 * when wildcards were used.
11 * Revision 1.13 1998/02/11 11:53:35 adam
12 * Changed code so that it compiles as C++.
14 * Revision 1.12 1997/11/24 11:33:56 adam
15 * Using function odr_nullval() instead of global ODR_NULLVAL when
18 * Revision 1.11 1997/11/18 09:51:09 adam
19 * Removed element num_children from data1_node. Minor changes in
22 * Revision 1.10 1997/09/17 12:10:36 adam
25 * Revision 1.9 1997/05/14 06:54:03 adam
28 * Revision 1.8 1996/12/05 13:17:49 quinn
29 * Fixed GRS-1 null-ref
31 * Revision 1.7 1996/10/11 11:57:23 quinn
34 * Revision 1.6 1996/07/06 19:58:34 quinn
35 * System headerfiles gathered in yconfig
37 * Revision 1.5 1996/06/03 09:46:42 quinn
38 * Added OID data type.
40 * Revision 1.4 1996/05/01 12:45:30 quinn
41 * Support use of local tag names in abs file.
43 * Revision 1.3 1995/11/13 09:27:35 quinn
44 * Fiddling with the variant stuff.
46 * Revision 1.2 1995/11/01 13:54:46 quinn
49 * Revision 1.1 1995/11/01 11:56:07 quinn
50 * Added Retrieval (data management) functions en masse.
63 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
65 static Z_ElementMetaData *get_ElementMetaData(ODR o)
67 Z_ElementMetaData *r = (Z_ElementMetaData *)odr_malloc(o, sizeof(*r));
74 r->num_supportedVariants = 0;
75 r->supportedVariants = 0;
77 r->elementDescriptor = 0;
79 r->surrogateElement = 0;
86 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
87 * from each of the triples beginning (ending) with 'n', up to the
88 * nearest parent tag. num should equal the number of triples in the
91 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
93 Z_Variant *v = (Z_Variant *)odr_malloc(o, sizeof(*v));
96 v->globalVariantSetId = 0;
98 v->triples = (Z_Triple **)odr_malloc(o, sizeof(Z_Triple*) * num);
101 * cycle back up through the tree of variants
102 * (traversing exactly 'level' variants).
104 for (p = n, num--; p && num >= 0; p = p->parent, num--)
108 assert(p->which == DATA1N_variant);
109 t = v->triples[num] = (Z_Triple *)odr_malloc(o, sizeof(*t));
111 t->zclass = (int *)odr_malloc(o, sizeof(int));
112 *t->zclass = p->u.variant.type->zclass->zclass;
113 t->type = (int *)odr_malloc(o, sizeof(int));
114 *t->type = p->u.variant.type->type;
116 switch (p->u.variant.type->datatype)
119 t->which = Z_Triple_internationalString;
120 t->value.internationalString = (char *)odr_malloc(o,
121 strlen(p->u.variant.value)+1);
122 strcpy(t->value.internationalString, p->u.variant.value);
125 logf(LOG_WARN, "Unable to handle value for variant %s",
126 p->u.variant.type->name);
134 * Traverse the variant children of n, constructing a supportedVariant list.
136 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
141 for (c = n->child; c; c = c->next)
142 if (c->which == DATA1N_data && level)
144 if (!m->supportedVariants)
145 m->supportedVariants = (Z_Variant **)odr_malloc(o, sizeof(Z_Variant*) *
147 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
149 logf(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
154 if (!(m->supportedVariants[m->num_supportedVariants++] =
155 make_variant(n, level, o)))
158 else if (c->which == DATA1N_variant)
159 if (traverse_triples(c, level+1, m, o) < 0)
164 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
165 int select, int leaf,
168 Z_ElementData *res = (Z_ElementData *)odr_malloc(o, sizeof(*res));
172 res->which = Z_ElementData_elementNotThere;
173 res->u.elementNotThere = odr_nullval();
175 else if (n->which == DATA1N_data && (leaf || n->next == NULL))
181 for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
184 switch (n->u.data.what)
187 res->which = Z_ElementData_numeric;
188 res->u.numeric = (int *)odr_malloc(o, sizeof(int));
189 *res->u.numeric = atoi(n->u.data.data);
193 toget = n->u.data.len;
194 if (p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
195 toget = p->u.tag.get_bytes;
196 res->which = Z_ElementData_string;
197 res->u.string = (char *)odr_malloc(o, toget+1);
198 memcpy(res->u.string, n->u.data.data, toget);
199 res->u.string[toget] = '\0';
203 res->which = Z_ElementData_oid;
204 strncpy(str, n->u.data.data, n->u.data.len);
205 str[n->u.data.len] = '\0';
206 res->u.oid = odr_getoidbystr(o, str);
207 *len += n->u.data.len;
210 logf(LOG_WARN, "Can't handle datatype.");
216 res->which = Z_ElementData_subtree;
217 if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
223 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
227 Z_TaggedElement *res = (Z_TaggedElement *)odr_malloc(o, sizeof(*res));
232 if (n->which == DATA1N_tag)
234 if (n->u.tag.element)
235 tag = n->u.tag.element->tag;
240 * If we're a data element at this point, we need to insert a
241 * wellKnown tag to wrap us up.
243 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
245 if (!(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
248 logf(LOG_WARN, "Unable to locate tag for 'wellKnown'");
256 logf(LOG_WARN, "Bad data.");
260 res->tagType = (int *)odr_malloc(o, sizeof(int));
261 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
262 res->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
263 if (tag && tag->which == DATA1T_numeric)
265 res->tagValue->which = Z_StringOrNumeric_numeric;
266 res->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
267 *res->tagValue->u.numeric = tag->value.numeric;
273 if (n->which == DATA1N_tag)
274 tagstr = n->u.tag.tag; /* tag at node */
276 tagstr = tag->value.string; /* no take from well-known */
278 tagstr = "?"; /* no tag at all! */
279 res->tagValue->which = Z_StringOrNumeric_string;
280 res->tagValue->u.string = (char *)odr_malloc(o, strlen(tagstr)+1);
281 strcpy(res->tagValue->u.string, tagstr);
283 res->tagOccurrence = 0;
284 res->appliedVariant = 0;
286 if (n->which == DATA1N_variant || (data && data->which ==
287 DATA1N_variant && data->next == NULL))
291 res->metaData = get_ElementMetaData(o);
292 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
293 if (traverse_triples(data, 0, res->metaData, o) < 0)
295 while (data && data->which == DATA1N_variant)
300 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
301 res->appliedVariant = make_variant(data->parent, nvars-1, o);
303 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
305 res->content = (Z_ElementData *)odr_malloc(o, sizeof(*res->content));
306 res->content->which = Z_ElementData_noDataRequested;
307 res->content->u.noDataRequested = odr_nullval();
309 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
316 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
317 int select, ODR o, int *len)
319 Z_GenericRecord *res = (Z_GenericRecord *)odr_malloc(o, sizeof(*res));
321 int num_children = 0;
323 for (c = n->child; c; c = c->next)
326 res->elements = (Z_TaggedElement **)odr_malloc(o, sizeof(Z_TaggedElement *) * num_children);
327 res->num_elements = 0;
328 for (c = n->child; c; c = c->next)
330 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
332 if (!(res->elements[res->num_elements++] =
333 nodetotaggedelement (dh, c, select, o, len)))