2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-11-01 11:56:08 quinn
8 * Added Retrieval (data management) functions en masse.
25 data1_maptab *data1_read_maptab(char *file)
27 data1_maptab *res = xmalloc(sizeof(*res));
30 char *argv[50], line[512];
33 if (!(f = fopen(file, "r")))
35 logf(LOG_WARN|LOG_ERRNO, "%s", file);
40 res->target_absyn_ref = ODR_NONE;
45 while ((argc = readconf_line(f, line, 512, argv, 50)))
46 if (!strcmp(argv[0], "targetref"))
50 logf(LOG_WARN, "%s: one argument required for targetref",
54 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
56 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
60 else if (!strcmp(argv[0], "targetname"))
64 logf(LOG_WARN, "%s: one argument required for targetref",
68 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
69 strcpy(res->target_absyn_name, argv[1]);
71 else if (!strcmp(argv[0], "name"))
75 logf(LOG_WARN, "%s: one argument required for name",
79 res->name = xmalloc(strlen(argv[1])+1);
80 strcpy(res->name, argv[1]);
82 else if (!strcmp(argv[0], "map"))
85 char *ep, *path = argv[2];
89 logf(LOG_WARN, "%s: At least 2 arguments required for map",
93 *mapp = xmalloc(sizeof(**mapp));
95 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
99 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
100 strcpy((*mapp)->source_element_name, argv[1]);
101 mtp = &(*mapp)->target_path;
104 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
105 (ep = strchr(path, '/'))))
108 char valstr[512], parm[512];
113 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
116 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
121 *mtp = xmalloc(sizeof(**mtp));
124 if (np > 2 && !data1_matchstr(parm, "new"))
125 (*mtp)->new_field = 1;
127 (*mtp)->new_field = 0;
129 if ((numval = atoi(valstr)))
131 (*mtp)->which = D1_MAPTAG_numeric;
132 (*mtp)->value.numeric = numval;
137 (*mtp)->which = D1_MAPTAG_string;
138 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
139 strcpy((*mtp)->value.string, valstr);
145 mapp = &(*mapp)->next;
148 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
155 * Locate node with givel elementname.
156 * NOTE: This is stupid - we don't find repeats this way.
158 static data1_node *find_node(data1_node *p, char *elementname)
162 for (c = p->child; c; c = c->next)
163 if (c->which == DATA1N_tag && c->u.tag.element &&
164 !data1_matchstr(c->u.tag.element->name, elementname))
166 else if ((r = find_node(c, elementname)))
172 * See if the node n is equivalent to the tag t.
174 static int tagmatch(data1_node *n, data1_maptag *t)
176 if (n->which != DATA1N_tag)
178 if (n->u.tag.element)
180 if (n->u.tag.element->tag->tagset->type != t->type)
182 if (n->u.tag.element->tag->which == DATA1T_numeric)
184 if (t->which != D1_MAPTAG_numeric)
186 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
191 if (t->which != D1_MAPTAG_string)
193 if (data1_matchstr(n->u.tag.element->tag->value.string,
204 if (t->which == D1_MAPTAG_numeric)
205 sprintf(str, "%d", t->value.numeric);
207 strcpy(str, t->value.string);
208 if (data1_matchstr(n->u.tag.tag, str))
214 static int map_children(data1_node *n, data1_maptab *map, data1_node *res)
219 * locate each source element in turn.
221 for (c = n->child; c; c = c->next)
222 if (c->which == DATA1N_tag && c->u.tag.element)
224 for (m = map->map; m; m = m->next)
226 if (!data1_matchstr(m->source_element_name,
227 c->u.tag.element->name))
229 data1_node *pn = res;
234 * process the target path specification.
236 for (mt = m->target_path; mt; mt = mt->next)
242 for (l = pn->child, last = 0; l; last = l, l = l->next)
244 match = tagmatch(l, mt);
246 if (!tagmatch(l, mt))
248 if (!match || !mt->next || mt->new_field)
250 cur = data1_mk_node();
251 cur->which = DATA1N_tag;
252 cur->u.tag.element = 0;
253 cur->u.tag.tag = mt->value.string;
254 cur->u.tag.node_selected = 0;
256 cur->root = pn->root;
259 cur->next = pn->child;
264 cur->next = last->next;
270 cur = last ? last : pn->child;
274 else if (!m->no_data)
276 cur->child = c->child;
277 cur->num_children = c->num_children;
284 if (map_children(c, map, res) < 0)
291 * Create a (possibly lossy) copy of the given record based on the
292 * table. The new copy will refer back to the data of the original record,
293 * which should not be discarded during the lifetime of the copy.
295 data1_node *data1_map_record(data1_node *n, data1_maptab *map)
297 data1_node *res = data1_mk_node();
299 res->which = DATA1N_root;
300 res->u.root.type = map->target_absyn_name;
301 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
303 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
304 map->name, map->target_absyn_name);
309 if (map_children(n, map, res) < 0)
311 data1_free_tree(res);