2 * Copyright (c) 1995-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 2002-05-03 13:48:27 adam
10 * Revision 1.19 2002/04/04 20:49:46 adam
11 * New functions yaz_is_abspath, yaz_path_fopen_base
13 * Revision 1.18 2000/11/29 14:22:47 adam
14 * Implemented XML/SGML attributes for data1 so that d1_read reads them
15 * and d1_write generates proper attributes for XML/SGML records. Added
16 * register locking for threaded version.
18 * Revision 1.17 1999/11/30 13:47:12 adam
19 * Improved installation. Moved header files to include/yaz.
21 * Revision 1.16 1999/10/21 12:06:29 adam
22 * Retrieval module no longer uses ctype.h - functions.
24 * Revision 1.15 1999/08/27 09:40:32 adam
25 * Renamed logf function to yaz_log. Removed VC++ project files.
27 * Revision 1.14 1998/10/13 16:09:50 adam
28 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
29 * Added support for multiple attribute set references and tagset references
30 * from an abstract syntax file.
31 * Fixed many bad logs-calls in routines that read the various
32 * specifications regarding data1 (*.abs,*.att,...) and made the messages
33 * consistent whenever possible.
34 * Added extra 'lineno' argument to function readconf_line.
36 * Revision 1.13 1998/02/11 11:53:35 adam
37 * Changed code so that it compiles as C++.
39 * Revision 1.12 1997/11/18 09:51:09 adam
40 * Removed element num_children from data1_node. Minor changes in
43 * Revision 1.11 1997/09/17 12:10:36 adam
46 * Revision 1.10 1997/09/05 09:50:56 adam
47 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
49 * Revision 1.9 1996/06/10 08:56:02 quinn
52 * Revision 1.8 1996/05/01 12:45:31 quinn
53 * Support use of local tag names in abs file.
55 * Revision 1.7 1995/12/13 13:44:31 quinn
56 * Modified Data1-system to use nmem
58 * Revision 1.6 1995/12/12 16:37:08 quinn
59 * Added destroy element to data1_node.
61 * Revision 1.5 1995/12/12 14:11:31 quinn
62 * More work on the large-record problem.
64 * Revision 1.4 1995/12/11 15:22:37 quinn
65 * Added last_child field to the node.
66 * Rewrote schema-mapping.
68 * Revision 1.3 1995/11/01 16:34:56 quinn
69 * Making data1 look for tables in data1_tabpath
71 * Revision 1.2 1995/11/01 13:54:46 quinn
74 * Revision 1.1 1995/11/01 11:56:08 quinn
75 * Added Retrieval (data management) functions en masse.
86 #include <yaz/readconf.h>
87 #include <yaz/tpath.h>
88 #include <yaz/data1.h>
89 #include <yaz/d1_map.h>
91 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
93 NMEM mem = data1_nmem_get (dh);
94 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
98 char *argv[50], line[512];
100 int local_numeric = 0;
102 if (!(f = data1_path_fopen(dh, file, "r")))
104 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
109 res->target_absyn_ref = VAL_NONE;
114 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
115 if (!strcmp(argv[0], "targetref"))
119 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetref",
123 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
126 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
127 file, lineno, argv[1]);
131 else if (!strcmp(argv[0], "targetname"))
135 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetname",
139 res->target_absyn_name =
140 (char *)nmem_malloc(mem, strlen(argv[1])+1);
141 strcpy(res->target_absyn_name, argv[1]);
143 else if (!yaz_matchstr(argv[0], "localnumeric"))
145 else if (!strcmp(argv[0], "name"))
149 yaz_log(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
152 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
153 strcpy(res->name, argv[1]);
155 else if (!strcmp(argv[0], "map"))
158 char *ep, *path = argv[2];
162 yaz_log(LOG_WARN, "%s:%d: Bad # of args for map",
166 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
168 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
169 (*mapp)->no_data = 1;
171 (*mapp)->no_data = 0;
172 (*mapp)->source_element_name =
173 (char *)nmem_malloc(mem, strlen(argv[1])+1);
174 strcpy((*mapp)->source_element_name, argv[1]);
175 mtp = &(*mapp)->target_path;
178 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
179 (ep = strchr(path, '/'))))
182 char valstr[512], parm[512];
186 if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
189 yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
190 "directive: %s", file, lineno, argv[2]);
194 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
197 if (np > 2 && !data1_matchstr(parm, "new"))
198 (*mtp)->new_field = 1;
200 (*mtp)->new_field = 0;
201 if ((type != 3 || local_numeric) && d1_isdigit(*valstr))
203 (*mtp)->which = D1_MAPTAG_numeric;
204 (*mtp)->value.numeric = atoi(valstr);
208 (*mtp)->which = D1_MAPTAG_string;
209 (*mtp)->value.string =
210 (char *)nmem_malloc(mem, strlen(valstr)+1);
211 strcpy((*mtp)->value.string, valstr);
215 mapp = &(*mapp)->next;
218 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
219 file, lineno, argv[0]);
226 * Locate node with given elementname.
227 * NOTE: This is stupid - we don't find repeats this way.
229 static data1_node *find_node(data1_node *p, char *elementname)
233 for (c = p->child; c; c = c->next)
234 if (c->which == DATA1N_tag && c->u.tag.element &&
235 !data1_matchstr(c->u.tag.element->name, elementname))
237 else if ((r = find_node(c, elementname)))
243 * See if the node n is equivalent to the tag t.
245 static int tagmatch(data1_node *n, data1_maptag *t)
247 if (n->which != DATA1N_tag)
249 if (n->u.tag.element)
251 if (n->u.tag.element->tag->tagset)
253 if (n->u.tag.element->tag->tagset->type != t->type)
256 else if (t->type != 3)
258 if (n->u.tag.element->tag->which == DATA1T_numeric)
260 if (t->which != D1_MAPTAG_numeric)
262 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
267 if (t->which != D1_MAPTAG_string)
269 if (data1_matchstr(n->u.tag.element->tag->value.string,
280 if (t->which == D1_MAPTAG_numeric)
281 sprintf(str, "%d", t->value.numeric);
283 strcpy(str, t->value.string);
284 if (data1_matchstr(n->u.tag.tag, str))
290 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
291 data1_node *res, NMEM mem)
296 * locate each source element in turn.
298 for (c = n->child; c; c = c->next)
299 if (c->which == DATA1N_tag && c->u.tag.element)
301 for (m = map->map; m; m = m->next)
303 if (!data1_matchstr(m->source_element_name,
304 c->u.tag.element->name))
306 data1_node *pn = res;
307 data1_node *cur = pn->last_child;
311 * process the target path specification.
313 for (mt = m->target_path; mt; mt = mt->next)
315 if (!cur || mt->new_field || !tagmatch(cur, mt))
317 cur = data1_mk_node (dh, mem, DATA1N_tag, pn);
318 cur->u.tag.tag = mt->value.string;
323 else if (!m->no_data)
325 cur->child = c->child;
326 cur->last_child = c->last_child;
334 if (map_children(dh, c, map, res, mem) < 0)
341 * Create a (possibly lossy) copy of the given record based on the
342 * table. The new copy will refer back to the data of the original record,
343 * which should not be discarded during the lifetime of the copy.
345 data1_node *data1_map_record (data1_handle dh, data1_node *n,
346 data1_maptab *map, NMEM m)
348 data1_node *res = data1_mk_node(dh, m, DATA1N_root, 0);
350 res->which = DATA1N_root;
351 res->u.root.type = map->target_absyn_name;
352 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
354 yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
355 map->name, map->target_absyn_name);
357 if (map_children(dh, n, map, res, m) < 0)
359 data1_free_tree(dh, res);