Implemented data1_add_insert_taggeddata utility which is more flexible
[yaz-moved-to-github.git] / retrieval / d1_read.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_read.c,v $
7  * Revision 1.22  1998-03-05 08:15:32  adam
8  * Implemented data1_add_insert_taggeddata utility which is more flexible
9  * than data1_insert_taggeddata.
10  *
11  * Revision 1.21  1998/02/27 14:08:05  adam
12  * Added const to some char pointer arguments.
13  * Reworked data1_read_node so that it doesn't create a tree with
14  * pointers to original "SGML"-buffer.
15  *
16  * Revision 1.20  1998/02/11 11:53:35  adam
17  * Changed code so that it compiles as C++.
18  *
19  * Revision 1.19  1997/12/09 16:17:09  adam
20  * Fix bug regarding variants. Tags with prefix "var" was incorrectly
21  * interpreted as "start of variants". Now, only "var" indicates such
22  * start.
23  * Cleaned up data1_read_node so tag names and variant names are
24  * copied and not pointed to by the generated data1 tree. Data nodes
25  * still point to old buffer.
26  *
27  * Revision 1.18  1997/11/18 09:51:09  adam
28  * Removed element num_children from data1_node. Minor changes in
29  * data1 to Explain.
30  *
31  * Revision 1.17  1997/11/05 09:20:51  adam
32  * Minor change.
33  *
34  * Revision 1.16  1997/09/17 12:10:37  adam
35  * YAZ version 1.4.
36  *
37  * Revision 1.15  1997/09/05 09:50:57  adam
38  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
39  *
40  * Revision 1.14  1997/05/14 06:54:04  adam
41  * C++ support.
42  *
43  * Revision 1.13  1996/10/29 13:35:38  adam
44  * Implemented data1_set_tabpath and data1_get_tabpath.
45  *
46  * Revision 1.12  1996/10/11 10:35:38  adam
47  * Fixed a bug that caused data1_read_node to core dump when no abstract
48  * syntax was defined in a "sgml"-record.
49  *
50  * Revision 1.11  1996/07/06 19:58:35  quinn
51  * System headerfiles gathered in yconfig
52  *
53  * Revision 1.10  1996/01/19  15:41:47  quinn
54  * Fixed uninitialized boolean.
55  *
56  * Revision 1.9  1996/01/17  14:52:47  adam
57  * Changed prototype for reader function parsed to data1_read_record.
58  *
59  * Revision 1.8  1995/12/15  16:20:41  quinn
60  * Added formatted text.
61  *
62  * Revision 1.7  1995/12/13  13:44:32  quinn
63  * Modified Data1-system to use nmem
64  *
65  * Revision 1.6  1995/12/12  16:37:08  quinn
66  * Added destroy element to data1_node.
67  *
68  * Revision 1.5  1995/12/11  15:22:37  quinn
69  * Added last_child field to the node.
70  * Rewrote schema-mapping.
71  *
72  * Revision 1.4  1995/11/13  09:27:36  quinn
73  * Fiddling with the variant stuff.
74  *
75  * Revision 1.3  1995/11/01  16:34:57  quinn
76  * Making data1 look for tables in data1_tabpath
77  *
78  * Revision 1.2  1995/11/01  13:54:48  quinn
79  * Minor adjustments
80  *
81  * Revision 1.1  1995/11/01  11:56:09  quinn
82  * Added Retrieval (data management) functions en masse.
83  *
84  * Revision 1.14  1995/10/30  12:40:55  quinn
85  * Fixed a couple of bugs.
86  *
87  * Revision 1.13  1995/10/25  16:00:47  quinn
88  * USMARC support is now almost operational
89  *
90  * Revision 1.12  1995/10/16  14:02:55  quinn
91  * Changes to support element set names and espec1
92  *
93  * Revision 1.11  1995/10/13  16:05:08  quinn
94  * Adding Espec1-processing
95  *
96  * Revision 1.10  1995/10/11  14:53:44  quinn
97  * Work on variants.
98  *
99  * Revision 1.9  1995/10/06  16:56:50  quinn
100  * Fixed ranked result.
101  *
102  * Revision 1.8  1995/10/06  16:44:13  quinn
103  * Work on attribute set mapping, etc.
104  *
105  * Revision 1.7  1995/10/06  12:58:35  quinn
106  * SUTRS support
107  *
108  * Revision 1.6  1995/10/04  09:29:49  quinn
109  * Adjustments to support USGS test data
110  *
111  * Revision 1.5  1995/10/03  17:56:43  quinn
112  * Fixing GRS code.
113  *
114  * Revision 1.4  1995/10/02  15:53:19  quinn
115  * Work
116  *
117  * Revision 1.3  1995/10/02  14:55:21  quinn
118  * *** empty log message ***
119  *
120  * Revision 1.2  1995/09/14  15:18:13  quinn
121  * Work
122  *
123  * Revision 1.1  1995/09/12  11:24:30  quinn
124  * Beginning to add code for structured records.
125  *
126  *
127  */
128
129 #include <assert.h>
130 #include <ctype.h>
131 #include <stdio.h>
132 #include <stdlib.h>
133
134 #include <xmalloc.h>
135 #include <log.h>
136 #include <data1.h>
137
138 /*
139  * get the tag which is the immediate parent of this node (this may mean
140  * traversing intermediate things like variants and stuff.
141  */
142 data1_node *get_parent_tag (data1_handle dh, data1_node *n)
143 {
144     for (; n && n->which != DATA1N_root; n = n->parent)
145         if (n->which == DATA1N_tag)
146             return n;
147     return 0;
148 }
149
150 data1_node *data1_mk_node (data1_handle dh, NMEM m)
151 {
152     data1_node *r;
153
154     r = (data1_node *)nmem_malloc(m, sizeof(*r));
155     r->next = r->child = r->last_child = r->parent = 0;
156     r->destroy = 0;
157     return r;
158 }
159
160 void data1_free_tree (data1_handle dh, data1_node *t)
161 {
162     data1_node *p = t->child, *pn;
163
164     while (p)
165     {
166         pn = p->next;
167         data1_free_tree (dh, p);
168         p = pn;
169     }
170     if (t->destroy)
171         (*t->destroy)(t);
172 }
173
174 char *data1_insert_string (data1_handle dh, data1_node *res,
175                            NMEM m, const char *str)
176 {
177     int len = strlen(str);
178
179     if (len >= DATA1_LOCALDATA)
180         return nmem_strdup (m, str);
181     else
182     {
183         strcpy (res->lbuf, str);
184         return res->lbuf;
185     }
186 }
187
188
189 data1_node *data1_add_insert_taggeddata(data1_handle dh, data1_node *root,
190                                         data1_node *at,
191                                         const char *tagname, NMEM m,
192                                         int first_flag, int local_allowed)
193 {
194     data1_node *partag = get_parent_tag (dh, at);
195     data1_node *tagn = data1_mk_node (dh, m);
196     data1_element *e = NULL;
197     data1_node *datn;
198
199     tagn->which = DATA1N_tag;
200     tagn->line = -1;
201     tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
202     tagn->u.tag.node_selected = 0;
203     tagn->u.tag.make_variantlist = 0;
204     tagn->u.tag.no_data_requested = 0;
205     tagn->u.tag.get_bytes = -1;
206
207     if (partag)
208         e = partag->u.tag.element;
209     tagn->u.tag.element =
210         data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
211     if (!local_allowed && !tagn->u.tag.element)
212         return NULL;
213     tagn->last_child = tagn->child = datn = data1_mk_node (dh, m);
214     datn->parent = tagn;
215     datn->root = root;
216     datn->which = DATA1N_data;
217     datn->u.data.formatted_text = 0;
218     tagn->parent = at;
219
220     if (first_flag)
221     {
222         tagn->next = at->child;
223         if (!tagn->next)
224             at->last_child = tagn;
225         at->child = tagn;
226     }
227     else
228     {
229         if (!at->child)
230             at->child = tagn;
231         else
232         {
233             assert (at->last_child);
234             at->last_child->next = tagn;
235         }
236         at->last_child = tagn;
237     }
238     return datn;
239 }
240
241 data1_node *data1_add_taggeddata(data1_handle dh, data1_node *root,
242                                  data1_node *at,
243                                  const char *tagname, NMEM m)
244 {
245     return data1_add_insert_taggeddata (dh, root, at, tagname, m, 0, 1);
246 }
247
248
249 /*
250  * Insert a tagged node into the record root as first child of the node at
251  * which should be root or tag itself). Returns pointer to the data node,
252  * which can then be modified.
253  */
254 data1_node *data1_insert_taggeddata(data1_handle dh, data1_node *root,
255                                     data1_node *at,
256                                     const char *tagname, NMEM m)
257 {
258     return data1_add_insert_taggeddata (dh, root, at, tagname, m, 1, 0);
259 }
260
261 /*
262  * Ugh. Sometimes functions just grow and grow on you. This one reads a
263  * 'node' and its children.
264  */
265 data1_node *data1_read_node (data1_handle dh, const char **buf,
266                              data1_node *parent, int *line,
267                              data1_absyn *absyn, NMEM m)
268 {
269     data1_node *res;
270
271     while (**buf && isspace(**buf))
272     {
273         if (**buf == '\n')
274             (*line)++;
275         (*buf)++;
276     }
277     if (!**buf)
278         return 0;
279
280     if (**buf == '<') /* beginning of tag */
281     {
282         char tag[64];
283         char args[256];
284         int i;
285         const char *t = (*buf) + 1;
286         data1_node **pp;
287         data1_element *elem = 0;
288         
289         for (i = 0; *t && *t != '>' && !isspace(*t); t++)
290             if (i < (sizeof(tag)-1))
291                 tag[i++] = *t;
292         tag[i] = '\0';
293         while (isspace(*t))
294             t++;
295         for (i = 0; *t && *t != '>'; t++)
296             if (i < (sizeof(args)-1))
297                 args[i++] = *t;
298         args[i] = '\0';
299         if (*t != '>' && !isspace(*t))
300         {
301             logf(LOG_WARN, "d1: %d: Malformed tag", *line);
302             return 0;
303         }
304         /*
305          * if end-tag, see if we terminate parent. If so, consume and return.
306          * Else, return.
307          */
308         if (*tag == '/')
309         {
310             if (parent && (!*(tag +1) ||
311                     (parent->which == DATA1N_root &&
312                      !strcmp(tag + 1,parent->u.root.type)) ||
313                     (parent->which == DATA1N_tag &&
314                      !strcmp(tag + 1, parent->u.tag.tag))))
315                 *buf = t + 1;
316             return 0;
317         }       
318         if (!absyn) /* parent node - what are we? */
319         {
320             if (!(absyn = data1_get_absyn (dh, tag)))
321             {
322                 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
323                     tag);
324                 return 0;
325             }
326             res = data1_mk_node (dh, m);
327             res->which = DATA1N_root;
328             res->u.root.type = data1_insert_string (dh, res, m, tag);
329             res->u.root.absyn = absyn;
330             res->root = res;
331             *buf = t + 1;
332         }
333         else if (!strcmp(tag, "var"))
334         {
335             char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
336             data1_vartype *tp;
337             int val_offset;
338             data1_node *p;
339
340             if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
341             {
342                 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
343                 return 0;
344             }
345             if (!(tp =
346                   data1_getvartypebyct(dh, parent->root->u.root.absyn->varset,
347                                        tclass, type)))
348                 return 0;
349             
350             /*
351              * If we're the first variant in this group, create a parent var,
352              * and insert it before the current variant.
353              */
354             if (parent->which != DATA1N_variant)
355             {
356                 res = data1_mk_node (dh, m);
357                 res->which = DATA1N_variant;
358                 res->u.variant.type = 0;
359                 res->u.variant.value = 0;
360                 res->root = parent->root;
361             }
362             else
363             {
364                 /*
365                  * now determine if one of our ancestor triples is of same type.
366                  * If so, we break here. This will make the parser unwind until
367                  * we become a sibling (alternate variant) to the aforementioned
368                  * triple. It stinks that we re-parse these tags on every
369                  * iteration of this. This is a function in need of a rewrite.
370                  */
371                 for (p = parent; p->which == DATA1N_variant; p = p->parent)
372                     if (p->u.variant.type == tp)
373                         return 0;
374                 res =  data1_mk_node (dh, m);
375                 res->which = DATA1N_variant;
376                 res->root = parent->root;
377                 res->u.variant.type = tp;
378                 res->u.variant.value =
379                     data1_insert_string (dh, res, m, args + val_offset);
380                 *buf = t + 1;
381             }
382         }
383         else /* tag.. acquire our element in the abstract syntax */
384         {
385             data1_node *partag = get_parent_tag (dh, parent);
386             data1_element *e = 0;
387             int localtag = 0;
388
389             if (parent->which == DATA1N_variant)
390                 return 0;
391             if (partag)
392                 if (!(e = partag->u.tag.element))
393                     localtag = 1; /* our parent is a local tag */
394
395             elem = data1_getelementbytagname(dh, absyn, e, tag);
396             res = data1_mk_node (dh, m);
397             res->which = DATA1N_tag;
398             res->u.tag.tag = data1_insert_string (dh, res, m, tag);
399             res->u.tag.element = elem;
400             res->u.tag.node_selected = 0;
401             res->u.tag.make_variantlist = 0;
402             res->u.tag.no_data_requested = 0;
403             res->u.tag.get_bytes = -1;
404             res->root = parent->root;
405             *buf = t + 1;
406         }
407
408         res->parent = parent;
409         pp = &res->child;
410         /*
411          * Read child nodes.
412          */
413         while ((*pp = data1_read_node(dh, buf, res, line, absyn, m)))
414         {
415             res->last_child = *pp;
416             pp = &(*pp)->next;
417         }
418     }
419     else /* != '<'... this is a body of text */
420     {
421         const char *src;
422         char *dst;
423         int len, prev_char = 0;
424
425         if (!parent)
426             return 0;
427
428         res = data1_mk_node(dh, m);
429         res->parent = parent;
430         res->which = DATA1N_data;
431         res->u.data.what = DATA1I_text;
432         res->u.data.formatted_text = 0;
433         res->root = parent->root;
434
435         /* determine length of "data" */
436         src = strchr (*buf, '<');
437         if (src)
438             len = src - *buf;
439         else
440             len = strlen (*buf);
441
442         /* use local buffer of nmem if too large */
443         if (len >= DATA1_LOCALDATA)
444             res->u.data.data = (char*) nmem_malloc (m, len);
445         else
446             res->u.data.data = res->lbuf;
447
448         /* read "data" and transfer while removing white space */
449         dst = res->u.data.data;
450         for (src = *buf; --len >= 0; src++)
451         {
452             if (*src == '\n')
453                 (*line)++;
454             if (isspace (*src))
455                 prev_char = ' ';
456             else
457             {
458                 if (prev_char)
459                 {
460                     *dst++ = prev_char;
461                     prev_char = 0;
462                 }
463                 *dst++ = *src;
464             }
465         }
466         *buf = src;
467         res->u.data.len = dst - res->u.data.data;
468     }
469     return res;
470 }
471
472 /*
473  * Read a record in the native syntax.
474  */
475 data1_node *data1_read_record(data1_handle dh,
476                               int (*rf)(void *, char *, size_t), void *fh,
477                               NMEM m)
478 {
479     int *size;
480     char **buf = data1_get_read_buf (dh, &size);
481     const char *bp;
482     int rd = 0, res;
483     int line = 0;
484     
485     if (!*buf)
486         *buf = (char *)xmalloc(*size = 4096);
487     
488     for (;;)
489     {
490         if (rd + 4096 > *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
491             abort();
492         if ((res = (*rf)(fh, *buf + rd, 4096)) <= 0)
493         {
494             if (!res)
495             {
496                 bp = *buf;
497                 return data1_read_node(dh, &bp, 0, &line, 0, m);
498             }
499             else
500                 return 0;
501         }
502         rd += res;
503     }
504 }
505
506 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
507 {
508     const char *bp = buf;
509     int line = 0;
510     return data1_read_node (dh, &bp, 0, &line, 0, m);
511 }