2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1998-10-13 16:09:50 adam
8 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
9 * Added support for multiple attribute set references and tagset references
10 * from an abstract syntax file.
11 * Fixed many bad logs-calls in routines that read the various
12 * specifications regarding data1 (*.abs,*.att,...) and made the messages
13 * consistent whenever possible.
14 * Added extra 'lineno' argument to function readconf_line.
16 * Revision 1.13 1998/02/11 11:53:35 adam
17 * Changed code so that it compiles as C++.
19 * Revision 1.12 1997/11/18 09:51:09 adam
20 * Removed element num_children from data1_node. Minor changes in
23 * Revision 1.11 1997/09/17 12:10:36 adam
26 * Revision 1.10 1997/09/05 09:50:56 adam
27 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
29 * Revision 1.9 1996/06/10 08:56:02 quinn
32 * Revision 1.8 1996/05/01 12:45:31 quinn
33 * Support use of local tag names in abs file.
35 * Revision 1.7 1995/12/13 13:44:31 quinn
36 * Modified Data1-system to use nmem
38 * Revision 1.6 1995/12/12 16:37:08 quinn
39 * Added destroy element to data1_node.
41 * Revision 1.5 1995/12/12 14:11:31 quinn
42 * More work on the large-record problem.
44 * Revision 1.4 1995/12/11 15:22:37 quinn
45 * Added last_child field to the node.
46 * Rewrote schema-mapping.
48 * Revision 1.3 1995/11/01 16:34:56 quinn
49 * Making data1 look for tables in data1_tabpath
51 * Revision 1.2 1995/11/01 13:54:46 quinn
54 * Revision 1.1 1995/11/01 11:56:08 quinn
55 * Added Retrieval (data management) functions en masse.
73 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
75 NMEM mem = data1_nmem_get (dh);
76 data1_maptab *res = (data1_maptab *)nmem_malloc(mem, sizeof(*res));
80 char *argv[50], line[512];
82 int local_numeric = 0;
84 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
86 logf(LOG_WARN|LOG_ERRNO, "%s", file);
91 res->target_absyn_ref = VAL_NONE;
96 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
97 if (!strcmp(argv[0], "targetref"))
101 logf(LOG_WARN, "%s:%d: Bad # args for targetref",
105 if ((res->target_absyn_ref = oid_getvalbyname(argv[1]))
108 logf(LOG_WARN, "%s:%d: Unknown reference '%s'",
109 file, lineno, argv[1]);
113 else if (!strcmp(argv[0], "targetname"))
117 logf(LOG_WARN, "%s:%d: Bad # args for targetname",
121 res->target_absyn_name =
122 (char *)nmem_malloc(mem, strlen(argv[1])+1);
123 strcpy(res->target_absyn_name, argv[1]);
125 else if (!yaz_matchstr(argv[0], "localnumeric"))
127 else if (!strcmp(argv[0], "name"))
131 logf(LOG_WARN, "%s:%d: Bad # args for name", file, lineno);
134 res->name = (char *)nmem_malloc(mem, strlen(argv[1])+1);
135 strcpy(res->name, argv[1]);
137 else if (!strcmp(argv[0], "map"))
140 char *ep, *path = argv[2];
144 logf(LOG_WARN, "%s:%d: Bad # of args for map",
148 *mapp = (data1_mapunit *)nmem_malloc(mem, sizeof(**mapp));
150 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
151 (*mapp)->no_data = 1;
153 (*mapp)->no_data = 0;
154 (*mapp)->source_element_name =
155 (char *)nmem_malloc(mem, strlen(argv[1])+1);
156 strcpy((*mapp)->source_element_name, argv[1]);
157 mtp = &(*mapp)->target_path;
160 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
161 (ep = strchr(path, '/'))))
164 char valstr[512], parm[512];
168 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
171 logf(LOG_WARN, "%s:%d: Syntax error in map directive: %s",
172 file, lineno, argv[2]);
176 *mtp = (data1_maptag *)nmem_malloc(mem, sizeof(**mtp));
179 if (np > 2 && !data1_matchstr(parm, "new"))
180 (*mtp)->new_field = 1;
182 (*mtp)->new_field = 0;
183 if ((type != 3 || local_numeric) && isdigit(*valstr))
185 (*mtp)->which = D1_MAPTAG_numeric;
186 (*mtp)->value.numeric = atoi(valstr);
190 (*mtp)->which = D1_MAPTAG_string;
191 (*mtp)->value.string =
192 (char *)nmem_malloc(mem, strlen(valstr)+1);
193 strcpy((*mtp)->value.string, valstr);
197 mapp = &(*mapp)->next;
200 logf(LOG_WARN, "%s:%d: Unknown directive '%s'",
201 file, lineno, argv[0]);
208 * Locate node with given elementname.
209 * NOTE: This is stupid - we don't find repeats this way.
211 static data1_node *find_node(data1_node *p, char *elementname)
215 for (c = p->child; c; c = c->next)
216 if (c->which == DATA1N_tag && c->u.tag.element &&
217 !data1_matchstr(c->u.tag.element->name, elementname))
219 else if ((r = find_node(c, elementname)))
225 * See if the node n is equivalent to the tag t.
227 static int tagmatch(data1_node *n, data1_maptag *t)
229 if (n->which != DATA1N_tag)
231 if (n->u.tag.element)
233 if (n->u.tag.element->tag->tagset)
235 if (n->u.tag.element->tag->tagset->type != t->type)
238 else if (t->type != 3)
240 if (n->u.tag.element->tag->which == DATA1T_numeric)
242 if (t->which != D1_MAPTAG_numeric)
244 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
249 if (t->which != D1_MAPTAG_string)
251 if (data1_matchstr(n->u.tag.element->tag->value.string,
262 if (t->which == D1_MAPTAG_numeric)
263 sprintf(str, "%d", t->value.numeric);
265 strcpy(str, t->value.string);
266 if (data1_matchstr(n->u.tag.tag, str))
272 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
273 data1_node *res, NMEM mem)
278 * locate each source element in turn.
280 for (c = n->child; c; c = c->next)
281 if (c->which == DATA1N_tag && c->u.tag.element)
283 for (m = map->map; m; m = m->next)
285 if (!data1_matchstr(m->source_element_name,
286 c->u.tag.element->name))
288 data1_node *pn = res;
289 data1_node *cur = pn->last_child;
293 * process the target path specification.
295 for (mt = m->target_path; mt; mt = mt->next)
297 if (!cur || mt->new_field || !tagmatch(cur, mt))
299 cur = data1_mk_node (dh, mem);
300 cur->which = DATA1N_tag;
301 cur->u.tag.element = 0;
302 cur->u.tag.tag = mt->value.string;
303 cur->u.tag.node_selected = 0;
305 cur->root = pn->root;
309 pn->last_child->next = cur;
310 pn->last_child = cur;
315 else if (!m->no_data)
317 cur->child = c->child;
318 cur->last_child = c->last_child;
326 if (map_children(dh, c, map, res, mem) < 0)
333 * Create a (possibly lossy) copy of the given record based on the
334 * table. The new copy will refer back to the data of the original record,
335 * which should not be discarded during the lifetime of the copy.
337 data1_node *data1_map_record (data1_handle dh, data1_node *n,
338 data1_maptab *map, NMEM m)
340 data1_node *res = data1_mk_node(dh, m);
342 res->which = DATA1N_root;
343 res->u.root.type = map->target_absyn_name;
344 if (!(res->u.root.absyn = data1_get_absyn(dh, map->target_absyn_name)))
346 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
347 map->name, map->target_absyn_name);
352 if (map_children(dh, n, map, res, m) < 0)
354 data1_free_tree(dh, res);