2 * Copyright (c) 1995-2001, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.38 2001-03-27 23:06:21 adam
8 * Quotes and slashes may occur within attributes.
10 * Revision 1.37 2001/02/28 09:00:06 adam
11 * Fixed problem with stack overflow for very nested records.
13 * Revision 1.36 2001/02/21 13:46:53 adam
16 * Revision 1.35 2000/12/05 14:44:25 adam
17 * Readers skips <! ...> sections.
19 * Revision 1.34 2000/12/05 10:06:23 adam
20 * Added support for null-data rules like <tag/>.
22 * Revision 1.33 2000/11/29 14:22:47 adam
23 * Implemented XML/SGML attributes for data1 so that d1_read reads them
24 * and d1_write generates proper attributes for XML/SGML records. Added
25 * register locking for threaded version.
27 * Revision 1.32 2000/01/06 11:25:59 adam
28 * Added case to prevent warning.
30 * Revision 1.31 1999/12/21 14:16:20 ian
31 * Changed retrieval module to allow data1 trees with no associated absyn.
32 * Also added a simple interface for extracting values from data1 trees using
33 * a string based tagpath.
35 * Revision 1.30 1999/11/30 13:47:12 adam
36 * Improved installation. Moved header files to include/yaz.
38 * Revision 1.29 1999/10/21 12:06:29 adam
39 * Retrieval module no longer uses ctype.h - functions.
41 * Revision 1.28 1999/10/21 09:50:33 adam
42 * SGML reader uses own isspace - it doesn't do 8-bit on WIN32!
44 * Revision 1.27 1999/08/27 09:40:32 adam
45 * Renamed logf function to yaz_log. Removed VC++ project files.
47 * Revision 1.26 1999/07/13 13:23:48 adam
48 * Non-recursive version of data1_read_node. data1_read_nodex reads
49 * stream of bytes (instead of buffer in memory).
51 * Revision 1.25 1999/04/20 09:56:48 adam
52 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
53 * Modified all encoders/decoders to reflect this change.
55 * Revision 1.24 1998/10/28 15:10:09 adam
56 * Added --with-yc option to configure. For the data1_node in data1.h:
57 * decreased size of localdata and removed member "line" which wasn't useful.
59 * Revision 1.23 1998/03/12 11:28:45 adam
60 * Fix: didn't set root member of tagged node in function.
61 * data1_add_insert_taggeddata.
63 * Revision 1.22 1998/03/05 08:15:32 adam
64 * Implemented data1_add_insert_taggeddata utility which is more flexible
65 * than data1_insert_taggeddata.
67 * Revision 1.21 1998/02/27 14:08:05 adam
68 * Added const to some char pointer arguments.
69 * Reworked data1_read_node so that it doesn't create a tree with
70 * pointers to original "SGML"-buffer.
72 * Revision 1.20 1998/02/11 11:53:35 adam
73 * Changed code so that it compiles as C++.
75 * Revision 1.19 1997/12/09 16:17:09 adam
76 * Fix bug regarding variants. Tags with prefix "var" was incorrectly
77 * interpreted as "start of variants". Now, only "var" indicates such
79 * Cleaned up data1_read_node so tag names and variant names are
80 * copied and not pointed to by the generated data1 tree. Data nodes
81 * still point to old buffer.
83 * Revision 1.18 1997/11/18 09:51:09 adam
84 * Removed element num_children from data1_node. Minor changes in
87 * Revision 1.17 1997/11/05 09:20:51 adam
90 * Revision 1.16 1997/09/17 12:10:37 adam
93 * Revision 1.15 1997/09/05 09:50:57 adam
94 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
96 * Revision 1.14 1997/05/14 06:54:04 adam
99 * Revision 1.13 1996/10/29 13:35:38 adam
100 * Implemented data1_set_tabpath and data1_get_tabpath.
102 * Revision 1.12 1996/10/11 10:35:38 adam
103 * Fixed a bug that caused data1_read_node to core dump when no abstract
104 * syntax was defined in a "sgml"-record.
106 * Revision 1.11 1996/07/06 19:58:35 quinn
107 * System headerfiles gathered in yconfig
109 * Revision 1.10 1996/01/19 15:41:47 quinn
110 * Fixed uninitialized boolean.
112 * Revision 1.9 1996/01/17 14:52:47 adam
113 * Changed prototype for reader function parsed to data1_read_record.
115 * Revision 1.8 1995/12/15 16:20:41 quinn
116 * Added formatted text.
118 * Revision 1.7 1995/12/13 13:44:32 quinn
119 * Modified Data1-system to use nmem
121 * Revision 1.6 1995/12/12 16:37:08 quinn
122 * Added destroy element to data1_node.
124 * Revision 1.5 1995/12/11 15:22:37 quinn
125 * Added last_child field to the node.
126 * Rewrote schema-mapping.
128 * Revision 1.4 1995/11/13 09:27:36 quinn
129 * Fiddling with the variant stuff.
131 * Revision 1.3 1995/11/01 16:34:57 quinn
132 * Making data1 look for tables in data1_tabpath
134 * Revision 1.2 1995/11/01 13:54:48 quinn
137 * Revision 1.1 1995/11/01 11:56:09 quinn
138 * Added Retrieval (data management) functions en masse.
140 * Revision 1.14 1995/10/30 12:40:55 quinn
141 * Fixed a couple of bugs.
143 * Revision 1.13 1995/10/25 16:00:47 quinn
144 * USMARC support is now almost operational
146 * Revision 1.12 1995/10/16 14:02:55 quinn
147 * Changes to support element set names and espec1
149 * Revision 1.11 1995/10/13 16:05:08 quinn
150 * Adding Espec1-processing
152 * Revision 1.10 1995/10/11 14:53:44 quinn
155 * Revision 1.9 1995/10/06 16:56:50 quinn
156 * Fixed ranked result.
158 * Revision 1.8 1995/10/06 16:44:13 quinn
159 * Work on attribute set mapping, etc.
161 * Revision 1.7 1995/10/06 12:58:35 quinn
164 * Revision 1.6 1995/10/04 09:29:49 quinn
165 * Adjustments to support USGS test data
167 * Revision 1.5 1995/10/03 17:56:43 quinn
170 * Revision 1.4 1995/10/02 15:53:19 quinn
173 * Revision 1.3 1995/10/02 14:55:21 quinn
174 * *** empty log message ***
176 * Revision 1.2 1995/09/14 15:18:13 quinn
179 * Revision 1.1 1995/09/12 11:24:30 quinn
180 * Beginning to add code for structured records.
189 #include <yaz/xmalloc.h>
191 #include <yaz/data1.h>
194 * get the tag which is the immediate parent of this node (this may mean
195 * traversing intermediate things like variants and stuff.
197 data1_node *get_parent_tag (data1_handle dh, data1_node *n)
199 for (; n && n->which != DATA1N_root; n = n->parent)
200 if (n->which == DATA1N_tag)
205 data1_node *data1_mk_node (data1_handle dh, NMEM m)
209 r = (data1_node *)nmem_malloc(m, sizeof(*r));
210 r->next = r->child = r->last_child = r->parent = 0;
215 data1_node *data1_mk_node_type (data1_handle dh, NMEM m, int type)
219 r = data1_mk_node(dh, m);
225 r->u.tag.element = 0;
226 r->u.tag.no_data_requested = 0;
227 r->u.tag.node_selected = 0;
228 r->u.tag.make_variantlist = 0;
229 r->u.tag.get_bytes = -1;
230 #if DATA1_USING_XATTR
231 r->u.tag.attributes = 0;
242 r->u.data.formatted_text = 0;
245 logf (LOG_WARN, "data_mk_node_type. bad type = %d\n", type);
250 void data1_free_tree (data1_handle dh, data1_node *t)
252 data1_node *p = t->child, *pn;
257 data1_free_tree (dh, p);
264 char *data1_insert_string (data1_handle dh, data1_node *res,
265 NMEM m, const char *str)
267 int len = strlen(str);
269 if (len >= DATA1_LOCALDATA)
270 return nmem_strdup (m, str);
273 strcpy (res->lbuf, str);
278 data1_node *data1_add_insert_taggeddata(data1_handle dh, data1_node *root,
280 const char *tagname, NMEM m,
281 int first_flag, int local_allowed)
283 data1_node *partag = get_parent_tag (dh, at);
284 data1_node *tagn = data1_mk_node_type (dh, m, DATA1N_tag);
285 data1_element *e = NULL;
288 tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
291 e = partag->u.tag.element;
292 tagn->u.tag.element =
293 data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
294 if (!local_allowed && !tagn->u.tag.element)
296 tagn->last_child = tagn->child = datn = data1_mk_node_type (dh, m, DATA1N_data);
300 datn->u.data.formatted_text = 0;
305 tagn->next = at->child;
307 at->last_child = tagn;
316 assert (at->last_child);
317 at->last_child->next = tagn;
319 at->last_child = tagn;
324 data1_node *data1_add_taggeddata(data1_handle dh, data1_node *root,
326 const char *tagname, NMEM m)
328 return data1_add_insert_taggeddata (dh, root, at, tagname, m, 0, 1);
333 * Insert a tagged node into the record root as first child of the node at
334 * which should be root or tag itself). Returns pointer to the data node,
335 * which can then be modified.
337 data1_node *data1_insert_taggeddata(data1_handle dh, data1_node *root,
339 const char *tagname, NMEM m)
341 return data1_add_insert_taggeddata (dh, root, at, tagname, m, 1, 0);
344 #if DATA1_USING_XATTR
345 data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
346 int (*get_byte)(void *fh), void *fh,
347 WRBUF wrbuf, int *ch)
349 data1_xattr *p_first = 0;
350 data1_xattr **pp = &p_first;
356 while (c && d1_isspace(c))
358 if (!c || c == '>' || c == '/')
360 *pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
366 while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
368 wrbuf_putc (wrbuf, c);
371 wrbuf_putc (wrbuf, '\0');
372 len = wrbuf_len(wrbuf);
373 p->name = (char*) nmem_malloc (m, len);
374 strcpy (p->name, wrbuf_buf(wrbuf));
382 while (c && c != '"')
384 wrbuf_putc (wrbuf, c);
393 while (c && c != '>' && c != '/')
395 wrbuf_putc (wrbuf, c);
399 wrbuf_putc (wrbuf, '\0');
400 len = wrbuf_len(wrbuf);
401 p->value = (char*) nmem_malloc (m, len);
402 strcpy (p->value, wrbuf_buf(wrbuf));
411 * Ugh. Sometimes functions just grow and grow on you. This one reads a
412 * 'node' and its children.
414 data1_node *data1_read_nodex (data1_handle dh, NMEM m,
415 int (*get_byte)(void *fh), void *fh, WRBUF wrbuf)
417 data1_absyn *absyn = 0;
418 data1_node *d1_stack[256];
428 data1_node *parent = level ? d1_stack[level-1] : 0;
429 while (c != '\0' && d1_isspace(c))
438 if (c == '<') /* beginning of tag */
440 #if DATA1_USING_XATTR
455 else if (c == '!') /* tags/comments that we don't deal with yet */
457 while (c && c != '>')
463 while (c && c != '>' && c != '/' && !d1_isspace(c))
465 if (i < (sizeof(tag)-1))
470 #if DATA1_USING_XATTR
471 xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c);
474 while (d1_isspace(c))
476 for (i = 0; c && c != '>' && c != '/'; c = (*get_byte)(fh))
477 if (i < (sizeof(args)-1))
482 { /* <tag attrs/> or <tag/> */
488 yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
504 parent = d1_stack[--i];
505 if ((parent->which == DATA1N_root &&
506 !strcmp(tag, parent->u.root.type)) ||
507 (parent->which == DATA1N_tag &&
508 !strcmp(tag, parent->u.tag.tag)))
516 yaz_log (LOG_WARN, "%d: no begin tag for %s",
525 if (level == 0) /* root ? */
527 if (!(absyn = data1_get_absyn (dh, tag)))
529 yaz_log(LOG_WARN, "Unable to acquire abstract syntax " "for '%s'", tag);
530 /* It's now OK for a record not to have an absyn */
532 res = data1_mk_node_type (dh, m, DATA1N_root);
533 res->u.root.type = data1_insert_string (dh, res, m, tag);
534 res->u.root.absyn = absyn;
537 else if (!strcmp(tag, "var"))
539 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
543 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
545 yaz_log(LOG_WARN, "Malformed variant triple at '%s'", tag);
549 data1_getvartypebyct(dh,
550 parent->root->u.root.absyn->varset,
554 * If we're the first variant in this group, create a parent
555 * variant, and insert it before the current variant.
557 if (parent->which != DATA1N_variant)
559 res = data1_mk_node (dh, m);
560 res->which = DATA1N_variant;
561 res->u.variant.type = 0;
562 res->u.variant.value = 0;
567 * now determine if one of our ancestor triples is of
568 * same type. If so, we break here.
571 for (i = level-1; d1_stack[i]->which==DATA1N_variant; --i)
572 if (d1_stack[i]->u.variant.type == tp)
577 res = data1_mk_node (dh, m);
578 res->which = DATA1N_variant;
579 res->u.variant.type = tp;
580 res->u.variant.value =
581 data1_insert_string (dh, res, m, args + val_offset);
584 else /* tag.. acquire our element in the abstract syntax */
586 data1_node *partag = get_parent_tag (dh, parent);
587 data1_element *elem, *e = 0;
590 if (parent->which == DATA1N_variant)
593 if (!(e = partag->u.tag.element))
594 localtag = 1; /* our parent is a local tag */
596 elem = data1_getelementbytagname(dh, absyn, e, tag);
597 res = data1_mk_node_type (dh, m, DATA1N_tag);
598 res->u.tag.tag = data1_insert_string (dh, res, m, tag);
599 res->u.tag.element = elem;
600 #if DATA1_USING_XATTR
601 res->u.tag.attributes = xattr;
606 parent->last_child = res;
607 res->root = parent->root;
609 res->parent = parent;
611 d1_stack[level]->next = res;
614 d1_stack[level] = res;
615 d1_stack[level+1] = 0;
616 if (level < 250 && !null_tag)
619 else /* != '<'... this is a body of text */
623 int len, prev_char = 0;
630 res = data1_mk_node_type (dh, m, DATA1N_data);
631 res->parent = parent;
632 res->u.data.what = DATA1I_text;
633 res->u.data.formatted_text = 0;
634 res->root = parent->root;
635 parent->last_child = res;
637 d1_stack[level]->next = res;
640 d1_stack[level] = res;
644 while (c && c != '<')
646 wrbuf_putc (wrbuf, c);
649 len = wrbuf_len(wrbuf);
651 /* use local buffer of nmem if too large */
652 if (len >= DATA1_LOCALDATA)
653 res->u.data.data = (char*) nmem_malloc (m, len);
655 res->u.data.data = res->lbuf;
657 /* read "data" and transfer while removing white space */
658 dst = res->u.data.data;
659 for (src = wrbuf_buf(wrbuf); --len >= 0; src++)
663 if (d1_isspace (*src))
675 res->u.data.len = dst - res->u.data.data;
681 int getc_mem (void *fh)
683 const char **p = (const char **) fh;
689 data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m)
691 WRBUF wrbuf = wrbuf_alloc();
694 node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf);
695 wrbuf_free (wrbuf, 1);
700 * Read a record in the native syntax.
702 data1_node *data1_read_record(data1_handle dh,
703 int (*rf)(void *, char *, size_t), void *fh,
707 char **buf = data1_get_read_buf (dh, &size);
712 *buf = (char *)xmalloc(*size = 4096);
716 if (rd + 2048 >= *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
718 if ((res = (*rf)(fh, *buf + rd, 2048)) <= 0)
724 return data1_read_node(dh, &bp, m);
733 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
735 const char *bp = buf;
736 return data1_read_node (dh, &bp, m);