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