2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1996-12-05 13:17:49 quinn
10 * Revision 1.7 1996/10/11 11:57:23 quinn
13 * Revision 1.6 1996/07/06 19:58:34 quinn
14 * System headerfiles gathered in yconfig
16 * Revision 1.5 1996/06/03 09:46:42 quinn
17 * Added OID data type.
19 * Revision 1.4 1996/05/01 12:45:30 quinn
20 * Support use of local tag names in abs file.
22 * Revision 1.3 1995/11/13 09:27:35 quinn
23 * Fiddling with the variant stuff.
25 * Revision 1.2 1995/11/01 13:54:46 quinn
28 * Revision 1.1 1995/11/01 11:56:07 quinn
29 * Added Retrieval (data management) functions en masse.
42 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
44 Z_GenericRecord *data1_nodetogr(data1_node *n, int select, ODR o, int *len);
46 static Z_ElementMetaData *get_ElementMetaData(ODR o)
48 Z_ElementMetaData *r = odr_malloc(o, sizeof(*r));
55 r->num_supportedVariants = 0;
56 r->supportedVariants = 0;
58 r->elementDescriptor = 0;
60 r->surrogateElement = 0;
67 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
68 * from each of the triples beginning (ending) with 'n', up to the
69 * nearest parent tag. num should equal the number of triples in the
72 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
74 Z_Variant *v = odr_malloc(o, sizeof(*v));
77 v->globalVariantSetId = 0;
79 v->triples = odr_malloc(o, sizeof(Z_Triple*) * num);
82 * cycle back up through the tree of variants
83 * (traversing exactly 'level' variants).
85 for (p = n, num--; p && num >= 0; p = p->parent, num--)
89 assert(p->which == DATA1N_variant);
90 t = v->triples[num] = odr_malloc(o, sizeof(*t));
92 t->class = odr_malloc(o, sizeof(int));
93 *t->class = p->u.variant.type->class->class;
94 t->type = odr_malloc(o, sizeof(int));
95 *t->type = p->u.variant.type->type;
97 switch (p->u.variant.type->datatype)
100 t->which = Z_Triple_internationalString;
101 t->value.internationalString = odr_malloc(o,
102 strlen(p->u.variant.value)+1);
103 strcpy(t->value.internationalString, p->u.variant.value);
106 logf(LOG_WARN, "Unable to handle value for variant %s",
107 p->u.variant.type->name);
115 * Traverse the variant children of n, constructing a supportedVariant list.
117 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
122 for (c = n->child; c; c = c->next)
123 if (c->which == DATA1N_data && level)
125 if (!m->supportedVariants)
126 m->supportedVariants = odr_malloc(o, sizeof(Z_Variant*) *
128 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
130 logf(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
135 if (!(m->supportedVariants[m->num_supportedVariants++] =
136 make_variant(n, level, o)))
139 else if (c->which == DATA1N_variant)
140 if (traverse_triples(c, level+1, m, o) < 0)
145 static Z_ElementData *nodetoelementdata(data1_node *n, int select, int leaf,
148 Z_ElementData *res = odr_malloc(o, sizeof(*res));
152 res->which = Z_ElementData_elementNotThere;
153 res->u.elementNotThere = ODR_NULLVAL;
155 else if (n->which == DATA1N_data && (leaf || n->parent->num_children == 1))
161 for (p = n->parent; p && p->which != DATA1N_tag; p = p->parent)
164 switch (n->u.data.what)
167 res->which = Z_ElementData_numeric;
168 res->u.numeric = odr_malloc(o, sizeof(int));
169 *res->u.numeric = atoi(n->u.data.data);
173 toget = n->u.data.len;
174 if (p->u.tag.get_bytes > 0 && p->u.tag.get_bytes < toget)
175 toget = p->u.tag.get_bytes;
176 res->which = Z_ElementData_string;
177 res->u.string = odr_malloc(o, toget+1);
178 memcpy(res->u.string, n->u.data.data, toget);
179 res->u.string[toget] = '\0';
183 res->which = Z_ElementData_oid;
184 strncpy(str, n->u.data.data, n->u.data.len);
185 str[n->u.data.len] = '\0';
186 res->u.oid = odr_getoidbystr(o, str);
187 *len += n->u.data.len;
190 logf(LOG_WARN, "Can't handle datatype.");
196 res->which = Z_ElementData_subtree;
197 if (!(res->u.subtree = data1_nodetogr(n->parent, select, o, len)))
203 static Z_TaggedElement *nodetotaggedelement(data1_node *n, int select, ODR o,
206 Z_TaggedElement *res = odr_malloc(o, sizeof(*res));
211 if (n->which == DATA1N_tag)
213 if (n->u.tag.element)
214 tag = n->u.tag.element->tag;
219 * If we're a data element at this point, we need to insert a
220 * wellKnown tag to wrap us up.
222 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
224 if (!(tag = data1_gettagbyname(n->root->u.root.absyn->tagset,
227 logf(LOG_WARN, "Unable to locate tag for 'wellKnown'");
235 logf(LOG_WARN, "Bad data.");
239 res->tagType = odr_malloc(o, sizeof(int));
240 *res->tagType = (tag && tag->tagset) ? tag->tagset->type : 3;
241 res->tagValue = odr_malloc(o, sizeof(Z_StringOrNumeric));
242 if (tag && tag->which == DATA1T_numeric)
244 res->tagValue->which = Z_StringOrNumeric_numeric;
245 res->tagValue->u.numeric = odr_malloc(o, sizeof(int));
246 *res->tagValue->u.numeric = tag->value.numeric;
252 if (tag) /* well-known tag */
253 tagstr = tag->value.string;
254 else /* tag local to this file */
255 tagstr = n->u.tag.tag;
257 res->tagValue->which = Z_StringOrNumeric_string;
258 res->tagValue->u.string = odr_malloc(o, strlen(tagstr)+1);
259 strcpy(res->tagValue->u.string, tagstr);
261 res->tagOccurrence = 0;
262 res->appliedVariant = 0;
264 if (n->which == DATA1N_variant || (data && data->which ==
265 DATA1N_variant && data->parent->num_children == 1))
269 res->metaData = get_ElementMetaData(o);
270 if (n->which == DATA1N_tag && n->u.tag.make_variantlist)
271 if (traverse_triples(data, 0, res->metaData, o) < 0)
273 while (data && data->which == DATA1N_variant)
278 if (n->which != DATA1N_tag || !n->u.tag.no_data_requested)
279 res->appliedVariant = make_variant(data->parent, nvars-1, o);
281 if (n->which == DATA1N_tag && n->u.tag.no_data_requested)
283 res->content = odr_malloc(o, sizeof(*res->content));
284 res->content->which = Z_ElementData_noDataRequested;
285 res->content->u.noDataRequested = ODR_NULLVAL;
287 else if (!(res->content = nodetoelementdata(data, select, leaf, o, len)))
293 Z_GenericRecord *data1_nodetogr(data1_node *n, int select, ODR o, int *len)
295 Z_GenericRecord *res = odr_malloc(o, sizeof(*res));
298 res->elements = odr_malloc(o, sizeof(Z_TaggedElement *) * n->num_children);
299 res->num_elements = 0;
300 for (c = n->child; c; c = c->next)
302 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
304 if (!(res->elements[res->num_elements++] =
305 nodetotaggedelement(c, select, o, len)))