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