2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1999-08-27 09:40:32 adam
8 * Renamed logf function to yaz_log. Removed VC++ project files.
10 * Revision 1.14 1998/10/13 16:09:50 adam
11 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
12 * Added support for multiple attribute set references and tagset references
13 * from an abstract syntax file.
14 * Fixed many bad logs-calls in routines that read the various
15 * specifications regarding data1 (*.abs,*.att,...) and made the messages
16 * consistent whenever possible.
17 * Added extra 'lineno' argument to function readconf_line.
19 * Revision 1.13 1998/02/11 11:53:35 adam
20 * Changed code so that it compiles as C++.
22 * Revision 1.12 1997/11/18 09:51:09 adam
23 * Removed element num_children from data1_node. Minor changes in
26 * Revision 1.11 1997/09/17 12:10:36 adam
29 * Revision 1.10 1997/09/05 09:50:56 adam
30 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
32 * Revision 1.9 1996/06/10 08:56:02 quinn
35 * Revision 1.8 1996/05/01 12:45:31 quinn
36 * Support use of local tag names in abs file.
38 * Revision 1.7 1995/12/13 13:44:31 quinn
39 * Modified Data1-system to use nmem
41 * Revision 1.6 1995/12/12 16:37:08 quinn
42 * Added destroy element to data1_node.
44 * Revision 1.5 1995/12/12 14:11:31 quinn
45 * More work on the large-record problem.
47 * Revision 1.4 1995/12/11 15:22:37 quinn
48 * Added last_child field to the node.
49 * Rewrote schema-mapping.
51 * Revision 1.3 1995/11/01 16:34:56 quinn
52 * Making data1 look for tables in data1_tabpath
54 * Revision 1.2 1995/11/01 13:54:46 quinn
57 * Revision 1.1 1995/11/01 11:56:08 quinn
58 * Added Retrieval (data management) functions en masse.
76 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
78 NMEM mem = data1_nmem_get (dh);
79 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
83 char *argv[50], line[512];
85 int local_numeric = 0;
87 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
89 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
94 res->target_absyn_ref = VAL_NONE;
99 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
100 if (!strcmp(argv[0], "targetref"))
104 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetref",
108 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
111 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
112 file, lineno, argv[1]);
116 else if (!strcmp(argv[0], "targetname"))
120 yaz_log(LOG_WARN, "%s:%d: Bad # args for targetname",
124 res->target_absyn_name =
125 (char *)nmem_malloc(mem, strlen(argv[1])+1);
126 strcpy(res->target_absyn_name, argv[1]);
128 else if (!yaz_matchstr(argv[0], "localnumeric"))
130 else if (!strcmp(argv[0], "name"))
134 yaz_log(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
137 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
138 strcpy(res->name, argv[1]);
140 else if (!strcmp(argv[0], "map"))
143 char *ep, *path = argv[2];
147 yaz_log(LOG_WARN, "%s:%d: Bad # of args for map",
151 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
153 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
154 (*mapp)->no_data = 1;
156 (*mapp)->no_data = 0;
157 (*mapp)->source_element_name =
158 (char *)nmem_malloc(mem, strlen(argv[1])+1);
159 strcpy((*mapp)->source_element_name, argv[1]);
160 mtp = &(*mapp)->target_path;
163 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
164 (ep = strchr(path, '/'))))
167 char valstr[512], parm[512];
171 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
174 yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
175 "directive: %s", file, lineno, argv[2]);
179 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
182 if (np > 2 && !data1_matchstr(parm, "new"))
183 (*mtp)->new_field = 1;
185 (*mtp)->new_field = 0;
186 if ((type != 3 || local_numeric) && isdigit(*valstr))
188 (*mtp)->which = D1_MAPTAG_numeric;
189 (*mtp)->value.numeric = atoi(valstr);
193 (*mtp)->which = D1_MAPTAG_string;
194 (*mtp)->value.string =
195 (char *)nmem_malloc(mem, strlen(valstr)+1);
196 strcpy((*mtp)->value.string, valstr);
200 mapp = &(*mapp)->next;
203 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
204 file, lineno, argv[0]);
211 * Locate node with given elementname.
212 * NOTE: This is stupid - we don't find repeats this way.
214 static data1_node *find_node(data1_node *p, char *elementname)
218 for (c = p->child; c; c = c->next)
219 if (c->which == DATA1N_tag && c->u.tag.element &&
220 !data1_matchstr(c->u.tag.element->name, elementname))
222 else if ((r = find_node(c, elementname)))
228 * See if the node n is equivalent to the tag t.
230 static int tagmatch(data1_node *n, data1_maptag *t)
232 if (n->which != DATA1N_tag)
234 if (n->u.tag.element)
236 if (n->u.tag.element->tag->tagset)
238 if (n->u.tag.element->tag->tagset->type != t->type)
241 else if (t->type != 3)
243 if (n->u.tag.element->tag->which == DATA1T_numeric)
245 if (t->which != D1_MAPTAG_numeric)
247 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
252 if (t->which != D1_MAPTAG_string)
254 if (data1_matchstr(n->u.tag.element->tag->value.string,
265 if (t->which == D1_MAPTAG_numeric)
266 sprintf(str, "%d", t->value.numeric);
268 strcpy(str, t->value.string);
269 if (data1_matchstr(n->u.tag.tag, str))
275 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
276 data1_node *res, NMEM mem)
281 * locate each source element in turn.
283 for (c = n->child; c; c = c->next)
284 if (c->which == DATA1N_tag && c->u.tag.element)
286 for (m = map->map; m; m = m->next)
288 if (!data1_matchstr(m->source_element_name,
289 c->u.tag.element->name))
291 data1_node *pn = res;
292 data1_node *cur = pn->last_child;
296 * process the target path specification.
298 for (mt = m->target_path; mt; mt = mt->next)
300 if (!cur || mt->new_field || !tagmatch(cur, mt))
302 cur = data1_mk_node (dh, mem);
303 cur->which = DATA1N_tag;
304 cur->u.tag.element = 0;
305 cur->u.tag.tag = mt->value.string;
306 cur->u.tag.node_selected = 0;
308 cur->root = pn->root;
312 pn->last_child->next = cur;
313 pn->last_child = cur;
318 else if (!m->no_data)
320 cur->child = c->child;
321 cur->last_child = c->last_child;
329 if (map_children(dh, c, map, res, mem) < 0)
336 * Create a (possibly lossy) copy of the given record based on the
337 * table. The new copy will refer back to the data of the original record,
338 * which should not be discarded during the lifetime of the copy.
340 data1_node *data1_map_record (data1_handle dh, data1_node *n,
341 data1_maptab *map, NMEM m)
343 data1_node *res = data1_mk_node(dh, m);
345 res->which = DATA1N_root;
346 res->u.root.type = map->target_absyn_name;
347 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
349 yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
350 map->name, map->target_absyn_name);
355 if (map_children(dh, n, map, res, m) < 0)
357 data1_free_tree(dh, res);