2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-09-05 09:50:56 adam
8 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
10 * Revision 1.9 1996/06/10 08:56:02 quinn
13 * Revision 1.8 1996/05/01 12:45:31 quinn
14 * Support use of local tag names in abs file.
16 * Revision 1.7 1995/12/13 13:44:31 quinn
17 * Modified Data1-system to use nmem
19 * Revision 1.6 1995/12/12 16:37:08 quinn
20 * Added destroy element to data1_node.
22 * Revision 1.5 1995/12/12 14:11:31 quinn
23 * More work on the large-record problem.
25 * Revision 1.4 1995/12/11 15:22:37 quinn
26 * Added last_child field to the node.
27 * Rewrote schema-mapping.
29 * Revision 1.3 1995/11/01 16:34:56 quinn
30 * Making data1 look for tables in data1_tabpath
32 * Revision 1.2 1995/11/01 13:54:46 quinn
35 * Revision 1.1 1995/11/01 11:56:08 quinn
36 * Added Retrieval (data management) functions en masse.
55 data1_maptab *data1_read_maptab(char *file)
57 data1_maptab *res = xmalloc(sizeof(*res));
60 char *argv[50], line[512];
62 int local_numeric = 0;
64 if (!(f = yaz_path_fopen(data1_get_tabpath(), file, "r")))
66 logf(LOG_WARN|LOG_ERRNO, "%s", file);
71 res->target_absyn_ref = ODR_NONE;
76 while ((argc = readconf_line(f, line, 512, argv, 50)))
77 if (!strcmp(argv[0], "targetref"))
81 logf(LOG_WARN, "%s: one argument required for targetref",
85 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
87 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
91 else if (!strcmp(argv[0], "targetname"))
95 logf(LOG_WARN, "%s: one argument required for targetref",
99 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
100 strcpy(res->target_absyn_name, argv[1]);
102 else if (!yaz_matchstr(argv[0], "localnumeric"))
104 else if (!strcmp(argv[0], "name"))
108 logf(LOG_WARN, "%s: one argument required for name",
112 res->name = xmalloc(strlen(argv[1])+1);
113 strcpy(res->name, argv[1]);
115 else if (!strcmp(argv[0], "map"))
118 char *ep, *path = argv[2];
122 logf(LOG_WARN, "%s: At least 2 arguments required for map",
126 *mapp = xmalloc(sizeof(**mapp));
128 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
129 (*mapp)->no_data = 1;
131 (*mapp)->no_data = 0;
132 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
133 strcpy((*mapp)->source_element_name, argv[1]);
134 mtp = &(*mapp)->target_path;
137 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
138 (ep = strchr(path, '/'))))
141 char valstr[512], parm[512];
145 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
148 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
153 *mtp = xmalloc(sizeof(**mtp));
156 if (np > 2 && !data1_matchstr(parm, "new"))
157 (*mtp)->new_field = 1;
159 (*mtp)->new_field = 0;
160 if ((type != 3 || local_numeric) && isdigit(*valstr))
162 (*mtp)->which = D1_MAPTAG_numeric;
163 (*mtp)->value.numeric = atoi(valstr);
167 (*mtp)->which = D1_MAPTAG_string;
168 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
169 strcpy((*mtp)->value.string, valstr);
173 mapp = &(*mapp)->next;
176 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
183 * Locate node with given elementname.
184 * NOTE: This is stupid - we don't find repeats this way.
186 static data1_node *find_node(data1_node *p, char *elementname)
190 for (c = p->child; c; c = c->next)
191 if (c->which == DATA1N_tag && c->u.tag.element &&
192 !data1_matchstr(c->u.tag.element->name, elementname))
194 else if ((r = find_node(c, elementname)))
200 * See if the node n is equivalent to the tag t.
202 static int tagmatch(data1_node *n, data1_maptag *t)
204 if (n->which != DATA1N_tag)
206 if (n->u.tag.element)
208 if (n->u.tag.element->tag->tagset)
210 if (n->u.tag.element->tag->tagset->type != t->type)
213 else if (t->type != 3)
215 if (n->u.tag.element->tag->which == DATA1T_numeric)
217 if (t->which != D1_MAPTAG_numeric)
219 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
224 if (t->which != D1_MAPTAG_string)
226 if (data1_matchstr(n->u.tag.element->tag->value.string,
237 if (t->which == D1_MAPTAG_numeric)
238 sprintf(str, "%d", t->value.numeric);
240 strcpy(str, t->value.string);
241 if (data1_matchstr(n->u.tag.tag, str))
247 static int map_children(data1_node *n, data1_maptab *map, data1_node *res,
253 * locate each source element in turn.
255 for (c = n->child; c; c = c->next)
256 if (c->which == DATA1N_tag && c->u.tag.element)
258 for (m = map->map; m; m = m->next)
260 if (!data1_matchstr(m->source_element_name,
261 c->u.tag.element->name))
263 data1_node *pn = res;
264 data1_node *cur = pn->last_child;
268 * process the target path specification.
270 for (mt = m->target_path; mt; mt = mt->next)
272 if (!cur || mt->new_field || !tagmatch(cur, mt))
274 cur = data1_mk_node(mem);
275 cur->which = DATA1N_tag;
276 cur->u.tag.element = 0;
277 cur->u.tag.tag = mt->value.string;
278 cur->u.tag.node_selected = 0;
280 cur->root = pn->root;
284 pn->last_child->next = cur;
285 pn->last_child = cur;
291 else if (!m->no_data)
293 cur->child = c->child;
294 cur->last_child = c->last_child;
295 cur->num_children = c->num_children;
304 if (map_children(c, map, res, mem) < 0)
311 * Create a (possibly lossy) copy of the given record based on the
312 * table. The new copy will refer back to the data of the original record,
313 * which should not be discarded during the lifetime of the copy.
315 data1_node *data1_map_record(data1_node *n, data1_maptab *map, NMEM m)
317 data1_node *res = data1_mk_node(m);
319 res->which = DATA1N_root;
320 res->u.root.type = map->target_absyn_name;
321 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
323 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
324 map->name, map->target_absyn_name);
329 if (map_children(n, map, res, m) < 0)
331 data1_free_tree(res);