2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1997-09-05 09:50:57 adam
8 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
10 * Revision 1.14 1997/05/14 06:54:04 adam
13 * Revision 1.13 1996/10/29 13:35:38 adam
14 * Implemented data1_set_tabpath and data1_get_tabpath.
16 * Revision 1.12 1996/10/11 10:35:38 adam
17 * Fixed a bug that caused data1_read_node to core dump when no abstract
18 * syntax was defined in a "sgml"-record.
20 * Revision 1.11 1996/07/06 19:58:35 quinn
21 * System headerfiles gathered in yconfig
23 * Revision 1.10 1996/01/19 15:41:47 quinn
24 * Fixed uninitialized boolean.
26 * Revision 1.9 1996/01/17 14:52:47 adam
27 * Changed prototype for reader function parsed to data1_read_record.
29 * Revision 1.8 1995/12/15 16:20:41 quinn
30 * Added formatted text.
32 * Revision 1.7 1995/12/13 13:44:32 quinn
33 * Modified Data1-system to use nmem
35 * Revision 1.6 1995/12/12 16:37:08 quinn
36 * Added destroy element to data1_node.
38 * Revision 1.5 1995/12/11 15:22:37 quinn
39 * Added last_child field to the node.
40 * Rewrote schema-mapping.
42 * Revision 1.4 1995/11/13 09:27:36 quinn
43 * Fiddling with the variant stuff.
45 * Revision 1.3 1995/11/01 16:34:57 quinn
46 * Making data1 look for tables in data1_tabpath
48 * Revision 1.2 1995/11/01 13:54:48 quinn
51 * Revision 1.1 1995/11/01 11:56:09 quinn
52 * Added Retrieval (data management) functions en masse.
54 * Revision 1.14 1995/10/30 12:40:55 quinn
55 * Fixed a couple of bugs.
57 * Revision 1.13 1995/10/25 16:00:47 quinn
58 * USMARC support is now almost operational
60 * Revision 1.12 1995/10/16 14:02:55 quinn
61 * Changes to support element set names and espec1
63 * Revision 1.11 1995/10/13 16:05:08 quinn
64 * Adding Espec1-processing
66 * Revision 1.10 1995/10/11 14:53:44 quinn
69 * Revision 1.9 1995/10/06 16:56:50 quinn
70 * Fixed ranked result.
72 * Revision 1.8 1995/10/06 16:44:13 quinn
73 * Work on attribute set mapping, etc.
75 * Revision 1.7 1995/10/06 12:58:35 quinn
78 * Revision 1.6 1995/10/04 09:29:49 quinn
79 * Adjustments to support USGS test data
81 * Revision 1.5 1995/10/03 17:56:43 quinn
84 * Revision 1.4 1995/10/02 15:53:19 quinn
87 * Revision 1.3 1995/10/02 14:55:21 quinn
88 * *** empty log message ***
90 * Revision 1.2 1995/09/14 15:18:13 quinn
93 * Revision 1.1 1995/09/12 11:24:30 quinn
94 * Beginning to add code for structured records.
107 static char *data1_tabpath = 0; /* global path for tables */
109 void data1_set_tabpath(const char *p)
113 xfree (data1_tabpath);
114 data1_tabpath = NULL;
118 data1_tabpath = xmalloc (strlen(p)+1);
119 strcpy (data1_tabpath, p);
123 const char *data1_get_tabpath (void)
125 return data1_tabpath;
130 * get the tag which is the immediate parent of this node (this may mean
131 * traversing intermediate things like variants and stuff.
133 data1_node *get_parent_tag(data1_node *n)
135 for (; n && n->which != DATA1N_root; n = n->parent)
136 if (n->which == DATA1N_tag)
141 data1_node *data1_mk_node(NMEM m)
145 r = nmem_malloc(m, sizeof(*r));
146 r->next = r->child = r->last_child = r->parent = 0;
152 void data1_free_tree(data1_node *t)
154 data1_node *p = t->child, *pn;
167 * Insert a tagged node into the record root as first child of the node at
168 * which should be root or tag itself). Returns pointer to the data node,
169 * which can then be modified.
171 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
172 char *tagname, NMEM m)
174 data1_node *tagn = data1_mk_node(m);
177 tagn->which = DATA1N_tag;
180 tagn->u.tag.node_selected = 0;
181 tagn->u.tag.make_variantlist = 0;
182 tagn->u.tag.no_data_requested = 0;
183 tagn->u.tag.get_bytes = -1;
184 if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn,
187 tagn->child = datn = data1_mk_node(m);
188 tagn->num_children = 1;
191 datn->which = DATA1N_data;
192 datn->u.data.formatted_text = 0;
193 tagn->next = at->child;
201 * Ugh. Sometimes functions just grow and grow on you. This one reads a
202 * 'node' and its children.
204 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
205 data1_absyn *absyn, NMEM m)
209 while (**buf && isspace(**buf))
218 if (**buf == '<') /* beginning of tag */
220 char *tag = (*buf) + 1;
224 data1_element *elem = 0;
226 for (; *t && *t != '>' && !isspace(*t); t++);
227 if (*t != '>' && !isspace(*t))
229 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
232 if (isspace(*t)) /* the tag has arguments */
239 for (; *t && *t != '>'; t++);
240 if (*t != '>' && !isspace(*t))
242 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
249 * if end-tag, see if we terminate parent. If so, consume and return.
257 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
258 parent->u.root.type)) ||
259 (parent->which == DATA1N_tag && !strcmp(tag + 1,
272 if (!absyn) /* parent node - what are we? */
274 if (!(absyn = data1_get_absyn(tag)))
276 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
280 res = data1_mk_node(m);
281 res->which = DATA1N_root;
282 res->u.root.type = tag;
283 res->u.root.absyn = absyn;
287 else if (!strncmp(tag, "var", 3))
289 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
294 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
296 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
299 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
304 * If we're the first variant in this group, create a parent var,
305 * and insert it before the current variant.
307 if (parent->which != DATA1N_variant)
309 res = data1_mk_node(m);
310 res->which = DATA1N_variant;
311 res->u.variant.type = 0;
312 res->u.variant.value = 0;
313 res->root = parent->root;
319 * now determine if one of our ancestor triples is of same type.
320 * If so, we break here. This will make the parser unwind until
321 * we become a sibling (alternate variant) to the aforementioned
322 * triple. It stinks that we re-parse these tags on every
323 * iteration of this. This is a function in need of a rewrite.
325 for (p = parent; p->which == DATA1N_variant; p = p->parent)
326 if (p->u.variant.type == tp)
332 res = data1_mk_node(m);
333 res->which = DATA1N_variant;
334 res->root = parent->root;
335 res->u.variant.type = tp;
336 res->u.variant.value = args + val_offset;
340 else /* tag.. acquire our element in the abstract syntax */
342 data1_node *partag = get_parent_tag(parent);
343 data1_element *e = 0;
346 if (parent->which == DATA1N_variant)
352 if (!(e = partag->u.tag.element))
353 localtag = 1; /* our parent is a local tag */
356 if (!localtag && !(elem = data1_getelementbytagname(absyn,
357 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
359 if (parent->which == DATA1N_root)
360 logf(LOG_WARN, "Tag '%s' used out of context", tag);
365 elem = data1_getelementbytagname(absyn, e, tag);
367 res = data1_mk_node(m);
368 res->which = DATA1N_tag;
369 res->u.tag.element = elem;
370 res->u.tag.tag = tag;
371 res->u.tag.node_selected = 0;
372 res->u.tag.make_variantlist = 0;
373 res->u.tag.no_data_requested = 0;
374 res->u.tag.get_bytes = -1;
375 res->root = parent->root;
379 res->parent = parent;
380 res->num_children = 0;
386 while ((*pp = data1_read_node(buf, res, line, absyn, m)))
388 res->last_child = *pp;
393 else /* != '<'... this is a body of text */
396 char *data = *buf, *pp = *buf;
398 if (!parent) /* abort if abstract syntax is undefined */
400 /* Determine length and remove newlines/extra blanks */
401 while (**buf && **buf != '<')
409 while (isspace(**buf))
413 *(pp++) = *((*buf)++);
416 while (isspace(data[len-1]))
418 res = data1_mk_node(m);
419 res->parent = parent;
420 res->which = DATA1N_data;
421 res->u.data.what = DATA1I_text;
422 res->u.data.len = len;
423 res->u.data.data = data;
424 res->u.data.formatted_text = 0;
425 res->root = parent->root;
431 * Read a record in the native syntax.
433 data1_node *data1_read_record(int (*rf)(void *, char *, size_t), void *fh,
436 static char *buf = 0;
442 if (!buf && !(buf = xmalloc(size = 4096)))
447 if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2)))
449 if ((res = (*rf)(fh, buf + rd, 4096)) <= 0)
454 return data1_read_node(&bp, 0, &line, 0, m);