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