2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-12-13 13:44:32 quinn
8 * Modified Data1-system to use nmem
10 * Revision 1.6 1995/12/12 16:37:08 quinn
11 * Added destroy element to data1_node.
13 * Revision 1.5 1995/12/11 15:22:37 quinn
14 * Added last_child field to the node.
15 * Rewrote schema-mapping.
17 * Revision 1.4 1995/11/13 09:27:36 quinn
18 * Fiddling with the variant stuff.
20 * Revision 1.3 1995/11/01 16:34:57 quinn
21 * Making data1 look for tables in data1_tabpath
23 * Revision 1.2 1995/11/01 13:54:48 quinn
26 * Revision 1.1 1995/11/01 11:56:09 quinn
27 * Added Retrieval (data management) functions en masse.
29 * Revision 1.14 1995/10/30 12:40:55 quinn
30 * Fixed a couple of bugs.
32 * Revision 1.13 1995/10/25 16:00:47 quinn
33 * USMARC support is now almost operational
35 * Revision 1.12 1995/10/16 14:02:55 quinn
36 * Changes to support element set names and espec1
38 * Revision 1.11 1995/10/13 16:05:08 quinn
39 * Adding Espec1-processing
41 * Revision 1.10 1995/10/11 14:53:44 quinn
44 * Revision 1.9 1995/10/06 16:56:50 quinn
45 * Fixed ranked result.
47 * Revision 1.8 1995/10/06 16:44:13 quinn
48 * Work on attribute set mapping, etc.
50 * Revision 1.7 1995/10/06 12:58:35 quinn
53 * Revision 1.6 1995/10/04 09:29:49 quinn
54 * Adjustments to support USGS test data
56 * Revision 1.5 1995/10/03 17:56:43 quinn
59 * Revision 1.4 1995/10/02 15:53:19 quinn
62 * Revision 1.3 1995/10/02 14:55:21 quinn
63 * *** empty log message ***
65 * Revision 1.2 1995/09/14 15:18:13 quinn
68 * Revision 1.1 1995/09/12 11:24:30 quinn
69 * Beginning to add code for structured records.
82 char *data1_tabpath = 0; /* global path for tables */
84 void data1_set_tabpath(char *p)
85 { data1_tabpath = p; }
88 static data1_node *freelist = 0;
92 * get the tag which is the immediate parent of this node (this may mean
93 * traversing intermediate things like variants and stuff.
95 data1_node *get_parent_tag(data1_node *n)
97 for (; n && n->which != DATA1N_root; n = n->parent)
98 if (n->which == DATA1N_tag)
103 data1_node *data1_mk_node(NMEM m)
111 if (!(r = xmalloc(sizeof(*r))))
114 r = nmem_malloc(m, sizeof(*r));
116 r->next = r->child = r->last_child = r->parent = 0;
123 static void fr_node(data1_node *n)
130 void data1_free_tree(data1_node *t)
132 data1_node *p = t->child, *pn;
148 * Insert a tagged node into the record root as first child of the node at
149 * which should be root or tag itself). Returns pointer to the data node,
150 * which can then be modified.
152 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
153 char *tagname, NMEM m)
155 data1_node *tagn = data1_mk_node(m);
158 tagn->which = DATA1N_tag;
161 tagn->u.tag.node_selected = 0;
162 tagn->u.tag.make_variantlist = 0;
163 tagn->u.tag.no_data_requested = 0;
164 if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn,
167 tagn->child = datn = data1_mk_node(m);
168 tagn->num_children = 1;
171 datn->which = DATA1N_data;
172 tagn->next = at->child;
180 * Ugh. Sometimes functions just grow and grow on you. This one reads a
181 * 'node' and its children.
183 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
184 data1_absyn *absyn, NMEM m)
188 while (**buf && isspace(**buf))
197 if (**buf == '<') /* beginning of tag */
199 char *tag = (*buf) + 1;
203 data1_element *elem = 0;
205 for (; *t && *t != '>' && !isspace(*t); t++);
206 if (*t != '>' && !isspace(*t))
208 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
211 if (isspace(*t)) /* the tag has arguments */
218 for (; *t && *t != '>'; t++);
219 if (*t != '>' && !isspace(*t))
221 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
228 * if end-tag, see if we terminate parent. If so, consume and return.
236 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
237 parent->u.root.type)) ||
238 (parent->which == DATA1N_tag && !strcmp(tag + 1,
251 if (!absyn) /* parent node - what are we? */
253 if (!(absyn = data1_get_absyn(tag)))
255 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
259 res = data1_mk_node(m);
260 res->which = DATA1N_root;
261 res->u.root.type = tag;
262 res->u.root.absyn = absyn;
266 else if (!strncmp(tag, "var", 3))
268 char class[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
273 if (sscanf(args, "%s %s %n", class, type, &val_offset) != 2)
275 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
278 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
283 * If we're the first variant in this group, create a parent var,
284 * and insert it before the current variant.
286 if (parent->which != DATA1N_variant)
288 res = data1_mk_node(m);
289 res->which = DATA1N_variant;
290 res->u.variant.type = 0;
291 res->u.variant.value = 0;
292 res->root = parent->root;
298 * now determine if one of our ancestor triples is of same type.
299 * If so, we break here. This will make the parser unwind until
300 * we become a sibling (alternate variant) to the aforementioned
301 * triple. It stinks that we re-parse these tags on every
302 * iteration of this. This is a function in need of a rewrite.
304 for (p = parent; p->which == DATA1N_variant; p = p->parent)
305 if (p->u.variant.type == tp)
311 res = data1_mk_node(m);
312 res->which = DATA1N_variant;
313 res->root = parent->root;
314 res->u.variant.type = tp;
315 res->u.variant.value = args + val_offset;
319 else /* tag.. acquire our element in the abstract syntax */
321 data1_node *partag = get_parent_tag(parent);
322 data1_element *e = 0;
325 if (parent->which == DATA1N_variant)
331 if (!(e = partag->u.tag.element))
332 localtag = 1; /* our parent is a local tag */
335 if (!localtag && !(elem = data1_getelementbytagname(absyn,
336 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
338 if (parent->which == DATA1N_root)
339 logf(LOG_WARN, "Tag '%s' used out of context", tag);
344 elem = data1_getelementbytagname(absyn, e, tag);
346 res = data1_mk_node(m);
347 res->which = DATA1N_tag;
348 res->u.tag.element = elem;
349 res->u.tag.tag = tag;
350 res->u.tag.node_selected = 0;
351 res->u.tag.make_variantlist = 0;
352 res->u.tag.no_data_requested = 0;
353 res->root = parent->root;
357 res->parent = parent;
358 res->num_children = 0;
364 while ((*pp = data1_read_node(buf, res, line, absyn, m)))
366 res->last_child = *pp;
371 else /* != '<'... this is a body of text */
374 char *data = *buf, *pp = *buf;
376 data1_node *partag = get_parent_tag(parent);
379 /* Determine length and remove newlines/extra blanks */
380 while (**buf && **buf != '<')
388 while (isspace(**buf))
392 *(pp++) = *((*buf)++);
395 while (isspace(data[len-1]))
397 res = data1_mk_node(m);
398 res->parent = parent;
399 res->which = DATA1N_data;
400 res->u.data.what = DATA1I_text;
401 res->u.data.len = len;
402 res->u.data.data = data;
403 res->root = parent->root;
409 * Read a record in the native syntax.
411 data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd, NMEM m)
413 static char *buf = 0;
419 if (!buf && !(buf = xmalloc(size = 4096)))
424 if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2)))
426 if ((res = (*rf)(fd, buf + rd, 4096)) <= 0)
431 return data1_read_node(&bp, 0, &line, 0, m);