2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-11-01 13:54:46 quinn
10 * Revision 1.1 1995/11/01 11:56:08 quinn
11 * Added Retrieval (data management) functions en masse.
28 data1_maptab *data1_read_maptab(char *file)
30 data1_maptab *res = xmalloc(sizeof(*res));
33 char *argv[50], line[512];
36 if (!(f = fopen(file, "r")))
38 logf(LOG_WARN|LOG_ERRNO, "%s", file);
43 res->target_absyn_ref = ODR_NONE;
48 while ((argc = readconf_line(f, line, 512, argv, 50)))
49 if (!strcmp(argv[0], "targetref"))
53 logf(LOG_WARN, "%s: one argument required for targetref",
57 if ((res->target_absyn_ref = oid_getvalbyname(argv[1])) == ODR_NONE)
59 logf(LOG_WARN, "%s: Unknown reference '%s'", file, argv[1]);
63 else if (!strcmp(argv[0], "targetname"))
67 logf(LOG_WARN, "%s: one argument required for targetref",
71 res->target_absyn_name = xmalloc(strlen(argv[1])+1);
72 strcpy(res->target_absyn_name, argv[1]);
74 else if (!strcmp(argv[0], "name"))
78 logf(LOG_WARN, "%s: one argument required for name",
82 res->name = xmalloc(strlen(argv[1])+1);
83 strcpy(res->name, argv[1]);
85 else if (!strcmp(argv[0], "map"))
88 char *ep, *path = argv[2];
92 logf(LOG_WARN, "%s: At least 2 arguments required for map",
96 *mapp = xmalloc(sizeof(**mapp));
98 if (argc > 3 && !data1_matchstr(argv[3], "nodata"))
101 (*mapp)->no_data = 0;
102 (*mapp)->source_element_name = xmalloc(strlen(argv[1])+1);
103 strcpy((*mapp)->source_element_name, argv[1]);
104 mtp = &(*mapp)->target_path;
107 for (ep = strchr(path, '/'); path; (void)((path = ep) &&
108 (ep = strchr(path, '/'))))
111 char valstr[512], parm[512];
116 if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
119 logf(LOG_WARN, "%s: Syntax error in map directive: %s",
124 *mtp = xmalloc(sizeof(**mtp));
127 if (np > 2 && !data1_matchstr(parm, "new"))
128 (*mtp)->new_field = 1;
130 (*mtp)->new_field = 0;
132 if ((numval = atoi(valstr)))
134 (*mtp)->which = D1_MAPTAG_numeric;
135 (*mtp)->value.numeric = numval;
140 (*mtp)->which = D1_MAPTAG_string;
141 (*mtp)->value.string = xmalloc(strlen(valstr)+1);
142 strcpy((*mtp)->value.string, valstr);
148 mapp = &(*mapp)->next;
151 logf(LOG_WARN, "%s: Unknown directive '%s'", argv[0]);
158 * Locate node with givel elementname.
159 * NOTE: This is stupid - we don't find repeats this way.
161 static data1_node *find_node(data1_node *p, char *elementname)
165 for (c = p->child; c; c = c->next)
166 if (c->which == DATA1N_tag && c->u.tag.element &&
167 !data1_matchstr(c->u.tag.element->name, elementname))
169 else if ((r = find_node(c, elementname)))
175 * See if the node n is equivalent to the tag t.
177 static int tagmatch(data1_node *n, data1_maptag *t)
179 if (n->which != DATA1N_tag)
181 if (n->u.tag.element)
183 if (n->u.tag.element->tag->tagset->type != t->type)
185 if (n->u.tag.element->tag->which == DATA1T_numeric)
187 if (t->which != D1_MAPTAG_numeric)
189 if (n->u.tag.element->tag->value.numeric != t->value.numeric)
194 if (t->which != D1_MAPTAG_string)
196 if (data1_matchstr(n->u.tag.element->tag->value.string,
207 if (t->which == D1_MAPTAG_numeric)
208 sprintf(str, "%d", t->value.numeric);
210 strcpy(str, t->value.string);
211 if (data1_matchstr(n->u.tag.tag, str))
217 static int map_children(data1_node *n, data1_maptab *map, data1_node *res)
222 * locate each source element in turn.
224 for (c = n->child; c; c = c->next)
225 if (c->which == DATA1N_tag && c->u.tag.element)
227 for (m = map->map; m; m = m->next)
229 if (!data1_matchstr(m->source_element_name,
230 c->u.tag.element->name))
232 data1_node *pn = res;
237 * process the target path specification.
239 for (mt = m->target_path; mt; mt = mt->next)
245 for (l = pn->child, last = 0; l; last = l, l = l->next)
247 match = tagmatch(l, mt);
249 if (!tagmatch(l, mt))
251 if (!match || !mt->next || mt->new_field)
253 cur = data1_mk_node();
254 cur->which = DATA1N_tag;
255 cur->u.tag.element = 0;
256 cur->u.tag.tag = mt->value.string;
257 cur->u.tag.node_selected = 0;
259 cur->root = pn->root;
262 cur->next = pn->child;
267 cur->next = last->next;
273 cur = last ? last : pn->child;
277 else if (!m->no_data)
279 cur->child = c->child;
280 cur->num_children = c->num_children;
287 if (map_children(c, map, res) < 0)
294 * Create a (possibly lossy) copy of the given record based on the
295 * table. The new copy will refer back to the data of the original record,
296 * which should not be discarded during the lifetime of the copy.
298 data1_node *data1_map_record(data1_node *n, data1_maptab *map)
300 data1_node *res = data1_mk_node();
302 res->which = DATA1N_root;
303 res->u.root.type = map->target_absyn_name;
304 if (!(res->u.root.absyn = data1_get_absyn(map->target_absyn_name)))
306 logf(LOG_WARN, "%s: Failed to load target absyn '%s'",
307 map->name, map->target_absyn_name);
312 if (map_children(n, map, res) < 0)
314 data1_free_tree(res);