2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-11-01 13:54:46 quinn
10 * Revision 1.1 1995/11/01 11:56:07 quinn
11 * Added Retrieval (data management) functions en masse.
24 #define D1_VARIANTARRAY 20 /* fixed max length on sup'd variant-list. Lazy me */
26 Z_GenericRecord *data1_nodetogr(data1_node *n, int select, ODR o);
28 static Z_ElementMetaData *get_ElementMetaData(ODR o)
30 Z_ElementMetaData *r = odr_malloc(o, sizeof(*r));
37 r->num_supportedVariants = 0;
38 r->supportedVariants = 0;
40 r->elementDescriptor = 0;
42 r->surrogateElement = 0;
49 * N should point to the *last* (leaf) triple in a sequence. Construct a variant
50 * from each of the triples beginning (ending) with 'n', up to the
51 * nearest parent tag. num should equal the number of triples in the
54 static Z_Variant *make_variant(data1_node *n, int num, ODR o)
56 Z_Variant *v = odr_malloc(o, sizeof(*v));
59 v->globalVariantSetId = 0;
61 v->triples = odr_malloc(o, sizeof(Z_Triple*) * num);
64 * cycle back up through the tree of variants
65 * (traversing exactly 'level' variants).
67 for (p = n, num--; p && num >= 0; p = p->parent, num--)
71 assert(p->which == DATA1N_variant);
72 t = v->triples[num] = odr_malloc(o, sizeof(*t));
74 t->class = odr_malloc(o, sizeof(int));
75 *t->class = p->u.variant.type->class->class;
76 t->type = odr_malloc(o, sizeof(int));
77 *t->type = p->u.variant.type->type;
79 switch (p->u.variant.type->datatype)
82 t->which = Z_Triple_internationalString;
83 t->value.internationalString = odr_malloc(o,
84 strlen(p->u.variant.value)+1);
85 strcpy(t->value.internationalString, p->u.variant.value);
88 logf(LOG_WARN, "Unable to handle value for variant %s",
89 p->u.variant.type->name);
97 * Traverse the variant children of n, constructing a supportedVariant list.
99 static int traverse_triples(data1_node *n, int level, Z_ElementMetaData *m,
104 for (c = n->child; c; c = c->next)
105 if (c->which == DATA1N_data && level)
107 if (!m->supportedVariants)
108 m->supportedVariants = odr_malloc(o, sizeof(Z_Variant*) *
110 else if (m->num_supportedVariants >= D1_VARIANTARRAY)
112 logf(LOG_WARN, "Too many variants (D1_VARIANTARRAY==%d)",
117 if (!(m->supportedVariants[m->num_supportedVariants++] =
118 make_variant(n, level, o)))
121 else if (c->which == DATA1N_variant)
122 if (traverse_triples(c, level+1, m, o) < 0)
127 static Z_ElementData *nodetoelementdata(data1_node *n, int select, int leaf,
130 Z_ElementData *res = odr_malloc(o, sizeof(*res));
134 res->which = Z_ElementData_elementNotThere;
135 res->u.elementNotThere = ODR_NULLVAL;
137 else if (n->which == DATA1N_data && (leaf || n->parent->num_children == 1))
139 switch (n->u.data.what)
142 res->which = Z_ElementData_numeric;
143 res->u.numeric = odr_malloc(o, sizeof(int));
144 *res->u.numeric = atoi(n->u.data.data);
147 res->which = Z_ElementData_string;
148 res->u.string = odr_malloc(o, n->u.data.len+1);
149 memcpy(res->u.string, n->u.data.data, n->u.data.len);
150 res->u.string[n->u.data.len] = '\0';
153 logf(LOG_WARN, "Can't handle datatype.");
159 res->which = Z_ElementData_subtree;
160 if (!(res->u.subtree = data1_nodetogr(n->parent, select, o)))
166 static Z_TaggedElement *nodetotaggedelement(data1_node *n, int select, ODR o)
168 Z_TaggedElement *res = odr_malloc(o, sizeof(*res));
173 if (n->which == DATA1N_tag)
175 if (n->u.tag.element)
176 tag = n->u.tag.element->tag;
180 else if (n->which == DATA1N_data || n->which == DATA1N_variant)
182 if (!(tag = data1_gettagbyname(n->root->u.root.absyn->tagset,
185 logf(LOG_WARN, "Unable to locate tag for 'wellKnown'");
193 logf(LOG_WARN, "Bad data.");
197 res->tagType = odr_malloc(o, sizeof(int));
198 *res->tagType = tag ? tag->tagset->type : 3;
199 res->tagValue = odr_malloc(o, sizeof(Z_StringOrNumeric));
200 if (tag && tag->which == DATA1T_numeric)
202 res->tagValue->which = Z_StringOrNumeric_numeric;
203 res->tagValue->u.numeric = odr_malloc(o, sizeof(int));
204 *res->tagValue->u.numeric = tag->value.numeric;
210 if (tag) /* well-known tag */
211 tagstr = tag->value.string;
212 else /* tag local to this file */
213 tagstr = n->u.tag.tag;
215 res->tagValue->which = Z_StringOrNumeric_string;
216 res->tagValue->u.string = odr_malloc(o, strlen(tagstr)+1);
217 strcpy(res->tagValue->u.string, tagstr);
219 res->tagOccurrence = 0;
220 res->appliedVariant = 0;
222 if (n->which == DATA1N_variant || (data && data->which ==
223 DATA1N_variant && data->parent->num_children == 1))
227 res->metaData = get_ElementMetaData(o);
228 if (traverse_triples(data, 0, res->metaData, o) < 0)
230 while (data && data->which == DATA1N_variant)
235 res->appliedVariant = make_variant(data->parent, nvars-1, o);
237 if (!(res->content = nodetoelementdata(data, select, leaf, o)))
242 Z_GenericRecord *data1_nodetogr(data1_node *n, int select, ODR o)
244 Z_GenericRecord *res = odr_malloc(o, sizeof(*res));
247 res->elements = odr_malloc(o, sizeof(Z_TaggedElement *) * n->num_children);
248 res->num_elements = 0;
249 for (c = n->child; c; c = c->next)
251 if (c->which == DATA1N_tag && select && !c->u.tag.node_selected)
253 if (!(res->elements[res->num_elements++] =
254 nodetotaggedelement(c, select, o)))