2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-11-01 11:56:09 quinn
8 * Added Retrieval (data management) functions en masse.
10 * Revision 1.14 1995/10/30 12:40:55 quinn
11 * Fixed a couple of bugs.
13 * Revision 1.13 1995/10/25 16:00:47 quinn
14 * USMARC support is now almost operational
16 * Revision 1.12 1995/10/16 14:02:55 quinn
17 * Changes to support element set names and espec1
19 * Revision 1.11 1995/10/13 16:05:08 quinn
20 * Adding Espec1-processing
22 * Revision 1.10 1995/10/11 14:53:44 quinn
25 * Revision 1.9 1995/10/06 16:56:50 quinn
26 * Fixed ranked result.
28 * Revision 1.8 1995/10/06 16:44:13 quinn
29 * Work on attribute set mapping, etc.
31 * Revision 1.7 1995/10/06 12:58:35 quinn
34 * Revision 1.6 1995/10/04 09:29:49 quinn
35 * Adjustments to support USGS test data
37 * Revision 1.5 1995/10/03 17:56:43 quinn
40 * Revision 1.4 1995/10/02 15:53:19 quinn
43 * Revision 1.3 1995/10/02 14:55:21 quinn
44 * *** empty log message ***
46 * Revision 1.2 1995/09/14 15:18:13 quinn
49 * Revision 1.1 1995/09/12 11:24:30 quinn
50 * Beginning to add code for structured records.
63 static data1_node *freelist = 0;
66 * get the tag which is the immediate parent of this node (this may mean
67 * traversing intermediate things like variants and stuff.
69 data1_node *get_parent_tag(data1_node *n)
71 for (; n && n->which != DATA1N_root; n = n->parent)
72 if (n->which == DATA1N_tag)
77 data1_node *data1_mk_node(void)
84 if (!(r = xmalloc(sizeof(*r))))
86 r->next = r->child = r->parent = 0;
91 static void fr_node(data1_node *n)
97 void data1_free_tree(data1_node *t)
99 data1_node *p = t->child, *pn;
111 * Insert a tagged node into the record root as first child of the node at
112 * which should be root or tag itself). Returns pointer to the data node,
113 * which can then be modified.
115 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
118 data1_node *tagn = data1_mk_node();
121 tagn->which = DATA1N_tag;
124 tagn->u.tag.node_selected = 0;
125 if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn,
131 tagn->child = datn = data1_mk_node();
132 tagn->num_children = 1;
135 datn->which = DATA1N_data;
136 tagn->next = at->child;
144 * Ugh. Sometimes functions just grow and grow on you. This one reads a
145 * 'node' and its children.
147 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
152 while (**buf && isspace(**buf))
161 if (**buf == '<') /* beginning of tag */
163 char *tag = (*buf) + 1;
167 data1_element *elem = 0;
169 for (; *t && *t != '>' && !isspace(*t); t++);
170 if (*t != '>' && !isspace(*t))
172 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
175 if (isspace(*t)) /* the tag has arguments */
182 for (; *t && *t != '>'; t++);
183 if (*t != '>' && !isspace(*t))
185 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
192 * if end-tag, see if we terminate parent. If so, consume and return.
200 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
201 parent->u.root.type)) ||
202 (parent->which == DATA1N_tag && !strcmp(tag + 1,
215 if (!absyn) /* parent node - what are we? */
217 if (!(absyn = data1_get_absyn(tag)))
219 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
223 res = data1_mk_node();
224 res->which = DATA1N_root;
225 res->u.root.type = tag;
226 res->u.root.absyn = absyn;
230 else if (!strncmp(tag, "var", 3))
232 char class[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
237 if (sscanf(args, "%s %s %n", class, type, &val_offset) != 2)
239 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
242 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
247 * If we're the first variant in this group, create a parent var,
248 * and insert it before the current variant.
250 if (parent->which != DATA1N_variant)
252 res = data1_mk_node();
253 res->which = DATA1N_variant;
254 res->u.variant.type = 0;
255 res->u.variant.value = 0;
256 res->root = parent->root;
262 * now determine if one of our ancestor triples is of same type.
263 * If so, we break here. This will make the parser unwind until
264 * we become a sibling (alternate variant) to the aforementioned
265 * triple. It stinks that we re-parse these tags on every
266 * iteration of this. This is a function in need of a rewrite.
268 for (p = parent; p->which == DATA1N_variant; p = p->parent)
269 if (p->u.variant.type == tp)
275 res = data1_mk_node();
276 res->which = DATA1N_variant;
277 res->root = parent->root;
278 res->u.variant.type = tp;
279 res->u.variant.value = args + val_offset;
283 else /* acquire our element in the abstract syntax */
285 data1_node *partag = get_parent_tag(parent);
286 data1_element *e = 0;
289 if (parent->which == DATA1N_variant)
295 if (!(e = partag->u.tag.element))
296 localtag = 1; /* our parent is a local tag */
299 if (!localtag && !(elem = data1_getelementbytagname(absyn,
300 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
302 if (parent->which == DATA1N_root)
303 logf(LOG_WARN, "Tag '%s' used out of context", tag);
308 elem = data1_getelementbytagname(absyn, e, tag);
310 res = data1_mk_node();
311 res->which = DATA1N_tag;
312 res->u.tag.element = elem;
313 res->u.tag.tag = tag;
314 res->u.tag.node_selected = 0;
315 res->root = parent->root;
319 res->parent = parent;
320 res->num_children = 0;
326 while ((*pp = data1_read_node(buf, res, line, absyn)))
332 else /* != '<'... this is a body of text */
335 char *data = *buf, *pp = *buf;
337 data1_node *partag = get_parent_tag(parent);
340 /* Determine length and remove newlines/extra blanks */
341 while (**buf && **buf != '<')
349 while (isspace(**buf))
353 *(pp++) = *((*buf)++);
356 while (isspace(data[len-1]))
358 res = data1_mk_node();
359 res->parent = parent;
360 res->which = DATA1N_data;
361 res->u.data.what = DATA1I_text;
362 res->u.data.len = len;
363 res->u.data.data = data;
364 res->root = parent->root;
367 * if the parent is structured, we'll insert a 'wellKnown' marker
368 * in front of the data.
371 if (partag->u.tag.element && partag->u.tag.element->tag->kind ==
374 data1_node *wk = mk_node();
375 static data1_element wk_element = { 0, 0, 0, 0, 0};
378 wk->root = partag->root;
379 wk->which = DATA1N_tag;
382 * get well-known tagdef if required.
384 if (!wk_element.tag && !(wk_element.tag =
385 data1_gettagbynum(wk->root->u.root.absyn->tagset, 1, 19)))
388 "Failed to initialize 'wellknown' tag from tagsetM");
391 wk->u.tag.element = &wk_element;
392 wk->child = partag->child;
394 wk->child->parent = wk;
403 * Read a record in the native syntax.
405 data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd)
407 static char *buf = 0;
413 if (!buf && !(buf = xmalloc(size = 4096)))
417 if (rd + 4096 > size && !(buf = realloc(buf, size *= 2)))
419 if ((res = (*rf)(fd, buf + rd, 4096)) <= 0)
424 return data1_read_node(&bp, 0, &line, 0);