Moved matchstr to global util
[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.7  1995-12-13 13:44:31  quinn
8  * Modified Data1-system to use nmem
9  *
10  * Revision 1.6  1995/12/12  16:37:08  quinn
11  * Added destroy element to data1_node.
12  *
13  * Revision 1.5  1995/12/12  14:11:31  quinn
14  * More work on the large-record problem.
15  *
16  * Revision 1.4  1995/12/11  15:22:37  quinn
17  * Added last_child field to the node.
18  * Rewrote schema-mapping.
19  *
20  * Revision 1.3  1995/11/01  16:34:56  quinn
21  * Making data1 look for tables in data1_tabpath
22  *
23  * Revision 1.2  1995/11/01  13:54:46  quinn
24  * Minor adjustments
25  *
26  * Revision 1.1  1995/11/01  11:56:08  quinn
27  * Added Retrieval (data management) functions en masse.
28  *
29  *
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <oid.h>
37 #include <xmalloc.h>
38 #include <log.h>
39 #include <readconf.h>
40
41 #include <tpath.h>
42 #include <data1.h>
43 #include "d1_map.h"
44
45 data1_maptab *data1_read_maptab(char *file)
46 {
47     data1_maptab *res = xmalloc(sizeof(*res));
48     FILE *f;
49     int argc;
50     char *argv[50], line[512];
51     data1_mapunit **mapp;
52
53     if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
54     {
55         logf(LOG_WARN|LOG_ERRNO, "%s", file);
56         return 0;
57     }
58
59     res->name = 0;
60     res->target_absyn_ref = ODR_NONE;
61     res->map = 0;
62     mapp = &res->map;
63     res->next = 0;
64
65     while ((argc = readconf_line(f, line, 512, argv, 50)))
66         if (!strcmp(argv[0], "targetref"))
67         {
68             if (argc != 2)
69             {
70                 logf(LOG_WARN, "%s: one argument required for targetref",
71                     file);
72                 continue;
73             }
74             if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
75             {
76                 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
77                 continue;
78             }
79         }
80         else if (!strcmp(argv[0], "targetname"))
81         {
82             if (argc != 2)
83             {
84                 logf(LOG_WARN, "%s: one argument required for targetref",
85                     file);
86                 continue;
87             }
88             res->target_absyn_name = xmalloc(strlen(argv[1])+1);
89             strcpy(res->target_absyn_name, argv[1]);
90         }
91         else if (!strcmp(argv[0], "name"))
92         {
93             if (argc != 2)
94             {
95                 logf(LOG_WARN, "%s: one argument required for name",
96                     file);
97                 continue;
98             }
99             res->name = xmalloc(strlen(argv[1])+1);
100             strcpy(res->name, argv[1]);
101         }
102         else if (!strcmp(argv[0], "map"))
103         {
104             data1_maptag **mtp;
105             char *ep, *path = argv[2];
106
107             if (argc < 3)
108             {
109                 logf(LOG_WARN, "%s: At least 2 arguments required for map",
110                     file);
111                 continue;
112             }
113             *mapp = xmalloc(sizeof(**mapp));
114             (*mapp)->next = 0;
115             if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
116                 (*mapp)->no_data = 1;
117             else
118                 (*mapp)->no_data = 0;
119             (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
120             strcpy((*mapp)->source_element_name, argv[1]);
121             mtp = &(*mapp)->target_path;
122             if (*path == '/')
123                 path++;
124             for (ep = strchr(path, '/'); path; (void)((path = ep) &&
125                 (ep = strchr(path, '/'))))
126             {
127                 int type, np;
128                 char valstr[512], parm[512];
129
130                 if (ep)
131                     ep++;
132                 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
133                     parm)) < 2)
134                 {
135                     logf(LOG_WARN, "%s: Syntax error in map directive: %s",
136                         file, argv[2]);
137                     fclose(f);
138                     return 0;
139                 }
140                 *mtp = xmalloc(sizeof(**mtp));
141                 (*mtp)->next = 0;
142                 (*mtp)->type = type;
143                 if (np > 2 && !data1_matchstr(parm, "new"))
144                     (*mtp)->new_field = 1;
145                 else
146                     (*mtp)->new_field = 0;
147 #if 0
148                 if ((numval = atoi(valstr)))
149                 {
150                     (*mtp)->which = D1_MAPTAG_numeric;
151                     (*mtp)->value.numeric = numval;
152                 }
153                 else
154                 {
155 #endif
156                     (*mtp)->which = D1_MAPTAG_string;
157                     (*mtp)->value.string = xmalloc(strlen(valstr)+1);
158                     strcpy((*mtp)->value.string, valstr);
159 #if 0
160                 }
161 #endif 
162                 mtp = &(*mtp)->next;
163             }
164             mapp = &(*mapp)->next;
165         }
166         else 
167             logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
168
169     fclose(f);
170     return res;
171 }
172
173 #if 1
174
175 /*
176  * Locate node with givel elementname.
177  * NOTE: This is stupid - we don't find repeats this way.
178  */
179 static data1_node *find_node(data1_node *p, char *elementname)
180 {
181     data1_node *c, *r;
182
183     for (c = p->child; c; c = c->next)
184         if (c->which == DATA1N_tag && c->u.tag.element &&
185             !data1_matchstr(c->u.tag.element->name, elementname))
186             return c;
187         else if ((r = find_node(c, elementname)))
188             return r;
189     return 0;
190 }
191
192 /*
193  * See if the node n is equivalent to the tag t.
194  */
195 static int tagmatch(data1_node *n, data1_maptag *t)
196 {
197     if (n->which != DATA1N_tag)
198         return 0;
199     if (n->u.tag.element)
200     {
201         if (n->u.tag.element->tag->tagset->type != t->type)
202             return 0;
203         if (n->u.tag.element->tag->which == DATA1T_numeric)
204         {
205             if (t->which != D1_MAPTAG_numeric)
206                 return 0;
207             if (n->u.tag.element->tag->value.numeric != t->value.numeric)
208                 return 0;
209         }
210         else
211         {
212             if (t->which != D1_MAPTAG_string)
213                 return 0;
214             if (data1_matchstr(n->u.tag.element->tag->value.string,
215                 t->value.string))
216                 return 0;
217         }
218     }
219     else /* local tag */
220     {
221         char str[10];
222
223         if (t->type != 3)
224             return 0;
225         if (t->which == D1_MAPTAG_numeric)
226             sprintf(str, "%d", t->value.numeric);
227         else
228             strcpy(str, t->value.string);
229         if (data1_matchstr(n->u.tag.tag, str))
230             return 0;
231     }
232     return 1;
233 }
234
235 static int map_children(data1_node *n, data1_maptab *map, data1_node *res,
236     NMEM mem)
237 {
238     data1_node *c;
239     data1_mapunit *m;
240     /*
241      * locate each source element in turn.
242      */
243     for (c = n->child; c; c = c->next)
244         if (c->which == DATA1N_tag && c->u.tag.element)
245         {
246             for (m = map->map; m; m = m->next)
247             {
248                 if (!data1_matchstr(m->source_element_name,
249                     c->u.tag.element->name))
250                 {
251                     data1_node *pn = res;
252                     data1_node *cur = pn->last_child;
253                     data1_maptag *mt;
254
255                     /*
256                      * process the target path specification.
257                      */
258                     for (mt = m->target_path; mt; mt = mt->next)
259                     {
260                         if (!cur || mt->new_field || !tagmatch(cur, mt))
261                         {
262                             cur = data1_mk_node(mem);
263                             cur->which = DATA1N_tag;
264                             cur->u.tag.element = 0;
265                             cur->u.tag.tag = mt->value.string;
266                             cur->u.tag.node_selected = 0;
267                             cur->parent = pn;
268                             cur->root = pn->root;
269                             if (!pn->child)
270                                 pn->child = cur;
271                             if (pn->last_child)
272                                 pn->last_child->next = cur;
273                             pn->last_child = cur;
274                             pn->num_children++;
275                         }
276                         
277                         if (mt->next)
278                             pn = cur;
279                         else if (!m->no_data)
280                         {
281                             cur->child = c->child;
282                             cur->last_child = c->last_child;
283                             cur->num_children = c->num_children;
284                             c->child = 0;
285                             c->last_child = 0;
286                             c->num_children = 0;
287                         }
288                     }
289                     break;
290                 }
291             }
292             if (map_children(c, map, res, mem) < 0)
293                 return -1;
294         }
295     return 0;
296 }
297
298
299 #else
300
301 /*
302  * See if the node n is equivalent to the tag t.
303  */
304 static int tagmatch(data1_node *n, data1_maptag *t)
305 {
306     if (n->which != DATA1N_tag)
307         return 0;
308     if (n->u.tag.element)
309     {
310         if (n->u.tag.element->tag->tagset->type != t->type)
311             return 0;
312         if (n->u.tag.element->tag->which == DATA1T_numeric)
313         {
314             if (t->which != D1_MAPTAG_numeric)
315                 return 0;
316             if (n->u.tag.element->tag->value.numeric != t->value.numeric)
317                 return 0;
318         }
319         else
320         {
321             if (t->which != D1_MAPTAG_string)
322                 return 0;
323             if (data1_matchstr(n->u.tag.element->tag->value.string,
324                 t->value.string))
325                 return 0;
326         }
327     }
328     else /* local tag */
329     {
330         char str[10];
331
332         if (t->type != 3)
333             return 0;
334         if (t->which == D1_MAPTAG_numeric)
335             sprintf(str, "%d", t->value.numeric);
336         else
337             strcpy(str, t->value.string);
338         if (data1_matchstr(n->u.tag.tag, str))
339             return 0;
340     }
341     return 1;
342 }
343
344 static int map_elements(data1_node *res, data1_node *n, data1_mapunit *m)
345 {
346     data1_node *c;
347
348     for (c = n->child; c; c = c->next)
349     {
350         if (c->which == DATA1N_tag)
351         {
352             if (c->u.tag.element && !data1_matchstr(c->u.tag.element->name,
353                 m->source_element_name))
354             {
355                 /* Process target path specification */
356                 data1_maptag *mt;
357                 data1_node *pn = res, *cur = pn->last_child;
358
359                 for (mt = m->target_path; mt; mt = mt->next)
360                 {
361                     if (!cur || !tagmatch(cur, mt))
362                     {
363                         cur = data1_mk_node();
364                         cur->which = DATA1N_tag;
365                         cur->u.tag.element = 0;
366                         cur->u.tag.tag = mt->value.string;
367                         cur->u.tag.node_selected = 0;
368                         cur->parent = pn;
369                         cur->root = pn->root;
370                         if (!pn->child)
371                             pn->child = cur;
372                         if (pn->last_child)
373                             pn->last_child->next = cur;
374                         pn->last_child = cur;
375                         pn->num_children++;
376                     }
377                     if (mt->next)
378                         pn = cur;
379                     else if (!m->no_data)
380                     {
381                         cur->child = c->child;
382                         cur->num_children = c->num_children;
383                         c->child = 0;
384                         c->num_children = 0;
385                     }
386                 }
387             }
388             else if (map_elements(res, c, m) < 0)
389                 return -1;
390         }
391     }
392     return 0;
393 }
394
395 static int map_record(data1_node *res, data1_node *n, data1_maptab *map)
396 {
397     data1_mapunit *m;
398
399     for (m = map->map; m; m = m->next)
400         if (map_elements(res, n, m) < 0)
401             return -1;
402     return 0;
403 }
404
405 #endif
406
407 /*
408  * Create a (possibly lossy) copy of the given record based on the
409  * table. The new copy will refer back to the data of the original record,
410  * which should not be discarded during the lifetime of the copy.
411  */
412 data1_node *data1_map_record(data1_node *n, data1_maptab *map, NMEM m)
413 {
414     data1_node *res = data1_mk_node(m);
415
416     res->which = DATA1N_root;
417     res->u.root.type = map->target_absyn_name;
418     if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
419     {
420         logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
421             map->name, map->target_absyn_name);
422     }
423     res->parent = 0;
424     res->root = res;
425
426     if (map_children(n, map, res, m) < 0)
427     {
428         data1_free_tree(res);
429         return 0;
430     }
431     return res;
432 }
433