2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-09-17 12:10:36 adam
10 * Revision 1.9 1997/05/14 06:54:03 adam
13 * Revision 1.8 1996/12/05 13:17:49 quinn
14 * Fixed GRS-1 null-ref
16 * Revision 1.7 1996/10/11 11:57:23 quinn
19 * Revision 1.6 1996/07/06 19:58:34 quinn
20 * System headerfiles gathered in yconfig
22 * Revision 1.5 1996/06/03 09:46:42 quinn
23 * Added OID data type.
25 * Revision 1.4 1996/05/01 12:45:30 quinn
26 * Support use of local tag names in abs file.
28 * Revision 1.3 1995/11/13 09:27:35 quinn
29 * Fiddling with the variant stuff.
31 * Revision 1.2 1995/11/01 13:54:46 quinn
34 * Revision 1.1 1995/11/01 11:56:07 quinn
35 * Added Retrieval (data management) functions en masse.
48 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
50 static Z_ElementMetaData *get_ElementMetaData(ODR o)
52 Z_ElementMetaData *r = odr_malloc(o, sizeof(*r));
59 r->num_supportedVariants = 0;
60 r->supportedVariants = 0;
62 r->elementDescriptor = 0;
64 r->surrogateElement = 0;
71 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
72 * from each of the triples beginning (ending) with 'n', up to the
73 * nearest parent tag. num should equal the number of triples in the
76 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
78 Z_Variant *v = odr_malloc(o, sizeof(*v));
81 v->globalVariantSetId = 0;
83 v->triples = odr_malloc(o, sizeof(Z_Triple*) * num);
86 * cycle back up through the tree of variants
87 * (traversing exactly 'level' variants).
89 for (p = n, num--; p && num >= 0; p = p->parent, num--)
93 assert(p->which == DATA1N_variant);
94 t = v->triples[num] = odr_malloc(o, sizeof(*t));
96 t->zclass = odr_malloc(o, sizeof(int));
97 *t->zclass = p->u.variant.type->zclass->zclass;
98 t->type = odr_malloc(o, sizeof(int));
99 *t->type = p->u.variant.type->type;
101 switch (p->u.variant.type->datatype)
104 t->which = Z_Triple_internationalString;
105 t->value.internationalString = odr_malloc(o,
106 strlen(p->u.variant.value)+1);
107 strcpy(t->value.internationalString, p->u.variant.value);
110 logf(LOG_WARN, "Unable to handle value for variant %s",
111 p->u.variant.type->name);
119 * Traverse the variant children of n, constructing a supportedVariant list.
121 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
126 for (c = n->child; c; c = c->next)
127 if (c->which == DATA1N_data && level)
129 if (!m->supportedVariants)
130 m->supportedVariants = odr_malloc(o, sizeof(Z_Variant*) *
132 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
134 logf(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
139 if (!(m->supportedVariants[m->num_supportedVariants++] =
140 make_variant(n, level, o)))
143 else if (c->which == DATA1N_variant)
144 if (traverse_triples(c, level+1, m, o) < 0)
149 static Z_ElementData *nodetoelementdata(data1_handle dh, data1_node *n,
150 int select, int leaf,
153 Z_ElementData *res = odr_malloc(o, sizeof(*res));
157 res->which = Z_ElementData_elementNotThere;
158 res->u.elementNotThere = ODR_NULLVAL;
160 else if (n->which == DATA1N_data && (leaf || n->parent->num_children == 1))
166 for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
169 switch (n->u.data.what)
172 res->which = Z_ElementData_numeric;
173 res->u.numeric = odr_malloc(o, sizeof(int));
174 *res->u.numeric = atoi(n->u.data.data);
178 toget = n->u.data.len;
179 if (p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
180 toget = p->u.tag.get_bytes;
181 res->which = Z_ElementData_string;
182 res->u.string = odr_malloc(o, toget+1);
183 memcpy(res->u.string, n->u.data.data, toget);
184 res->u.string[toget] = '\0';
188 res->which = Z_ElementData_oid;
189 strncpy(str, n->u.data.data, n->u.data.len);
190 str[n->u.data.len] = '\0';
191 res->u.oid = odr_getoidbystr(o, str);
192 *len += n->u.data.len;
195 logf(LOG_WARN, "Can't handle datatype.");
201 res->which = Z_ElementData_subtree;
202 if (!(res->u.subtree = data1_nodetogr (dh, n->parent, select, o, len)))
208 static Z_TaggedElement *nodetotaggedelement(data1_handle dh, data1_node *n,
212 Z_TaggedElement *res = odr_malloc(o, sizeof(*res));
217 if (n->which == DATA1N_tag)
219 if (n->u.tag.element)
220 tag = n->u.tag.element->tag;
225 * If we're a data element at this point, we need to insert a
226 * wellKnown tag to wrap us up.
228 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
230 if (!(tag = data1_gettagbyname (dh, n->root->u.root.absyn->tagset,
233 logf(LOG_WARN, "Unable to locate tag for 'wellKnown'");
241 logf(LOG_WARN, "Bad data.");
245 res->tagType = odr_malloc(o, sizeof(int));
246 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
247 res->tagValue = odr_malloc(o, sizeof(Z_StringOrNumeric));
248 if (tag && tag->which == DATA1T_numeric)
250 res->tagValue->which = Z_StringOrNumeric_numeric;
251 res->tagValue->u.numeric = odr_malloc(o, sizeof(int));
252 *res->tagValue->u.numeric = tag->value.numeric;
258 if (tag) /* well-known tag */
259 tagstr = tag->value.string;
260 else /* tag local to this file */
261 tagstr = n->u.tag.tag;
263 res->tagValue->which = Z_StringOrNumeric_string;
264 res->tagValue->u.string = odr_malloc(o, strlen(tagstr)+1);
265 strcpy(res->tagValue->u.string, tagstr);
267 res->tagOccurrence = 0;
268 res->appliedVariant = 0;
270 if (n->which == DATA1N_variant || (data && data->which ==
271 DATA1N_variant && data->parent->num_children == 1))
275 res->metaData = get_ElementMetaData(o);
276 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
277 if (traverse_triples(data, 0, res->metaData, o) < 0)
279 while (data && data->which == DATA1N_variant)
284 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
285 res->appliedVariant = make_variant(data->parent, nvars-1, o);
287 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
289 res->content = odr_malloc(o, sizeof(*res->content));
290 res->content->which = Z_ElementData_noDataRequested;
291 res->content->u.noDataRequested = ODR_NULLVAL;
293 else if (!(res->content = nodetoelementdata (dh, data, select, leaf,
300 Z_GenericRecord *data1_nodetogr(data1_handle dh, data1_node *n,
301 int select, ODR o, int *len)
303 Z_GenericRecord *res = odr_malloc(o, sizeof(*res));
306 res->elements = odr_malloc(o, sizeof(Z_TaggedElement *) * n->num_children);
307 res->num_elements = 0;
308 for (c = n->child; c; c = c->next)
310 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
312 if (!(res->elements[res->num_elements++] =
313 nodetotaggedelement (dh, c, select, o, len)))