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