2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-10-21 12:06:29 adam
8 * Retrieval module no longer uses ctype.h - functions.
10 * Revision 1.15 1999/08/27 09:40:32 adam
11 * Renamed logf function to yaz_log. Removed VC++ project files.
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.
22 * Revision 1.13 1998/02/11 11:53:35 adam
23 * Changed code so that it compiles as C++.
25 * Revision 1.12 1997/11/18 09:51:09 adam
26 * Removed element num_children from data1_node. Minor changes in
29 * Revision 1.11 1997/09/17 12:10:36 adam
32 * Revision 1.10 1997/09/05 09:50:56 adam
33 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
35 * Revision 1.9 1996/06/10 08:56:02 quinn
38 * Revision 1.8 1996/05/01 12:45:31 quinn
39 * Support use of local tag names in abs file.
41 * Revision 1.7 1995/12/13 13:44:31 quinn
42 * Modified Data1-system to use nmem
44 * Revision 1.6 1995/12/12 16:37:08 quinn
45 * Added destroy element to data1_node.
47 * Revision 1.5 1995/12/12 14:11:31 quinn
48 * More work on the large-record problem.
50 * Revision 1.4 1995/12/11 15:22:37 quinn
51 * Added last_child field to the node.
52 * Rewrote schema-mapping.
54 * Revision 1.3 1995/11/01 16:34:56 quinn
55 * Making data1 look for tables in data1_tabpath
57 * Revision 1.2 1995/11/01 13:54:46 quinn
60 * Revision 1.1 1995/11/01 11:56:08 quinn
61 * Added Retrieval (data management) functions en masse.
78 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
80 NMEM mem = data1_nmem_get (dh);
81 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
85 char *argv[50], line[512];
87 int local_numeric = 0;
89 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
91 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
96 res->target_absyn_ref = VAL_NONE;
101 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
102 if (!strcmp(argv[0], "targetref"))
106 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetref",
110 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
113 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
114 file, lineno, argv[1]);
118 else if (!strcmp(argv[0], "targetname"))
122 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetname",
126 res->target_absyn_name =
127 (char *)nmem_malloc(mem, strlen(argv[1])+1);
128 strcpy(res->target_absyn_name, argv[1]);
130 else if (!yaz_matchstr(argv[0], "localnumeric"))
132 else if (!strcmp(argv[0], "name"))
136 yaz_log(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
139 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
140 strcpy(res->name, argv[1]);
142 else if (!strcmp(argv[0], "map"))
145 char *ep, *path = argv[2];
149 yaz_log(LOG_WARN, "%s:%d: Bad # of args for map",
153 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
155 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
156 (*mapp)->no_data = 1;
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;
165 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
166 (ep = strchr(path, '/'))))
169 char valstr[512], parm[512];
173 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
176 yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
177 "directive: %s", file, lineno, argv[2]);
181 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
184 if (np > 2 && !data1_matchstr(parm, "new"))
185 (*mtp)->new_field = 1;
187 (*mtp)->new_field = 0;
188 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
190 (*mtp)->which = D1_MAPTAG_numeric;
191 (*mtp)->value.numeric = atoi(valstr);
195 (*mtp)->which = D1_MAPTAG_string;
196 (*mtp)->value.string =
197 (char *)nmem_malloc(mem, strlen(valstr)+1);
198 strcpy((*mtp)->value.string, valstr);
202 mapp = &(*mapp)->next;
205 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
206 file, lineno, argv[0]);
213 * Locate node with given elementname.
214 * NOTE: This is stupid - we don't find repeats this way.
216 static data1_node *find_node(data1_node *p, char *elementname)
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))
224 else if ((r = find_node(c, elementname)))
230 * See if the node n is equivalent to the tag t.
232 static int tagmatch(data1_node *n, data1_maptag *t)
234 if (n->which != DATA1N_tag)
236 if (n->u.tag.element)
238 if (n->u.tag.element->tag->tagset)
240 if (n->u.tag.element->tag->tagset->type != t->type)
243 else if (t->type != 3)
245 if (n->u.tag.element->tag->which == DATA1T_numeric)
247 if (t->which != D1_MAPTAG_numeric)
249 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
254 if (t->which != D1_MAPTAG_string)
256 if (data1_matchstr(n->u.tag.element->tag->value.string,
267 if (t->which == D1_MAPTAG_numeric)
268 sprintf(str, "%d", t->value.numeric);
270 strcpy(str, t->value.string);
271 if (data1_matchstr(n->u.tag.tag, str))
277 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
278 data1_node *res, NMEM mem)
283 * locate each source element in turn.
285 for (c = n->child; c; c = c->next)
286 if (c->which == DATA1N_tag && c->u.tag.element)
288 for (m = map->map; m; m = m->next)
290 if (!data1_matchstr(m->source_element_name,
291 c->u.tag.element->name))
293 data1_node *pn = res;
294 data1_node *cur = pn->last_child;
298 * process the target path specification.
300 for (mt = m->target_path; mt; mt = mt->next)
302 if (!cur || mt->new_field || !tagmatch(cur, mt))
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;
310 cur->root = pn->root;
314 pn->last_child->next = cur;
315 pn->last_child = cur;
320 else if (!m->no_data)
322 cur->child = c->child;
323 cur->last_child = c->last_child;
331 if (map_children(dh, c, map, res, mem) < 0)
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.
342 data1_node *data1_map_record (data1_handle dh, data1_node *n,
343 data1_maptab *map, NMEM m)
345 data1_node *res = data1_mk_node(dh, m);
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)))
351 yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
352 map->name, map->target_absyn_name);
357 if (map_children(dh, n, map, res, m) < 0)
359 data1_free_tree(dh, res);