Fixed data1_gettagbyname. Bug introduced by previous revision.
[yaz-moved-to-github.git] / retrieval / d1_map.c
1 /*
2  * Copyright (c) 1995-1997, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_map.c,v $
7  * Revision 1.14  1998-10-13 16:09:50  adam
8  * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
9  * Added support for multiple attribute set references and tagset references
10  * from an abstract syntax file.
11  * Fixed many bad logs-calls in routines that read the various
12  * specifications regarding data1 (*.abs,*.att,...) and made the messages
13  * consistent whenever possible.
14  * Added extra 'lineno' argument to function readconf_line.
15  *
16  * Revision 1.13  1998/02/11 11:53:35  adam
17  * Changed code so that it compiles as C++.
18  *
19  * Revision 1.12  1997/11/18 09:51:09  adam
20  * Removed element num_children from data1_node. Minor changes in
21  * data1 to Explain.
22  *
23  * Revision 1.11  1997/09/17 12:10:36  adam
24  * YAZ version 1.4.
25  *
26  * Revision 1.10  1997/09/05 09:50:56  adam
27  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
28  *
29  * Revision 1.9  1996/06/10 08:56:02  quinn
30  * Work on Summary.
31  *
32  * Revision 1.8  1996/05/01  12:45:31  quinn
33  * Support use of local tag names in abs file.
34  *
35  * Revision 1.7  1995/12/13  13:44:31  quinn
36  * Modified Data1-system to use nmem
37  *
38  * Revision 1.6  1995/12/12  16:37:08  quinn
39  * Added destroy element to data1_node.
40  *
41  * Revision 1.5  1995/12/12  14:11:31  quinn
42  * More work on the large-record problem.
43  *
44  * Revision 1.4  1995/12/11  15:22:37  quinn
45  * Added last_child field to the node.
46  * Rewrote schema-mapping.
47  *
48  * Revision 1.3  1995/11/01  16:34:56  quinn
49  * Making data1 look for tables in data1_tabpath
50  *
51  * Revision 1.2  1995/11/01  13:54:46  quinn
52  * Minor adjustments
53  *
54  * Revision 1.1  1995/11/01  11:56:08  quinn
55  * Added Retrieval (data management) functions en masse.
56  *
57  *
58  */
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <ctype.h>
64
65 #include <oid.h>
66 #include <log.h>
67 #include <readconf.h>
68
69 #include <tpath.h>
70 #include <data1.h>
71 #include <d1_map.h>
72
73 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
74 {
75     NMEM mem = data1_nmem_get (dh);
76     data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
77     FILE *f;
78     int lineno = 0;
79     int argc;
80     char *argv[50], line[512];
81     data1_mapunit **mapp;
82     int local_numeric = 0;
83
84     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
85     {
86         logf(LOG_WARN|LOG_ERRNO, "%s", file);
87         return 0;
88     }
89
90     res->name = 0;
91     res->target_absyn_ref = VAL_NONE;
92     res->map = 0;
93     mapp = &res->map;
94     res->next = 0;
95
96     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
97         if (!strcmp(argv[0], "targetref"))
98         {
99             if (argc != 2)
100             {
101                 logf(LOG_WARN, "%s:%d: Bad # args for targetref",
102                     file, lineno);
103                 continue;
104             }
105             if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
106                 == VAL_NONE)
107             {
108                 logf(LOG_WARN, "%s:%d: Unknown reference '%s'",
109                      file, lineno, argv[1]);
110                 continue;
111             }
112         }
113         else if (!strcmp(argv[0], "targetname"))
114         {
115             if (argc != 2)
116             {
117                 logf(LOG_WARN, "%s:%d: Bad # args for targetname",
118                     file, lineno);
119                 continue;
120             }
121             res->target_absyn_name =
122                 (char *)nmem_malloc(mem, strlen(argv[1])+1);
123             strcpy(res->target_absyn_name, argv[1]);
124         }
125         else if (!yaz_matchstr(argv[0], "localnumeric"))
126             local_numeric = 1;
127         else if (!strcmp(argv[0], "name"))
128         {
129             if (argc != 2)
130             {
131                 logf(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
132                 continue;
133             }
134             res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
135             strcpy(res->name, argv[1]);
136         }
137         else if (!strcmp(argv[0], "map"))
138         {
139             data1_maptag **mtp;
140             char *ep, *path = argv[2];
141
142             if (argc < 3)
143             {
144                 logf(LOG_WARN, "%s:%d: Bad # of args for map",
145                     file, lineno);
146                 continue;
147             }
148             *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
149             (*mapp)->next = 0;
150             if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
151                 (*mapp)->no_data = 1;
152             else
153                 (*mapp)->no_data = 0;
154             (*mapp)->source_element_name =
155                 (char *)nmem_malloc(mem, strlen(argv[1])+1);
156             strcpy((*mapp)->source_element_name, argv[1]);
157             mtp = &(*mapp)->target_path;
158             if (*path == '/')
159                 path++;
160             for (ep = strchr(path, '/'); path; (void)((path = ep) &&
161                 (ep = strchr(path, '/'))))
162             {
163                 int type, np;
164                 char valstr[512], parm[512];
165
166                 if (ep)
167                     ep++;
168                 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
169                     parm)) < 2)
170                 {
171                     logf(LOG_WARN, "%s:%d: Syntax error in map directive: %s",
172                         file, lineno, argv[2]);
173                     fclose(f);
174                     return 0;
175                 }
176                 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
177                 (*mtp)->next = 0;
178                 (*mtp)->type = type;
179                 if (np > 2 && !data1_matchstr(parm, "new"))
180                     (*mtp)->new_field = 1;
181                 else
182                     (*mtp)->new_field = 0;
183                 if ((type != 3 || local_numeric) && isdigit(*valstr))
184                 {
185                     (*mtp)->which = D1_MAPTAG_numeric;
186                     (*mtp)->value.numeric = atoi(valstr);
187                 }
188                 else
189                 {
190                     (*mtp)->which = D1_MAPTAG_string;
191                     (*mtp)->value.string =
192                         (char *)nmem_malloc(mem, strlen(valstr)+1);
193                     strcpy((*mtp)->value.string, valstr);
194                 }
195                 mtp = &(*mtp)->next;
196             }
197             mapp = &(*mapp)->next;
198         }
199         else 
200             logf(LOG_WARN, "%s:%d: Unknown directive '%s'",
201                  file, lineno, argv[0]);
202
203     fclose(f);
204     return res;
205 }
206
207 /*
208  * Locate node with given elementname.
209  * NOTE: This is stupid - we don't find repeats this way.
210  */
211 static data1_node *find_node(data1_node *p, char *elementname)
212 {
213     data1_node *c, *r;
214
215     for (c = p->child; c; c = c->next)
216         if (c->which == DATA1N_tag && c->u.tag.element &&
217             !data1_matchstr(c->u.tag.element->name, elementname))
218             return c;
219         else if ((r = find_node(c, elementname)))
220             return r;
221     return 0;
222 }
223
224 /*
225  * See if the node n is equivalent to the tag t.
226  */
227 static int tagmatch(data1_node *n, data1_maptag *t)
228 {
229     if (n->which != DATA1N_tag)
230         return 0;
231     if (n->u.tag.element)
232     {
233         if (n->u.tag.element->tag->tagset)
234         {
235             if (n->u.tag.element->tag->tagset->type != t->type)
236                 return 0;
237         }
238         else if (t->type != 3)
239             return 0;
240         if (n->u.tag.element->tag->which == DATA1T_numeric)
241         {
242             if (t->which != D1_MAPTAG_numeric)
243                 return 0;
244             if (n->u.tag.element->tag->value.numeric != t->value.numeric)
245                 return 0;
246         }
247         else
248         {
249             if (t->which != D1_MAPTAG_string)
250                 return 0;
251             if (data1_matchstr(n->u.tag.element->tag->value.string,
252                 t->value.string))
253                 return 0;
254         }
255     }
256     else /* local tag */
257     {
258         char str[10];
259
260         if (t->type != 3)
261             return 0;
262         if (t->which == D1_MAPTAG_numeric)
263             sprintf(str, "%d", t->value.numeric);
264         else
265             strcpy(str, t->value.string);
266         if (data1_matchstr(n->u.tag.tag, str))
267             return 0;
268     }
269     return 1;
270 }
271
272 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
273                         data1_node *res, NMEM mem)
274 {
275     data1_node *c;
276     data1_mapunit *m;
277     /*
278      * locate each source element in turn.
279      */
280     for (c = n->child; c; c = c->next)
281         if (c->which == DATA1N_tag && c->u.tag.element)
282         {
283             for (m = map->map; m; m = m->next)
284             {
285                 if (!data1_matchstr(m->source_element_name,
286                     c->u.tag.element->name))
287                 {
288                     data1_node *pn = res;
289                     data1_node *cur = pn->last_child;
290                     data1_maptag *mt;
291
292                     /*
293                      * process the target path specification.
294                      */
295                     for (mt = m->target_path; mt; mt = mt->next)
296                     {
297                         if (!cur || mt->new_field || !tagmatch(cur, mt))
298                         {
299                             cur = data1_mk_node (dh, mem);
300                             cur->which = DATA1N_tag;
301                             cur->u.tag.element = 0;
302                             cur->u.tag.tag = mt->value.string;
303                             cur->u.tag.node_selected = 0;
304                             cur->parent = pn;
305                             cur->root = pn->root;
306                             if (!pn->child)
307                                 pn->child = cur;
308                             if (pn->last_child)
309                                 pn->last_child->next = cur;
310                             pn->last_child = cur;
311                         }
312                         
313                         if (mt->next)
314                             pn = cur;
315                         else if (!m->no_data)
316                         {
317                             cur->child = c->child;
318                             cur->last_child = c->last_child;
319                             c->child = 0;
320                             c->last_child = 0;
321                         }
322                     }
323                     break;
324                 }
325             }
326             if (map_children(dh, c, map, res, mem) < 0)
327                 return -1;
328         }
329     return 0;
330 }
331
332 /*
333  * Create a (possibly lossy) copy of the given record based on the
334  * table. The new copy will refer back to the data of the original record,
335  * which should not be discarded during the lifetime of the copy.
336  */
337 data1_node *data1_map_record (data1_handle dh, data1_node *n,
338                               data1_maptab *map, NMEM m)
339 {
340     data1_node *res = data1_mk_node(dh, m);
341
342     res->which = DATA1N_root;
343     res->u.root.type = map->target_absyn_name;
344     if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
345     {
346         logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
347             map->name, map->target_absyn_name);
348     }
349     res->parent = 0;
350     res->root = res;
351
352     if (map_children(dh, n, map, res, m) < 0)
353     {
354         data1_free_tree(dh, res);
355         return 0;
356     }
357     return res;
358 }
359