2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1997-09-05 09:50:56 adam
8 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
10 * Revision 1.7 1997/05/14 06:54:02 adam
13 * Revision 1.6 1996/07/06 19:58:34 quinn
14 * System headerfiles gathered in yconfig
16 * Revision 1.5 1996/01/02 08:57:44 quinn
17 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
19 * Revision 1.4 1995/12/05 11:16:10 quinn
22 * Revision 1.3 1995/11/13 09:27:34 quinn
23 * Fiddling with the variant stuff.
25 * Revision 1.2 1995/11/01 16:34:56 quinn
26 * Making data1 look for tables in data1_tabpath
28 * Revision 1.1 1995/11/01 11:56:07 quinn
29 * Added Retrieval (data management) functions en masse.
47 static Z_Variant *read_variant(int argc, char **argv, ODR o)
49 Z_Variant *r = odr_malloc(o, sizeof(*r));
53 var1.proto = PROTO_Z3950;
54 var1.oclass = CLASS_VARSET;
55 var1.value = VAL_VAR1;
56 r->globalVariantSetId = odr_oiddup(o, oid_getoidbyent(&var1));
59 r->triples = odr_malloc(o, sizeof(Z_Triple*) * argc);
62 r->num_triples = argc;
63 for (i = 0; i < argc; i++)
69 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
71 logf(LOG_WARN, "Syntax error in variant component '%s'",
75 t = r->triples[i] = odr_malloc(o, sizeof(Z_Triple));
77 t->zclass = odr_malloc(o, sizeof(int));
79 t->type = odr_malloc(o, sizeof(int));
82 * This is wrong.. we gotta look up the correct type for the
83 * variant, I guess... damn this stuff.
87 t->which = Z_Triple_null;
88 t->value.null = ODR_NULLVAL;
90 else if (isdigit(*value))
92 t->which = Z_Triple_integer;
93 t->value.integer = odr_malloc(o, sizeof(*t->value.integer));
94 *t->value.integer = atoi(value);
98 t->which = Z_Triple_internationalString;
99 t->value.internationalString = odr_malloc(o, strlen(value)+1);
100 strcpy(t->value.internationalString, value);
106 static Z_Occurrences *read_occurrences(char *occ, ODR o)
108 Z_Occurrences *op = odr_malloc(o, sizeof(*op));
113 op->which = Z_Occurrences_values;
114 op->u.values = odr_malloc(o, sizeof(Z_OccurValues));
115 op->u.values->start = odr_malloc(o, sizeof(int));
116 *op->u.values->start = 1;
117 op->u.values->howMany = 0;
119 else if (!strcmp(occ, "all"))
121 op->which = Z_Occurrences_all;
122 op->u.all = ODR_NULLVAL;
124 else if (!strcmp(occ, "last"))
126 op->which = Z_Occurrences_last;
127 op->u.all = ODR_NULLVAL;
131 Z_OccurValues *ov = odr_malloc(o, sizeof(*ov));
135 logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
138 op->which = Z_Occurrences_values;
140 ov->start = odr_malloc(o, sizeof(*ov->start));
141 *ov->start = atoi(occ);
142 if ((p = strchr(occ, '+')))
144 ov->howMany = odr_malloc(o, sizeof(*ov->howMany));
145 *ov->howMany = atoi(p + 1);
154 static Z_ETagUnit *read_tagunit(char *buf, ODR o)
156 Z_ETagUnit *u = odr_malloc(o, sizeof(*u));
159 char value[512], occ[512];
163 u->which = Z_ETagUnit_wildPath;
164 u->u.wildPath = ODR_NULLVAL;
166 else if (*buf == '?')
168 u->which = Z_ETagUnit_wildThing;
170 u->u.wildThing = read_occurrences(buf+2, o);
172 u->u.wildThing = read_occurrences(0, o);
174 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
180 int force_string = 0;
187 u->which = Z_ETagUnit_specificTag;
188 u->u.specificTag = t = odr_malloc(o, sizeof(*t));
189 t->tagType = odr_malloc(o, sizeof(*t->tagType));
191 t->tagValue = odr_malloc(o, sizeof(*t->tagValue));
192 if (!force_string && (numval = atoi(valp)))
194 t->tagValue->which = Z_StringOrNumeric_numeric;
195 t->tagValue->u.numeric = odr_malloc(o, sizeof(int));
196 *t->tagValue->u.numeric = numval;
200 t->tagValue->which = Z_StringOrNumeric_string;
201 t->tagValue->u.string = odr_malloc(o, strlen(valp)+1);
202 strcpy(t->tagValue->u.string, valp);
204 if (terms > 2) /* an occurrences-spec exists */
205 t->occurrences = read_occurrences(occ, o);
213 * Read an element-set specification from a file.
214 * NOTE: If !o, memory is allocated directly from the heap by odr_malloc().
216 Z_Espec1 *data1_read_espec1(char *file, ODR o)
219 int argc, size_esn = 0;
220 char *argv[50], line[512];
221 Z_Espec1 *res = odr_malloc(o, sizeof(*res));
223 if (!(f = yaz_path_fopen(data1_get_tabpath(), file, "r")))
225 logf(LOG_WARN|LOG_ERRNO, "%s", file);
229 res->num_elementSetNames = 0;
230 res->elementSetNames = 0;
231 res->defaultVariantSetId = 0;
232 res->defaultVariantRequest = 0;
233 res->defaultTagType = 0;
234 res->num_elements = 0;
237 while ((argc = readconf_line(f, line, 512, argv, 50)))
238 if (!strcmp(argv[0], "elementsetnames"))
240 int nnames = argc-1, i;
244 logf(LOG_WARN, "%s: Empty elementsetnames directive",
249 res->elementSetNames = odr_malloc(o, sizeof(char**)*nnames);
250 for (i = 0; i < nnames; i++)
252 res->elementSetNames[i] = odr_malloc(o, strlen(argv[i+1])+1);
253 strcpy(res->elementSetNames[i], argv[i+1]);
255 res->num_elementSetNames = nnames;
257 else if (!strcmp(argv[0], "defaultvariantsetid"))
259 if (argc != 2 || !(res->defaultVariantSetId =
260 odr_getoidbystr(o, argv[1])))
262 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
266 else if (!strcmp(argv[0], "defaulttagtype"))
270 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
273 res->defaultTagType = odr_malloc(o, sizeof(int));
274 *res->defaultTagType = atoi(argv[1]);
276 else if (!strcmp(argv[0], "defaultvariantrequest"))
278 if (!(res->defaultVariantRequest = read_variant(argc-1, argv+1, o)))
280 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
284 else if (!strcmp(argv[0], "simpleelement"))
286 Z_ElementRequest *er;
289 char *path = argv[1];
294 res->elements = odr_malloc(o, size_esn = 24*sizeof(er));
295 else if (res->num_elements >= size_esn/sizeof(er))
300 Z_ElementRequest **oe = res->elements;
302 res->elements = odr_malloc (o, size_esn*sizeof(er));
303 memcpy (res->elements, oe, size_esn/2);
307 xrealloc(res->elements, size_esn*sizeof(er));
311 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
315 res->elements[res->num_elements++] = er =
316 odr_malloc(o, sizeof(*er));
317 er->which = Z_ERequest_simpleElement;
318 er->u.simpleElement = se = odr_malloc(o, sizeof(*se));
319 se->variantRequest = 0;
320 se->path = tp = odr_malloc(o, sizeof(*tp));
323 * Parse the element selector.
325 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
327 tp->tags = odr_malloc(o, sizeof(Z_ETagUnit*)*num);
329 for ((ep = strchr(path, '/')) ; path ;
330 (void)((path = ep) && (ep = strchr(path, '/'))))
336 tp->tags[tp->num_tags++] = read_tagunit(path, o);
339 if (argc > 2 && !strcmp(argv[2], "variant"))
340 se->variantRequest= read_variant(argc-3, argv+3, o);
344 logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);