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