2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 2000-01-06 11:25:59 adam
8 * Added case to prevent warning.
10 * Revision 1.31 1999/12/21 14:16:20 ian
11 * Changed retrieval module to allow data1 trees with no associated absyn.
12 * Also added a simple interface for extracting values from data1 trees using
13 * a string based tagpath.
15 * Revision 1.30 1999/11/30 13:47:12 adam
16 * Improved installation. Moved header files to include/yaz.
18 * Revision 1.29 1999/10/21 12:06:29 adam
19 * Retrieval module no longer uses ctype.h - functions.
21 * Revision 1.28 1999/10/21 09:50:33 adam
22 * SGML reader uses own isspace - it doesn't do 8-bit on WIN32!
24 * Revision 1.27 1999/08/27 09:40:32 adam
25 * Renamed logf function to yaz_log. Removed VC++ project files.
27 * Revision 1.26 1999/07/13 13:23:48 adam
28 * Non-recursive version of data1_read_node. data1_read_nodex reads
29 * stream of bytes (instead of buffer in memory).
31 * Revision 1.25 1999/04/20 09:56:48 adam
32 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
33 * Modified all encoders/decoders to reflect this change.
35 * Revision 1.24 1998/10/28 15:10:09 adam
36 * Added --with-yc option to configure. For the data1_node in data1.h:
37 * decreased size of localdata and removed member "line" which wasn't useful.
39 * Revision 1.23 1998/03/12 11:28:45 adam
40 * Fix: didn't set root member of tagged node in function.
41 * data1_add_insert_taggeddata.
43 * Revision 1.22 1998/03/05 08:15:32 adam
44 * Implemented data1_add_insert_taggeddata utility which is more flexible
45 * than data1_insert_taggeddata.
47 * Revision 1.21 1998/02/27 14:08:05 adam
48 * Added const to some char pointer arguments.
49 * Reworked data1_read_node so that it doesn't create a tree with
50 * pointers to original "SGML"-buffer.
52 * Revision 1.20 1998/02/11 11:53:35 adam
53 * Changed code so that it compiles as C++.
55 * Revision 1.19 1997/12/09 16:17:09 adam
56 * Fix bug regarding variants. Tags with prefix "var" was incorrectly
57 * interpreted as "start of variants". Now, only "var" indicates such
59 * Cleaned up data1_read_node so tag names and variant names are
60 * copied and not pointed to by the generated data1 tree. Data nodes
61 * still point to old buffer.
63 * Revision 1.18 1997/11/18 09:51:09 adam
64 * Removed element num_children from data1_node. Minor changes in
67 * Revision 1.17 1997/11/05 09:20:51 adam
70 * Revision 1.16 1997/09/17 12:10:37 adam
73 * Revision 1.15 1997/09/05 09:50:57 adam
74 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
76 * Revision 1.14 1997/05/14 06:54:04 adam
79 * Revision 1.13 1996/10/29 13:35:38 adam
80 * Implemented data1_set_tabpath and data1_get_tabpath.
82 * Revision 1.12 1996/10/11 10:35:38 adam
83 * Fixed a bug that caused data1_read_node to core dump when no abstract
84 * syntax was defined in a "sgml"-record.
86 * Revision 1.11 1996/07/06 19:58:35 quinn
87 * System headerfiles gathered in yconfig
89 * Revision 1.10 1996/01/19 15:41:47 quinn
90 * Fixed uninitialized boolean.
92 * Revision 1.9 1996/01/17 14:52:47 adam
93 * Changed prototype for reader function parsed to data1_read_record.
95 * Revision 1.8 1995/12/15 16:20:41 quinn
96 * Added formatted text.
98 * Revision 1.7 1995/12/13 13:44:32 quinn
99 * Modified Data1-system to use nmem
101 * Revision 1.6 1995/12/12 16:37:08 quinn
102 * Added destroy element to data1_node.
104 * Revision 1.5 1995/12/11 15:22:37 quinn
105 * Added last_child field to the node.
106 * Rewrote schema-mapping.
108 * Revision 1.4 1995/11/13 09:27:36 quinn
109 * Fiddling with the variant stuff.
111 * Revision 1.3 1995/11/01 16:34:57 quinn
112 * Making data1 look for tables in data1_tabpath
114 * Revision 1.2 1995/11/01 13:54:48 quinn
117 * Revision 1.1 1995/11/01 11:56:09 quinn
118 * Added Retrieval (data management) functions en masse.
120 * Revision 1.14 1995/10/30 12:40:55 quinn
121 * Fixed a couple of bugs.
123 * Revision 1.13 1995/10/25 16:00:47 quinn
124 * USMARC support is now almost operational
126 * Revision 1.12 1995/10/16 14:02:55 quinn
127 * Changes to support element set names and espec1
129 * Revision 1.11 1995/10/13 16:05:08 quinn
130 * Adding Espec1-processing
132 * Revision 1.10 1995/10/11 14:53:44 quinn
135 * Revision 1.9 1995/10/06 16:56:50 quinn
136 * Fixed ranked result.
138 * Revision 1.8 1995/10/06 16:44:13 quinn
139 * Work on attribute set mapping, etc.
141 * Revision 1.7 1995/10/06 12:58:35 quinn
144 * Revision 1.6 1995/10/04 09:29:49 quinn
145 * Adjustments to support USGS test data
147 * Revision 1.5 1995/10/03 17:56:43 quinn
150 * Revision 1.4 1995/10/02 15:53:19 quinn
153 * Revision 1.3 1995/10/02 14:55:21 quinn
154 * *** empty log message ***
156 * Revision 1.2 1995/09/14 15:18:13 quinn
159 * Revision 1.1 1995/09/12 11:24:30 quinn
160 * Beginning to add code for structured records.
169 #include <yaz/xmalloc.h>
171 #include <yaz/data1.h>
174 * get the tag which is the immediate parent of this node (this may mean
175 * traversing intermediate things like variants and stuff.
177 data1_node *get_parent_tag (data1_handle dh, data1_node *n)
179 for (; n && n->which != DATA1N_root; n = n->parent)
180 if (n->which == DATA1N_tag)
185 data1_node *data1_mk_node (data1_handle dh, NMEM m)
189 r = (data1_node *)nmem_malloc(m, sizeof(*r));
190 r->next = r->child = r->last_child = r->parent = 0;
195 void data1_free_tree (data1_handle dh, data1_node *t)
197 data1_node *p = t->child, *pn;
202 data1_free_tree (dh, p);
209 char *data1_insert_string (data1_handle dh, data1_node *res,
210 NMEM m, const char *str)
212 int len = strlen(str);
214 if (len >= DATA1_LOCALDATA)
215 return nmem_strdup (m, str);
218 strcpy (res->lbuf, str);
223 data1_node *data1_add_insert_taggeddata(data1_handle dh, data1_node *root,
225 const char *tagname, NMEM m,
226 int first_flag, int local_allowed)
228 data1_node *partag = get_parent_tag (dh, at);
229 data1_node *tagn = data1_mk_node (dh, m);
230 data1_element *e = NULL;
233 tagn->which = DATA1N_tag;
234 tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
235 tagn->u.tag.node_selected = 0;
236 tagn->u.tag.make_variantlist = 0;
237 tagn->u.tag.no_data_requested = 0;
238 tagn->u.tag.get_bytes = -1;
241 e = partag->u.tag.element;
242 tagn->u.tag.element =
243 data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
244 if (!local_allowed && !tagn->u.tag.element)
246 tagn->last_child = tagn->child = datn = data1_mk_node (dh, m);
250 datn->which = DATA1N_data;
251 datn->u.data.formatted_text = 0;
256 tagn->next = at->child;
258 at->last_child = tagn;
267 assert (at->last_child);
268 at->last_child->next = tagn;
270 at->last_child = tagn;
275 data1_node *data1_add_taggeddata(data1_handle dh, data1_node *root,
277 const char *tagname, NMEM m)
279 return data1_add_insert_taggeddata (dh, root, at, tagname, m, 0, 1);
284 * Insert a tagged node into the record root as first child of the node at
285 * which should be root or tag itself). Returns pointer to the data node,
286 * which can then be modified.
288 data1_node *data1_insert_taggeddata(data1_handle dh, data1_node *root,
290 const char *tagname, NMEM m)
292 return data1_add_insert_taggeddata (dh, root, at, tagname, m, 1, 0);
296 * Ugh. Sometimes functions just grow and grow on you. This one reads a
297 * 'node' and its children.
299 data1_node *data1_read_nodex (data1_handle dh, NMEM m,
300 int (*get_byte)(void *fh), void *fh, WRBUF wrbuf)
302 data1_absyn *absyn = 0;
303 data1_node *d1_stack[256];
313 data1_node *parent = level ? d1_stack[level-1] : 0;
314 while (c != '\0' && d1_isspace(c))
323 if (c == '<') /* beginning of tag */
329 for (i = 0; (c=(*get_byte)(fh)) && c != '>' && !d1_isspace(c);)
330 if (i < (sizeof(tag)-1))
333 while (d1_isspace(c))
335 for (i = 0; c && c != '>'; c = (*get_byte)(fh))
336 if (i < (sizeof(args)-1))
341 yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
357 parent = d1_stack[--i];
358 if ((parent->which == DATA1N_root &&
359 !strcmp(tag+1, parent->u.root.type)) ||
360 (parent->which == DATA1N_tag &&
361 !strcmp(tag+1, parent->u.tag.tag)))
369 yaz_log (LOG_WARN, "%d: no begin tag for %s",
378 if (level == 0) /* root ? */
380 if (!(absyn = data1_get_absyn (dh, tag)))
382 yaz_log(LOG_WARN, "Unable to acquire abstract syntax " "for '%s'", tag);
383 /* It's now OK for a record not to have an absyn */
385 res = data1_mk_node (dh, m);
386 res->which = DATA1N_root;
387 res->u.root.type = data1_insert_string (dh, res, m, tag);
388 res->u.root.absyn = absyn;
391 else if (!strcmp(tag, "var"))
393 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
397 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
399 yaz_log(LOG_WARN, "Malformed variant triple at '%s'", tag);
403 data1_getvartypebyct(dh,
404 parent->root->u.root.absyn->varset,
408 * If we're the first variant in this group, create a parent
409 * variant, and insert it before the current variant.
411 if (parent->which != DATA1N_variant)
413 res = data1_mk_node (dh, m);
414 res->which = DATA1N_variant;
415 res->u.variant.type = 0;
416 res->u.variant.value = 0;
421 * now determine if one of our ancestor triples is of
422 * same type. If so, we break here.
425 for (i = level-1; d1_stack[i]->which==DATA1N_variant; --i)
426 if (d1_stack[i]->u.variant.type == tp)
431 res = data1_mk_node (dh, m);
432 res->which = DATA1N_variant;
433 res->u.variant.type = tp;
434 res->u.variant.value =
435 data1_insert_string (dh, res, m, args + val_offset);
438 else /* tag.. acquire our element in the abstract syntax */
440 data1_node *partag = get_parent_tag (dh, parent);
441 data1_element *elem, *e = 0;
444 if (parent->which == DATA1N_variant)
447 if (!(e = partag->u.tag.element))
448 localtag = 1; /* our parent is a local tag */
450 elem = data1_getelementbytagname(dh, absyn, e, tag);
451 res = data1_mk_node (dh, m);
452 res->which = DATA1N_tag;
453 res->u.tag.tag = data1_insert_string (dh, res, m, tag);
454 res->u.tag.element = elem;
455 res->u.tag.node_selected = 0;
456 res->u.tag.make_variantlist = 0;
457 res->u.tag.no_data_requested = 0;
458 res->u.tag.get_bytes = -1;
462 parent->last_child = res;
463 res->root = parent->root;
465 res->parent = parent;
467 d1_stack[level]->next = res;
470 d1_stack[level] = res;
471 d1_stack[++level] = 0;
473 else /* != '<'... this is a body of text */
477 int len, prev_char = 0;
484 res = data1_mk_node(dh, m);
485 res->parent = parent;
486 res->which = DATA1N_data;
487 res->u.data.what = DATA1I_text;
488 res->u.data.formatted_text = 0;
489 res->root = parent->root;
490 parent->last_child = res;
492 d1_stack[level]->next = res;
495 d1_stack[level] = res;
501 wrbuf_putc (wrbuf, c);
504 len = wrbuf_len(wrbuf);
506 /* use local buffer of nmem if too large */
507 if (len >= DATA1_LOCALDATA)
508 res->u.data.data = (char*) nmem_malloc (m, len);
510 res->u.data.data = res->lbuf;
512 /* read "data" and transfer while removing white space */
513 dst = res->u.data.data;
514 for (src = wrbuf_buf(wrbuf); --len >= 0; src++)
518 if (d1_isspace (*src))
530 res->u.data.len = dst - res->u.data.data;
536 int getc_mem (void *fh)
538 const char **p = (const char **) fh;
544 data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m)
546 WRBUF wrbuf = wrbuf_alloc();
549 node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf);
550 wrbuf_free (wrbuf, 1);
555 * Read a record in the native syntax.
557 data1_node *data1_read_record(data1_handle dh,
558 int (*rf)(void *, char *, size_t), void *fh,
562 char **buf = data1_get_read_buf (dh, &size);
567 *buf = (char *)xmalloc(*size = 4096);
571 if (rd + 2048 >= *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
573 if ((res = (*rf)(fh, *buf + rd, 2048)) <= 0)
579 return data1_read_node(dh, &bp, m);
588 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
590 const char *bp = buf;
591 return data1_read_node (dh, &bp, m);