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:48 quinn
10 * Revision 1.1 1995/11/01 11:56:09 quinn
11 * Added Retrieval (data management) functions en masse.
13 * Revision 1.14 1995/10/30 12:40:55 quinn
14 * Fixed a couple of bugs.
16 * Revision 1.13 1995/10/25 16:00:47 quinn
17 * USMARC support is now almost operational
19 * Revision 1.12 1995/10/16 14:02:55 quinn
20 * Changes to support element set names and espec1
22 * Revision 1.11 1995/10/13 16:05:08 quinn
23 * Adding Espec1-processing
25 * Revision 1.10 1995/10/11 14:53:44 quinn
28 * Revision 1.9 1995/10/06 16:56:50 quinn
29 * Fixed ranked result.
31 * Revision 1.8 1995/10/06 16:44:13 quinn
32 * Work on attribute set mapping, etc.
34 * Revision 1.7 1995/10/06 12:58:35 quinn
37 * Revision 1.6 1995/10/04 09:29:49 quinn
38 * Adjustments to support USGS test data
40 * Revision 1.5 1995/10/03 17:56:43 quinn
43 * Revision 1.4 1995/10/02 15:53:19 quinn
46 * Revision 1.3 1995/10/02 14:55:21 quinn
47 * *** empty log message ***
49 * Revision 1.2 1995/09/14 15:18:13 quinn
52 * Revision 1.1 1995/09/12 11:24:30 quinn
53 * Beginning to add code for structured records.
66 static data1_node *freelist = 0;
69 * get the tag which is the immediate parent of this node (this may mean
70 * traversing intermediate things like variants and stuff.
72 data1_node *get_parent_tag(data1_node *n)
74 for (; n && n->which != DATA1N_root; n = n->parent)
75 if (n->which == DATA1N_tag)
80 data1_node *data1_mk_node(void)
87 if (!(r = xmalloc(sizeof(*r))))
89 r->next = r->child = r->parent = 0;
94 static void fr_node(data1_node *n)
100 void data1_free_tree(data1_node *t)
102 data1_node *p = t->child, *pn;
114 * Insert a tagged node into the record root as first child of the node at
115 * which should be root or tag itself). Returns pointer to the data node,
116 * which can then be modified.
118 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
121 data1_node *tagn = data1_mk_node();
124 tagn->which = DATA1N_tag;
127 tagn->u.tag.node_selected = 0;
128 if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn,
134 tagn->child = datn = data1_mk_node();
135 tagn->num_children = 1;
138 datn->which = DATA1N_data;
139 tagn->next = at->child;
147 * Ugh. Sometimes functions just grow and grow on you. This one reads a
148 * 'node' and its children.
150 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
155 while (**buf && isspace(**buf))
164 if (**buf == '<') /* beginning of tag */
166 char *tag = (*buf) + 1;
170 data1_element *elem = 0;
172 for (; *t && *t != '>' && !isspace(*t); t++);
173 if (*t != '>' && !isspace(*t))
175 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
178 if (isspace(*t)) /* the tag has arguments */
185 for (; *t && *t != '>'; t++);
186 if (*t != '>' && !isspace(*t))
188 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
195 * if end-tag, see if we terminate parent. If so, consume and return.
203 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
204 parent->u.root.type)) ||
205 (parent->which == DATA1N_tag && !strcmp(tag + 1,
218 if (!absyn) /* parent node - what are we? */
220 if (!(absyn = data1_get_absyn(tag)))
222 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
226 res = data1_mk_node();
227 res->which = DATA1N_root;
228 res->u.root.type = tag;
229 res->u.root.absyn = absyn;
233 else if (!strncmp(tag, "var", 3))
235 char class[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
240 if (sscanf(args, "%s %s %n", class, type, &val_offset) != 2)
242 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
245 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
250 * If we're the first variant in this group, create a parent var,
251 * and insert it before the current variant.
253 if (parent->which != DATA1N_variant)
255 res = data1_mk_node();
256 res->which = DATA1N_variant;
257 res->u.variant.type = 0;
258 res->u.variant.value = 0;
259 res->root = parent->root;
265 * now determine if one of our ancestor triples is of same type.
266 * If so, we break here. This will make the parser unwind until
267 * we become a sibling (alternate variant) to the aforementioned
268 * triple. It stinks that we re-parse these tags on every
269 * iteration of this. This is a function in need of a rewrite.
271 for (p = parent; p->which == DATA1N_variant; p = p->parent)
272 if (p->u.variant.type == tp)
278 res = data1_mk_node();
279 res->which = DATA1N_variant;
280 res->root = parent->root;
281 res->u.variant.type = tp;
282 res->u.variant.value = args + val_offset;
286 else /* acquire our element in the abstract syntax */
288 data1_node *partag = get_parent_tag(parent);
289 data1_element *e = 0;
292 if (parent->which == DATA1N_variant)
298 if (!(e = partag->u.tag.element))
299 localtag = 1; /* our parent is a local tag */
302 if (!localtag && !(elem = data1_getelementbytagname(absyn,
303 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
305 if (parent->which == DATA1N_root)
306 logf(LOG_WARN, "Tag '%s' used out of context", tag);
311 elem = data1_getelementbytagname(absyn, e, tag);
313 res = data1_mk_node();
314 res->which = DATA1N_tag;
315 res->u.tag.element = elem;
316 res->u.tag.tag = tag;
317 res->u.tag.node_selected = 0;
318 res->root = parent->root;
322 res->parent = parent;
323 res->num_children = 0;
329 while ((*pp = data1_read_node(buf, res, line, absyn)))
335 else /* != '<'... this is a body of text */
338 char *data = *buf, *pp = *buf;
340 data1_node *partag = get_parent_tag(parent);
343 /* Determine length and remove newlines/extra blanks */
344 while (**buf && **buf != '<')
352 while (isspace(**buf))
356 *(pp++) = *((*buf)++);
359 while (isspace(data[len-1]))
361 res = data1_mk_node();
362 res->parent = parent;
363 res->which = DATA1N_data;
364 res->u.data.what = DATA1I_text;
365 res->u.data.len = len;
366 res->u.data.data = data;
367 res->root = parent->root;
370 * if the parent is structured, we'll insert a 'wellKnown' marker
371 * in front of the data.
374 if (partag->u.tag.element && partag->u.tag.element->tag->kind ==
377 data1_node *wk = mk_node();
378 static data1_element wk_element = { 0, 0, 0, 0, 0};
381 wk->root = partag->root;
382 wk->which = DATA1N_tag;
385 * get well-known tagdef if required.
387 if (!wk_element.tag && !(wk_element.tag =
388 data1_gettagbynum(wk->root->u.root.absyn->tagset, 1, 19)))
391 "Failed to initialize 'wellknown' tag from tagsetM");
394 wk->u.tag.element = &wk_element;
395 wk->child = partag->child;
397 wk->child->parent = wk;
406 * Read a record in the native syntax.
408 data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd)
410 static char *buf = 0;
416 if (!buf && !(buf = xmalloc(size = 4096)))
420 if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2)))
422 if ((res = (*rf)(fd, buf + rd, 4096)) <= 0)
427 return data1_read_node(&bp, 0, &line, 0);