2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-11-01 16:34:57 quinn
8 * Making data1 look for tables in data1_tabpath
10 * Revision 1.2 1995/11/01 13:54:48 quinn
13 * Revision 1.1 1995/11/01 11:56:09 quinn
14 * Added Retrieval (data management) functions en masse.
16 * Revision 1.14 1995/10/30 12:40:55 quinn
17 * Fixed a couple of bugs.
19 * Revision 1.13 1995/10/25 16:00:47 quinn
20 * USMARC support is now almost operational
22 * Revision 1.12 1995/10/16 14:02:55 quinn
23 * Changes to support element set names and espec1
25 * Revision 1.11 1995/10/13 16:05:08 quinn
26 * Adding Espec1-processing
28 * Revision 1.10 1995/10/11 14:53:44 quinn
31 * Revision 1.9 1995/10/06 16:56:50 quinn
32 * Fixed ranked result.
34 * Revision 1.8 1995/10/06 16:44:13 quinn
35 * Work on attribute set mapping, etc.
37 * Revision 1.7 1995/10/06 12:58:35 quinn
40 * Revision 1.6 1995/10/04 09:29:49 quinn
41 * Adjustments to support USGS test data
43 * Revision 1.5 1995/10/03 17:56:43 quinn
46 * Revision 1.4 1995/10/02 15:53:19 quinn
49 * Revision 1.3 1995/10/02 14:55:21 quinn
50 * *** empty log message ***
52 * Revision 1.2 1995/09/14 15:18:13 quinn
55 * Revision 1.1 1995/09/12 11:24:30 quinn
56 * Beginning to add code for structured records.
69 char *data1_tabpath = 0; /* global path for tables */
71 void data1_set_tabpath(char *p)
72 { data1_tabpath = p; }
74 static data1_node *freelist = 0;
77 * get the tag which is the immediate parent of this node (this may mean
78 * traversing intermediate things like variants and stuff.
80 data1_node *get_parent_tag(data1_node *n)
82 for (; n && n->which != DATA1N_root; n = n->parent)
83 if (n->which == DATA1N_tag)
88 data1_node *data1_mk_node(void)
95 if (!(r = xmalloc(sizeof(*r))))
97 r->next = r->child = r->parent = 0;
102 static void fr_node(data1_node *n)
108 void data1_free_tree(data1_node *t)
110 data1_node *p = t->child, *pn;
122 * Insert a tagged node into the record root as first child of the node at
123 * which should be root or tag itself). Returns pointer to the data node,
124 * which can then be modified.
126 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
129 data1_node *tagn = data1_mk_node();
132 tagn->which = DATA1N_tag;
135 tagn->u.tag.node_selected = 0;
136 if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn,
142 tagn->child = datn = data1_mk_node();
143 tagn->num_children = 1;
146 datn->which = DATA1N_data;
147 tagn->next = at->child;
155 * Ugh. Sometimes functions just grow and grow on you. This one reads a
156 * 'node' and its children.
158 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
163 while (**buf && isspace(**buf))
172 if (**buf == '<') /* beginning of tag */
174 char *tag = (*buf) + 1;
178 data1_element *elem = 0;
180 for (; *t && *t != '>' && !isspace(*t); t++);
181 if (*t != '>' && !isspace(*t))
183 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
186 if (isspace(*t)) /* the tag has arguments */
193 for (; *t && *t != '>'; t++);
194 if (*t != '>' && !isspace(*t))
196 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
203 * if end-tag, see if we terminate parent. If so, consume and return.
211 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
212 parent->u.root.type)) ||
213 (parent->which == DATA1N_tag && !strcmp(tag + 1,
226 if (!absyn) /* parent node - what are we? */
228 if (!(absyn = data1_get_absyn(tag)))
230 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
234 res = data1_mk_node();
235 res->which = DATA1N_root;
236 res->u.root.type = tag;
237 res->u.root.absyn = absyn;
241 else if (!strncmp(tag, "var", 3))
243 char class[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
248 if (sscanf(args, "%s %s %n", class, type, &val_offset) != 2)
250 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
253 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
258 * If we're the first variant in this group, create a parent var,
259 * and insert it before the current variant.
261 if (parent->which != DATA1N_variant)
263 res = data1_mk_node();
264 res->which = DATA1N_variant;
265 res->u.variant.type = 0;
266 res->u.variant.value = 0;
267 res->root = parent->root;
273 * now determine if one of our ancestor triples is of same type.
274 * If so, we break here. This will make the parser unwind until
275 * we become a sibling (alternate variant) to the aforementioned
276 * triple. It stinks that we re-parse these tags on every
277 * iteration of this. This is a function in need of a rewrite.
279 for (p = parent; p->which == DATA1N_variant; p = p->parent)
280 if (p->u.variant.type == tp)
286 res = data1_mk_node();
287 res->which = DATA1N_variant;
288 res->root = parent->root;
289 res->u.variant.type = tp;
290 res->u.variant.value = args + val_offset;
294 else /* acquire our element in the abstract syntax */
296 data1_node *partag = get_parent_tag(parent);
297 data1_element *e = 0;
300 if (parent->which == DATA1N_variant)
306 if (!(e = partag->u.tag.element))
307 localtag = 1; /* our parent is a local tag */
310 if (!localtag && !(elem = data1_getelementbytagname(absyn,
311 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
313 if (parent->which == DATA1N_root)
314 logf(LOG_WARN, "Tag '%s' used out of context", tag);
319 elem = data1_getelementbytagname(absyn, e, tag);
321 res = data1_mk_node();
322 res->which = DATA1N_tag;
323 res->u.tag.element = elem;
324 res->u.tag.tag = tag;
325 res->u.tag.node_selected = 0;
326 res->root = parent->root;
330 res->parent = parent;
331 res->num_children = 0;
337 while ((*pp = data1_read_node(buf, res, line, absyn)))
343 else /* != '<'... this is a body of text */
346 char *data = *buf, *pp = *buf;
348 data1_node *partag = get_parent_tag(parent);
351 /* Determine length and remove newlines/extra blanks */
352 while (**buf && **buf != '<')
360 while (isspace(**buf))
364 *(pp++) = *((*buf)++);
367 while (isspace(data[len-1]))
369 res = data1_mk_node();
370 res->parent = parent;
371 res->which = DATA1N_data;
372 res->u.data.what = DATA1I_text;
373 res->u.data.len = len;
374 res->u.data.data = data;
375 res->root = parent->root;
378 * if the parent is structured, we'll insert a 'wellKnown' marker
379 * in front of the data.
382 if (partag->u.tag.element && partag->u.tag.element->tag->kind ==
385 data1_node *wk = mk_node();
386 static data1_element wk_element = { 0, 0, 0, 0, 0};
389 wk->root = partag->root;
390 wk->which = DATA1N_tag;
393 * get well-known tagdef if required.
395 if (!wk_element.tag && !(wk_element.tag =
396 data1_gettagbynum(wk->root->u.root.absyn->tagset, 1, 19)))
399 "Failed to initialize 'wellknown' tag from tagsetM");
402 wk->u.tag.element = &wk_element;
403 wk->child = partag->child;
405 wk->child->parent = wk;
414 * Read a record in the native syntax.
416 data1_node *data1_read_record(int (*rf)(int, char *, size_t), int fd)
418 static char *buf = 0;
424 if (!buf && !(buf = xmalloc(size = 4096)))
428 if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2)))
430 if ((res = (*rf)(fd, buf + rd, 4096)) <= 0)
435 return data1_read_node(&bp, 0, &line, 0);