2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1999-11-30 13:47:12 adam
8 * Improved installation. Moved header files to include/yaz.
10 * Revision 1.16 1999/10/21 12:06:29 adam
11 * Retrieval module no longer uses ctype.h - functions.
13 * Revision 1.15 1999/08/27 09:40:32 adam
14 * Renamed logf function to yaz_log. Removed VC++ project files.
16 * Revision 1.14 1998/10/13 16:09:50 adam
17 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
18 * Added support for multiple attribute set references and tagset references
19 * from an abstract syntax file.
20 * Fixed many bad logs-calls in routines that read the various
21 * specifications regarding data1 (*.abs,*.att,...) and made the messages
22 * consistent whenever possible.
23 * Added extra 'lineno' argument to function readconf_line.
25 * Revision 1.13 1998/02/11 11:53:35 adam
26 * Changed code so that it compiles as C++.
28 * Revision 1.12 1997/11/18 09:51:09 adam
29 * Removed element num_children from data1_node. Minor changes in
32 * Revision 1.11 1997/09/17 12:10:36 adam
35 * Revision 1.10 1997/09/05 09:50:56 adam
36 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
38 * Revision 1.9 1996/06/10 08:56:02 quinn
41 * Revision 1.8 1996/05/01 12:45:31 quinn
42 * Support use of local tag names in abs file.
44 * Revision 1.7 1995/12/13 13:44:31 quinn
45 * Modified Data1-system to use nmem
47 * Revision 1.6 1995/12/12 16:37:08 quinn
48 * Added destroy element to data1_node.
50 * Revision 1.5 1995/12/12 14:11:31 quinn
51 * More work on the large-record problem.
53 * Revision 1.4 1995/12/11 15:22:37 quinn
54 * Added last_child field to the node.
55 * Rewrote schema-mapping.
57 * Revision 1.3 1995/11/01 16:34:56 quinn
58 * Making data1 look for tables in data1_tabpath
60 * Revision 1.2 1995/11/01 13:54:46 quinn
63 * Revision 1.1 1995/11/01 11:56:08 quinn
64 * Added Retrieval (data management) functions en masse.
75 #include <yaz/readconf.h>
76 #include <yaz/tpath.h>
77 #include <yaz/data1.h>
78 #include <yaz/d1_map.h>
80 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
82 NMEM mem = data1_nmem_get (dh);
83 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
87 char *argv[50], line[512];
89 int local_numeric = 0;
91 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
93 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
98 res->target_absyn_ref = VAL_NONE;
103 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
104 if (!strcmp(argv[0], "targetref"))
108 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetref",
112 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
115 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
116 file, lineno, argv[1]);
120 else if (!strcmp(argv[0], "targetname"))
124 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetname",
128 res->target_absyn_name =
129 (char *)nmem_malloc(mem, strlen(argv[1])+1);
130 strcpy(res->target_absyn_name, argv[1]);
132 else if (!yaz_matchstr(argv[0], "localnumeric"))
134 else if (!strcmp(argv[0], "name"))
138 yaz_log(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
141 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
142 strcpy(res->name, argv[1]);
144 else if (!strcmp(argv[0], "map"))
147 char *ep, *path = argv[2];
151 yaz_log(LOG_WARN, "%s:%d: Bad # of args for map",
155 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
157 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
158 (*mapp)->no_data = 1;
160 (*mapp)->no_data = 0;
161 (*mapp)->source_element_name =
162 (char *)nmem_malloc(mem, strlen(argv[1])+1);
163 strcpy((*mapp)->source_element_name, argv[1]);
164 mtp = &(*mapp)->target_path;
167 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
168 (ep = strchr(path, '/'))))
171 char valstr[512], parm[512];
175 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
178 yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
179 "directive: %s", file, lineno, argv[2]);
183 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
186 if (np > 2 && !data1_matchstr(parm, "new"))
187 (*mtp)->new_field = 1;
189 (*mtp)->new_field = 0;
190 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
192 (*mtp)->which = D1_MAPTAG_numeric;
193 (*mtp)->value.numeric = atoi(valstr);
197 (*mtp)->which = D1_MAPTAG_string;
198 (*mtp)->value.string =
199 (char *)nmem_malloc(mem, strlen(valstr)+1);
200 strcpy((*mtp)->value.string, valstr);
204 mapp = &(*mapp)->next;
207 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
208 file, lineno, argv[0]);
215 * Locate node with given elementname.
216 * NOTE: This is stupid - we don't find repeats this way.
218 static data1_node *find_node(data1_node *p, char *elementname)
222 for (c = p->child; c; c = c->next)
223 if (c->which == DATA1N_tag && c->u.tag.element &&
224 !data1_matchstr(c->u.tag.element->name, elementname))
226 else if ((r = find_node(c, elementname)))
232 * See if the node n is equivalent to the tag t.
234 static int tagmatch(data1_node *n, data1_maptag *t)
236 if (n->which != DATA1N_tag)
238 if (n->u.tag.element)
240 if (n->u.tag.element->tag->tagset)
242 if (n->u.tag.element->tag->tagset->type != t->type)
245 else if (t->type != 3)
247 if (n->u.tag.element->tag->which == DATA1T_numeric)
249 if (t->which != D1_MAPTAG_numeric)
251 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
256 if (t->which != D1_MAPTAG_string)
258 if (data1_matchstr(n->u.tag.element->tag->value.string,
269 if (t->which == D1_MAPTAG_numeric)
270 sprintf(str, "%d", t->value.numeric);
272 strcpy(str, t->value.string);
273 if (data1_matchstr(n->u.tag.tag, str))
279 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
280 data1_node *res, NMEM mem)
285 * locate each source element in turn.
287 for (c = n->child; c; c = c->next)
288 if (c->which == DATA1N_tag && c->u.tag.element)
290 for (m = map->map; m; m = m->next)
292 if (!data1_matchstr(m->source_element_name,
293 c->u.tag.element->name))
295 data1_node *pn = res;
296 data1_node *cur = pn->last_child;
300 * process the target path specification.
302 for (mt = m->target_path; mt; mt = mt->next)
304 if (!cur || mt->new_field || !tagmatch(cur, mt))
306 cur = data1_mk_node (dh, mem);
307 cur->which = DATA1N_tag;
308 cur->u.tag.element = 0;
309 cur->u.tag.tag = mt->value.string;
310 cur->u.tag.node_selected = 0;
312 cur->root = pn->root;
316 pn->last_child->next = cur;
317 pn->last_child = cur;
322 else if (!m->no_data)
324 cur->child = c->child;
325 cur->last_child = c->last_child;
333 if (map_children(dh, c, map, res, mem) < 0)
340 * Create a (possibly lossy) copy of the given record based on the
341 * table. The new copy will refer back to the data of the original record,
342 * which should not be discarded during the lifetime of the copy.
344 data1_node *data1_map_record (data1_handle dh, data1_node *n,
345 data1_maptab *map, NMEM m)
347 data1_node *res = data1_mk_node(dh, m);
349 res->which = DATA1N_root;
350 res->u.root.type = map->target_absyn_name;
351 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
353 yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
354 map->name, map->target_absyn_name);
359 if (map_children(dh, n, map, res, m) < 0)
361 data1_free_tree(dh, res);