2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1998-02-11 11:53:35 adam
8 * Changed code so that it compiles as C++.
10 * Revision 1.19 1997/12/09 16:17:09 adam
11 * Fix bug regarding variants. Tags with prefix "var" was incorrectly
12 * interpreted as "start of variants". Now, only "var" indicates such
14 * Cleaned up data1_read_node so tag names and variant names are
15 * copied and not pointed to by the generated data1 tree. Data nodes
16 * still point to old buffer.
18 * Revision 1.18 1997/11/18 09:51:09 adam
19 * Removed element num_children from data1_node. Minor changes in
22 * Revision 1.17 1997/11/05 09:20:51 adam
25 * Revision 1.16 1997/09/17 12:10:37 adam
28 * Revision 1.15 1997/09/05 09:50:57 adam
29 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
31 * Revision 1.14 1997/05/14 06:54:04 adam
34 * Revision 1.13 1996/10/29 13:35:38 adam
35 * Implemented data1_set_tabpath and data1_get_tabpath.
37 * Revision 1.12 1996/10/11 10:35:38 adam
38 * Fixed a bug that caused data1_read_node to core dump when no abstract
39 * syntax was defined in a "sgml"-record.
41 * Revision 1.11 1996/07/06 19:58:35 quinn
42 * System headerfiles gathered in yconfig
44 * Revision 1.10 1996/01/19 15:41:47 quinn
45 * Fixed uninitialized boolean.
47 * Revision 1.9 1996/01/17 14:52:47 adam
48 * Changed prototype for reader function parsed to data1_read_record.
50 * Revision 1.8 1995/12/15 16:20:41 quinn
51 * Added formatted text.
53 * Revision 1.7 1995/12/13 13:44:32 quinn
54 * Modified Data1-system to use nmem
56 * Revision 1.6 1995/12/12 16:37:08 quinn
57 * Added destroy element to data1_node.
59 * Revision 1.5 1995/12/11 15:22:37 quinn
60 * Added last_child field to the node.
61 * Rewrote schema-mapping.
63 * Revision 1.4 1995/11/13 09:27:36 quinn
64 * Fiddling with the variant stuff.
66 * Revision 1.3 1995/11/01 16:34:57 quinn
67 * Making data1 look for tables in data1_tabpath
69 * Revision 1.2 1995/11/01 13:54:48 quinn
72 * Revision 1.1 1995/11/01 11:56:09 quinn
73 * Added Retrieval (data management) functions en masse.
75 * Revision 1.14 1995/10/30 12:40:55 quinn
76 * Fixed a couple of bugs.
78 * Revision 1.13 1995/10/25 16:00:47 quinn
79 * USMARC support is now almost operational
81 * Revision 1.12 1995/10/16 14:02:55 quinn
82 * Changes to support element set names and espec1
84 * Revision 1.11 1995/10/13 16:05:08 quinn
85 * Adding Espec1-processing
87 * Revision 1.10 1995/10/11 14:53:44 quinn
90 * Revision 1.9 1995/10/06 16:56:50 quinn
91 * Fixed ranked result.
93 * Revision 1.8 1995/10/06 16:44:13 quinn
94 * Work on attribute set mapping, etc.
96 * Revision 1.7 1995/10/06 12:58:35 quinn
99 * Revision 1.6 1995/10/04 09:29:49 quinn
100 * Adjustments to support USGS test data
102 * Revision 1.5 1995/10/03 17:56:43 quinn
105 * Revision 1.4 1995/10/02 15:53:19 quinn
108 * Revision 1.3 1995/10/02 14:55:21 quinn
109 * *** empty log message ***
111 * Revision 1.2 1995/09/14 15:18:13 quinn
114 * Revision 1.1 1995/09/12 11:24:30 quinn
115 * Beginning to add code for structured records.
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_handle dh, 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 (data1_handle dh, NMEM m)
145 r = (data1_node *)nmem_malloc(m, sizeof(*r));
146 r->next = r->child = r->last_child = r->parent = 0;
151 void data1_free_tree (data1_handle dh, data1_node *t)
153 data1_node *p = t->child, *pn;
158 data1_free_tree (dh, p);
165 char *data1_insert_string (data1_handle dh, data1_node *res,
166 NMEM m, const char *str)
168 int len = strlen(str);
170 if (len > DATA1_LOCALDATA-1)
171 return nmem_strdup (m, str);
174 strcpy (res->lbuf, str);
180 * Insert a tagged node into the record root as first child of the node at
181 * which should be root or tag itself). Returns pointer to the data node,
182 * which can then be modified.
184 data1_node *data1_insert_taggeddata(data1_handle dh, data1_node *root,
186 char *tagname, NMEM m)
188 data1_node *tagn = data1_mk_node (dh, m);
191 tagn->which = DATA1N_tag;
194 tagn->u.tag.node_selected = 0;
195 tagn->u.tag.make_variantlist = 0;
196 tagn->u.tag.no_data_requested = 0;
197 tagn->u.tag.get_bytes = -1;
198 if (!(tagn->u.tag.element =
199 data1_getelementbytagname (dh, root->u.root.absyn, 0, tagname)))
201 tagn->child = datn = data1_mk_node (dh, m);
204 datn->which = DATA1N_data;
205 datn->u.data.formatted_text = 0;
206 tagn->next = at->child;
213 * Ugh. Sometimes functions just grow and grow on you. This one reads a
214 * 'node' and its children.
216 data1_node *data1_read_node (data1_handle dh, char **buf,
217 data1_node *parent, int *line,
218 data1_absyn *absyn, NMEM m)
222 while (**buf && isspace(**buf))
231 if (**buf == '<') /* beginning of tag */
236 char *t = (*buf) + 1;
238 data1_element *elem = 0;
240 for (i = 0; *t && *t != '>' && !isspace(*t); t++)
241 if (i < (sizeof(tag)-1))
246 for (i = 0; *t && *t != '>'; t++)
247 if (i < (sizeof(args)-1))
250 if (*t != '>' && !isspace(*t))
252 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
256 * if end-tag, see if we terminate parent. If so, consume and return.
261 if (parent && (!*(tag +1) ||
262 (parent->which == DATA1N_root &&
263 !strcmp(tag + 1,parent->u.root.type)) ||
264 (parent->which == DATA1N_tag &&
265 !strcmp(tag + 1, parent->u.tag.tag))))
269 if (!absyn) /* parent node - what are we? */
271 if (!(absyn = data1_get_absyn (dh, tag)))
273 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
277 res = data1_mk_node (dh, m);
278 res->which = DATA1N_root;
279 res->u.root.type = data1_insert_string (dh, res, m, tag);
280 res->u.root.absyn = absyn;
284 else if (!strcmp(tag, "var"))
286 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
291 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
293 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
297 data1_getvartypebyct(dh, parent->root->u.root.absyn->varset,
302 * If we're the first variant in this group, create a parent var,
303 * and insert it before the current variant.
305 if (parent->which != DATA1N_variant)
307 res = data1_mk_node (dh, m);
308 res->which = DATA1N_variant;
309 res->u.variant.type = 0;
310 res->u.variant.value = 0;
311 res->root = parent->root;
316 * now determine if one of our ancestor triples is of same type.
317 * If so, we break here. This will make the parser unwind until
318 * we become a sibling (alternate variant) to the aforementioned
319 * triple. It stinks that we re-parse these tags on every
320 * iteration of this. This is a function in need of a rewrite.
322 for (p = parent; p->which == DATA1N_variant; p = p->parent)
323 if (p->u.variant.type == tp)
325 res = data1_mk_node (dh, m);
326 res->which = DATA1N_variant;
327 res->root = parent->root;
328 res->u.variant.type = tp;
329 res->u.variant.value =
330 data1_insert_string (dh, res, m, args + val_offset);
334 else /* tag.. acquire our element in the abstract syntax */
336 data1_node *partag = get_parent_tag (dh, parent);
337 data1_element *e = 0;
340 if (parent->which == DATA1N_variant)
343 if (!(e = partag->u.tag.element))
344 localtag = 1; /* our parent is a local tag */
346 elem = data1_getelementbytagname(dh, absyn, e, tag);
347 res = data1_mk_node (dh, m);
348 res->which = DATA1N_tag;
349 res->u.tag.tag = data1_insert_string (dh, res, m, tag);
350 res->u.tag.element = elem;
351 res->u.tag.node_selected = 0;
352 res->u.tag.make_variantlist = 0;
353 res->u.tag.no_data_requested = 0;
354 res->u.tag.get_bytes = -1;
355 res->root = parent->root;
359 res->parent = parent;
364 while ((*pp = data1_read_node(dh, buf, res, line, absyn, m)))
366 res->last_child = *pp;
370 else /* != '<'... this is a body of text */
373 char *data = *buf, *pp = *buf;
375 if (!parent) /* abort if abstract syntax is undefined */
378 /* Determine length and remove newlines/extra blanks */
379 while (**buf && **buf != '<')
387 while (isspace(**buf))
391 *(pp++) = *((*buf)++);
394 while (isspace(data[len-1]))
396 res = data1_mk_node(dh, m);
397 res->parent = parent;
398 res->which = DATA1N_data;
399 res->u.data.what = DATA1I_text;
401 res->u.data.len = len;
402 res->u.data.data = data;
403 res->u.data.formatted_text = 0;
404 res->root = parent->root;
410 * Read a record in the native syntax.
412 data1_node *data1_read_record(data1_handle dh,
413 int (*rf)(void *, char *, size_t), void *fh,
417 char **buf = data1_get_read_buf (dh, &size);
423 *buf = (char *)xmalloc(*size = 4096);
427 if (rd + 4096 > *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
429 if ((res = (*rf)(fh, *buf + rd, 4096)) <= 0)
434 return data1_read_node(dh, &bp, 0, &line, 0, m);