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