2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-12-11 15:22:37 quinn
8 * Added last_child field to the node.
9 * Rewrote schema-mapping.
11 * Revision 1.4 1995/11/13 09:27:36 quinn
12 * Fiddling with the variant stuff.
14 * Revision 1.3 1995/11/01 16:34:57 quinn
15 * Making data1 look for tables in data1_tabpath
17 * Revision 1.2 1995/11/01 13:54:48 quinn
20 * Revision 1.1 1995/11/01 11:56:09 quinn
21 * Added Retrieval (data management) functions en masse.
23 * Revision 1.14 1995/10/30 12:40:55 quinn
24 * Fixed a couple of bugs.
26 * Revision 1.13 1995/10/25 16:00:47 quinn
27 * USMARC support is now almost operational
29 * Revision 1.12 1995/10/16 14:02:55 quinn
30 * Changes to support element set names and espec1
32 * Revision 1.11 1995/10/13 16:05:08 quinn
33 * Adding Espec1-processing
35 * Revision 1.10 1995/10/11 14:53:44 quinn
38 * Revision 1.9 1995/10/06 16:56:50 quinn
39 * Fixed ranked result.
41 * Revision 1.8 1995/10/06 16:44:13 quinn
42 * Work on attribute set mapping, etc.
44 * Revision 1.7 1995/10/06 12:58:35 quinn
47 * Revision 1.6 1995/10/04 09:29:49 quinn
48 * Adjustments to support USGS test data
50 * Revision 1.5 1995/10/03 17:56:43 quinn
53 * Revision 1.4 1995/10/02 15:53:19 quinn
56 * Revision 1.3 1995/10/02 14:55:21 quinn
57 * *** empty log message ***
59 * Revision 1.2 1995/09/14 15:18:13 quinn
62 * Revision 1.1 1995/09/12 11:24:30 quinn
63 * Beginning to add code for structured records.
76 char *data1_tabpath = 0; /* global path for tables */
78 void data1_set_tabpath(char *p)
79 { data1_tabpath = p; }
81 static data1_node *freelist = 0;
84 * get the tag which is the immediate parent of this node (this may mean
85 * traversing intermediate things like variants and stuff.
87 data1_node *get_parent_tag(data1_node *n)
89 for (; n && n->which != DATA1N_root; n = n->parent)
90 if (n->which == DATA1N_tag)
95 data1_node *data1_mk_node(void)
102 if (!(r = xmalloc(sizeof(*r))))
104 r->next = r->child = r->last_child = r->parent = 0;
109 static void fr_node(data1_node *n)
115 void data1_free_tree(data1_node *t)
117 data1_node *p = t->child, *pn;
129 * Insert a tagged node into the record root as first child of the node at
130 * which should be root or tag itself). Returns pointer to the data node,
131 * which can then be modified.
133 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
136 data1_node *tagn = data1_mk_node();
139 tagn->which = DATA1N_tag;
142 tagn->u.tag.node_selected = 0;
143 tagn->u.tag.make_variantlist = 0;
144 tagn->u.tag.no_data_requested = 0;
145 if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn,
151 tagn->child = datn = data1_mk_node();
152 tagn->num_children = 1;
155 datn->which = DATA1N_data;
156 tagn->next = at->child;
164 * Ugh. Sometimes functions just grow and grow on you. This one reads a
165 * 'node' and its children.
167 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
172 while (**buf && isspace(**buf))
181 if (**buf == '<') /* beginning of tag */
183 char *tag = (*buf) + 1;
187 data1_element *elem = 0;
189 for (; *t && *t != '>' && !isspace(*t); t++);
190 if (*t != '>' && !isspace(*t))
192 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
195 if (isspace(*t)) /* the tag has arguments */
202 for (; *t && *t != '>'; t++);
203 if (*t != '>' && !isspace(*t))
205 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
212 * if end-tag, see if we terminate parent. If so, consume and return.
220 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
221 parent->u.root.type)) ||
222 (parent->which == DATA1N_tag && !strcmp(tag + 1,
235 if (!absyn) /* parent node - what are we? */
237 if (!(absyn = data1_get_absyn(tag)))
239 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
243 res = data1_mk_node();
244 res->which = DATA1N_root;
245 res->u.root.type = tag;
246 res->u.root.absyn = absyn;
250 else if (!strncmp(tag, "var", 3))
252 char class[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
257 if (sscanf(args, "%s %s %n", class, type, &val_offset) != 2)
259 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
262 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
267 * If we're the first variant in this group, create a parent var,
268 * and insert it before the current variant.
270 if (parent->which != DATA1N_variant)
272 res = data1_mk_node();
273 res->which = DATA1N_variant;
274 res->u.variant.type = 0;
275 res->u.variant.value = 0;
276 res->root = parent->root;
282 * now determine if one of our ancestor triples is of same type.
283 * If so, we break here. This will make the parser unwind until
284 * we become a sibling (alternate variant) to the aforementioned
285 * triple. It stinks that we re-parse these tags on every
286 * iteration of this. This is a function in need of a rewrite.
288 for (p = parent; p->which == DATA1N_variant; p = p->parent)
289 if (p->u.variant.type == tp)
295 res = data1_mk_node();
296 res->which = DATA1N_variant;
297 res->root = parent->root;
298 res->u.variant.type = tp;
299 res->u.variant.value = args + val_offset;
303 else /* tag.. acquire our element in the abstract syntax */
305 data1_node *partag = get_parent_tag(parent);
306 data1_element *e = 0;
309 if (parent->which == DATA1N_variant)
315 if (!(e = partag->u.tag.element))
316 localtag = 1; /* our parent is a local tag */
319 if (!localtag && !(elem = data1_getelementbytagname(absyn,
320 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
322 if (parent->which == DATA1N_root)
323 logf(LOG_WARN, "Tag '%s' used out of context", tag);
328 elem = data1_getelementbytagname(absyn, e, tag);
330 res = data1_mk_node();
331 res->which = DATA1N_tag;
332 res->u.tag.element = elem;
333 res->u.tag.tag = tag;
334 res->u.tag.node_selected = 0;
335 res->u.tag.make_variantlist = 0;
336 res->u.tag.no_data_requested = 0;
337 res->root = parent->root;
341 res->parent = parent;
342 res->num_children = 0;
348 while ((*pp = data1_read_node(buf, res, line, absyn)))
350 res->last_child = *pp;
355 else /* != '<'... this is a body of text */
358 char *data = *buf, *pp = *buf;
360 data1_node *partag = get_parent_tag(parent);
363 /* Determine length and remove newlines/extra blanks */
364 while (**buf && **buf != '<')
372 while (isspace(**buf))
376 *(pp++) = *((*buf)++);
379 while (isspace(data[len-1]))
381 res = data1_mk_node();
382 res->parent = parent;
383 res->which = DATA1N_data;
384 res->u.data.what = DATA1I_text;
385 res->u.data.len = len;
386 res->u.data.data = data;
387 res->root = parent->root;
390 * if the parent is structured, we'll insert a 'wellKnown' marker
391 * in front of the data.
394 if (partag->u.tag.element && partag->u.tag.element->tag->kind ==
397 data1_node *wk = mk_node();
398 static data1_element wk_element = { 0, 0, 0, 0, 0};
401 wk->root = partag->root;
402 wk->which = DATA1N_tag;
405 * get well-known tagdef if required.
407 if (!wk_element.tag && !(wk_element.tag =
408 data1_gettagbynum(wk->root->u.root.absyn->tagset, 1, 19)))
411 "Failed to initialize 'wellknown' tag from tagsetM");
414 wk->u.tag.element = &wk_element;
415 wk->child = partag->child;
417 wk->child->parent = wk;
426 * Read a record in the native syntax.
428 data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd)
430 static char *buf = 0;
436 if (!buf && !(buf = xmalloc(size = 4096)))
440 if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2)))
442 if ((res = (*rf)(fd, buf + rd, 4096)) <= 0)
447 return data1_read_node(&bp, 0, &line, 0);