2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.31 1999-12-21 14:16:20 ian
8 * Changed retrieval module to allow data1 trees with no associated absyn.
9 * Also added a simple interface for extracting values from data1 trees using
10 * a string based tagpath.
12 * Revision 1.30 1999/11/30 13:47:12 adam
13 * Improved installation. Moved header files to include/yaz.
15 * Revision 1.29 1999/10/21 12:06:29 adam
16 * Retrieval module no longer uses ctype.h - functions.
18 * Revision 1.28 1999/10/21 09:50:33 adam
19 * SGML reader uses own isspace - it doesn't do 8-bit on WIN32!
21 * Revision 1.27 1999/08/27 09:40:32 adam
22 * Renamed logf function to yaz_log. Removed VC++ project files.
24 * Revision 1.26 1999/07/13 13:23:48 adam
25 * Non-recursive version of data1_read_node. data1_read_nodex reads
26 * stream of bytes (instead of buffer in memory).
28 * Revision 1.25 1999/04/20 09:56:48 adam
29 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
30 * Modified all encoders/decoders to reflect this change.
32 * Revision 1.24 1998/10/28 15:10:09 adam
33 * Added --with-yc option to configure. For the data1_node in data1.h:
34 * decreased size of localdata and removed member "line" which wasn't useful.
36 * Revision 1.23 1998/03/12 11:28:45 adam
37 * Fix: didn't set root member of tagged node in function.
38 * data1_add_insert_taggeddata.
40 * Revision 1.22 1998/03/05 08:15:32 adam
41 * Implemented data1_add_insert_taggeddata utility which is more flexible
42 * than data1_insert_taggeddata.
44 * Revision 1.21 1998/02/27 14:08:05 adam
45 * Added const to some char pointer arguments.
46 * Reworked data1_read_node so that it doesn't create a tree with
47 * pointers to original "SGML"-buffer.
49 * Revision 1.20 1998/02/11 11:53:35 adam
50 * Changed code so that it compiles as C++.
52 * Revision 1.19 1997/12/09 16:17:09 adam
53 * Fix bug regarding variants. Tags with prefix "var" was incorrectly
54 * interpreted as "start of variants". Now, only "var" indicates such
56 * Cleaned up data1_read_node so tag names and variant names are
57 * copied and not pointed to by the generated data1 tree. Data nodes
58 * still point to old buffer.
60 * Revision 1.18 1997/11/18 09:51:09 adam
61 * Removed element num_children from data1_node. Minor changes in
64 * Revision 1.17 1997/11/05 09:20:51 adam
67 * Revision 1.16 1997/09/17 12:10:37 adam
70 * Revision 1.15 1997/09/05 09:50:57 adam
71 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
73 * Revision 1.14 1997/05/14 06:54:04 adam
76 * Revision 1.13 1996/10/29 13:35:38 adam
77 * Implemented data1_set_tabpath and data1_get_tabpath.
79 * Revision 1.12 1996/10/11 10:35:38 adam
80 * Fixed a bug that caused data1_read_node to core dump when no abstract
81 * syntax was defined in a "sgml"-record.
83 * Revision 1.11 1996/07/06 19:58:35 quinn
84 * System headerfiles gathered in yconfig
86 * Revision 1.10 1996/01/19 15:41:47 quinn
87 * Fixed uninitialized boolean.
89 * Revision 1.9 1996/01/17 14:52:47 adam
90 * Changed prototype for reader function parsed to data1_read_record.
92 * Revision 1.8 1995/12/15 16:20:41 quinn
93 * Added formatted text.
95 * Revision 1.7 1995/12/13 13:44:32 quinn
96 * Modified Data1-system to use nmem
98 * Revision 1.6 1995/12/12 16:37:08 quinn
99 * Added destroy element to data1_node.
101 * Revision 1.5 1995/12/11 15:22:37 quinn
102 * Added last_child field to the node.
103 * Rewrote schema-mapping.
105 * Revision 1.4 1995/11/13 09:27:36 quinn
106 * Fiddling with the variant stuff.
108 * Revision 1.3 1995/11/01 16:34:57 quinn
109 * Making data1 look for tables in data1_tabpath
111 * Revision 1.2 1995/11/01 13:54:48 quinn
114 * Revision 1.1 1995/11/01 11:56:09 quinn
115 * Added Retrieval (data management) functions en masse.
117 * Revision 1.14 1995/10/30 12:40:55 quinn
118 * Fixed a couple of bugs.
120 * Revision 1.13 1995/10/25 16:00:47 quinn
121 * USMARC support is now almost operational
123 * Revision 1.12 1995/10/16 14:02:55 quinn
124 * Changes to support element set names and espec1
126 * Revision 1.11 1995/10/13 16:05:08 quinn
127 * Adding Espec1-processing
129 * Revision 1.10 1995/10/11 14:53:44 quinn
132 * Revision 1.9 1995/10/06 16:56:50 quinn
133 * Fixed ranked result.
135 * Revision 1.8 1995/10/06 16:44:13 quinn
136 * Work on attribute set mapping, etc.
138 * Revision 1.7 1995/10/06 12:58:35 quinn
141 * Revision 1.6 1995/10/04 09:29:49 quinn
142 * Adjustments to support USGS test data
144 * Revision 1.5 1995/10/03 17:56:43 quinn
147 * Revision 1.4 1995/10/02 15:53:19 quinn
150 * Revision 1.3 1995/10/02 14:55:21 quinn
151 * *** empty log message ***
153 * Revision 1.2 1995/09/14 15:18:13 quinn
156 * Revision 1.1 1995/09/12 11:24:30 quinn
157 * Beginning to add code for structured records.
166 #include <yaz/xmalloc.h>
168 #include <yaz/data1.h>
171 * get the tag which is the immediate parent of this node (this may mean
172 * traversing intermediate things like variants and stuff.
174 data1_node *get_parent_tag (data1_handle dh, data1_node *n)
176 for (; n && n->which != DATA1N_root; n = n->parent)
177 if (n->which == DATA1N_tag)
182 data1_node *data1_mk_node (data1_handle dh, NMEM m)
186 r = (data1_node *)nmem_malloc(m, sizeof(*r));
187 r->next = r->child = r->last_child = r->parent = 0;
192 void data1_free_tree (data1_handle dh, data1_node *t)
194 data1_node *p = t->child, *pn;
199 data1_free_tree (dh, p);
206 char *data1_insert_string (data1_handle dh, data1_node *res,
207 NMEM m, const char *str)
209 int len = strlen(str);
211 if (len >= DATA1_LOCALDATA)
212 return nmem_strdup (m, str);
215 strcpy (res->lbuf, str);
220 data1_node *data1_add_insert_taggeddata(data1_handle dh, data1_node *root,
222 const char *tagname, NMEM m,
223 int first_flag, int local_allowed)
225 data1_node *partag = get_parent_tag (dh, at);
226 data1_node *tagn = data1_mk_node (dh, m);
227 data1_element *e = NULL;
230 tagn->which = DATA1N_tag;
231 tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
232 tagn->u.tag.node_selected = 0;
233 tagn->u.tag.make_variantlist = 0;
234 tagn->u.tag.no_data_requested = 0;
235 tagn->u.tag.get_bytes = -1;
238 e = partag->u.tag.element;
239 tagn->u.tag.element =
240 data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
241 if (!local_allowed && !tagn->u.tag.element)
243 tagn->last_child = tagn->child = datn = data1_mk_node (dh, m);
247 datn->which = DATA1N_data;
248 datn->u.data.formatted_text = 0;
253 tagn->next = at->child;
255 at->last_child = tagn;
264 assert (at->last_child);
265 at->last_child->next = tagn;
267 at->last_child = tagn;
272 data1_node *data1_add_taggeddata(data1_handle dh, data1_node *root,
274 const char *tagname, NMEM m)
276 return data1_add_insert_taggeddata (dh, root, at, tagname, m, 0, 1);
281 * Insert a tagged node into the record root as first child of the node at
282 * which should be root or tag itself). Returns pointer to the data node,
283 * which can then be modified.
285 data1_node *data1_insert_taggeddata(data1_handle dh, data1_node *root,
287 const char *tagname, NMEM m)
289 return data1_add_insert_taggeddata (dh, root, at, tagname, m, 1, 0);
293 * Ugh. Sometimes functions just grow and grow on you. This one reads a
294 * 'node' and its children.
296 data1_node *data1_read_nodex (data1_handle dh, NMEM m,
297 int (*get_byte)(void *fh), void *fh, WRBUF wrbuf)
299 data1_absyn *absyn = 0;
300 data1_node *d1_stack[256];
310 data1_node *parent = level ? d1_stack[level-1] : 0;
311 while (c != '\0' && d1_isspace(c))
320 if (c == '<') /* beginning of tag */
326 for (i = 0; (c=(*get_byte)(fh)) && c != '>' && !d1_isspace(c);)
327 if (i < (sizeof(tag)-1))
330 while (d1_isspace(c))
332 for (i = 0; c && c != '>'; c = (*get_byte)(fh))
333 if (i < (sizeof(args)-1))
338 yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
354 parent = d1_stack[--i];
355 if ((parent->which == DATA1N_root &&
356 !strcmp(tag+1, parent->u.root.type)) ||
357 (parent->which == DATA1N_tag &&
358 !strcmp(tag+1, parent->u.tag.tag)))
366 yaz_log (LOG_WARN, "%d: no begin tag for %s",
375 if (level == 0) /* root ? */
377 if (!(absyn = data1_get_absyn (dh, tag)))
379 yaz_log(LOG_WARN, "Unable to acquire abstract syntax " "for '%s'", tag);
380 /* It's now OK for a record not to have an absyn */
382 res = data1_mk_node (dh, m);
383 res->which = DATA1N_root;
384 res->u.root.type = data1_insert_string (dh, res, m, tag);
385 res->u.root.absyn = absyn;
388 else if (!strcmp(tag, "var"))
390 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
394 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
396 yaz_log(LOG_WARN, "Malformed variant triple at '%s'", tag);
400 data1_getvartypebyct(dh,
401 parent->root->u.root.absyn->varset,
405 * If we're the first variant in this group, create a parent
406 * variant, and insert it before the current variant.
408 if (parent->which != DATA1N_variant)
410 res = data1_mk_node (dh, m);
411 res->which = DATA1N_variant;
412 res->u.variant.type = 0;
413 res->u.variant.value = 0;
418 * now determine if one of our ancestor triples is of
419 * same type. If so, we break here.
422 for (i = level-1; d1_stack[i]->which==DATA1N_variant; --i)
423 if (d1_stack[i]->u.variant.type == tp)
428 res = data1_mk_node (dh, m);
429 res->which = DATA1N_variant;
430 res->u.variant.type = tp;
431 res->u.variant.value =
432 data1_insert_string (dh, res, m, args + val_offset);
435 else /* tag.. acquire our element in the abstract syntax */
437 data1_node *partag = get_parent_tag (dh, parent);
438 data1_element *elem, *e = 0;
441 if (parent->which == DATA1N_variant)
444 if (!(e = partag->u.tag.element))
445 localtag = 1; /* our parent is a local tag */
447 elem = data1_getelementbytagname(dh, absyn, e, tag);
448 res = data1_mk_node (dh, m);
449 res->which = DATA1N_tag;
450 res->u.tag.tag = data1_insert_string (dh, res, m, tag);
451 res->u.tag.element = elem;
452 res->u.tag.node_selected = 0;
453 res->u.tag.make_variantlist = 0;
454 res->u.tag.no_data_requested = 0;
455 res->u.tag.get_bytes = -1;
459 parent->last_child = res;
460 res->root = parent->root;
462 res->parent = parent;
464 d1_stack[level]->next = res;
467 d1_stack[level] = res;
468 d1_stack[++level] = 0;
470 else /* != '<'... this is a body of text */
474 int len, prev_char = 0;
481 res = data1_mk_node(dh, m);
482 res->parent = parent;
483 res->which = DATA1N_data;
484 res->u.data.what = DATA1I_text;
485 res->u.data.formatted_text = 0;
486 res->root = parent->root;
487 parent->last_child = res;
489 d1_stack[level]->next = res;
492 d1_stack[level] = res;
498 wrbuf_putc (wrbuf, c);
501 len = wrbuf_len(wrbuf);
503 /* use local buffer of nmem if too large */
504 if (len >= DATA1_LOCALDATA)
505 res->u.data.data = (char*) nmem_malloc (m, len);
507 res->u.data.data = res->lbuf;
509 /* read "data" and transfer while removing white space */
510 dst = res->u.data.data;
511 for (src = wrbuf_buf(wrbuf); --len >= 0; src++)
515 if (d1_isspace (*src))
527 res->u.data.len = dst - res->u.data.data;
533 int getc_mem (void *fh)
535 const char **p = (const char **) fh;
541 data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m)
543 WRBUF wrbuf = wrbuf_alloc();
546 node = data1_read_nodex(dh, m, getc_mem, buf, wrbuf);
547 wrbuf_free (wrbuf, 1);
552 * Read a record in the native syntax.
554 data1_node *data1_read_record(data1_handle dh,
555 int (*rf)(void *, char *, size_t), void *fh,
559 char **buf = data1_get_read_buf (dh, &size);
564 *buf = (char *)xmalloc(*size = 4096);
568 if (rd + 2048 >= *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
570 if ((res = (*rf)(fh, *buf + rd, 2048)) <= 0)
576 return data1_read_node(dh, &bp, m);
585 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
587 const char *bp = buf;
588 return data1_read_node (dh, &bp, m);