2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-09-29 07:21:10 adam
8 * Added typecast to avoid warnings on MSVC.
10 * Revision 1.9 1997/09/17 12:10:35 adam
13 * Revision 1.8 1997/09/05 09:50:56 adam
14 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
16 * Revision 1.7 1997/05/14 06:54:02 adam
19 * Revision 1.6 1996/07/06 19:58:34 quinn
20 * System headerfiles gathered in yconfig
22 * Revision 1.5 1996/01/02 08:57:44 quinn
23 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
25 * Revision 1.4 1995/12/05 11:16:10 quinn
28 * Revision 1.3 1995/11/13 09:27:34 quinn
29 * Fiddling with the variant stuff.
31 * Revision 1.2 1995/11/01 16:34:56 quinn
32 * Making data1 look for tables in data1_tabpath
34 * Revision 1.1 1995/11/01 11:56:07 quinn
35 * Added Retrieval (data management) functions en masse.
53 static Z_Variant *read_variant(int argc, char **argv, ODR o)
55 Z_Variant *r = odr_malloc(o, sizeof(*r));
59 var1.proto = PROTO_Z3950;
60 var1.oclass = CLASS_VARSET;
61 var1.value = VAL_VAR1;
62 r->globalVariantSetId = odr_oiddup(o, oid_getoidbyent(&var1));
65 r->triples = odr_malloc(o, sizeof(Z_Triple*) * argc);
68 r->num_triples = argc;
69 for (i = 0; i < argc; i++)
75 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
77 logf(LOG_WARN, "Syntax error in variant component '%s'",
81 t = r->triples[i] = odr_malloc(o, sizeof(Z_Triple));
83 t->zclass = odr_malloc(o, sizeof(int));
85 t->type = odr_malloc(o, sizeof(int));
88 * This is wrong.. we gotta look up the correct type for the
89 * variant, I guess... damn this stuff.
93 t->which = Z_Triple_null;
94 t->value.null = ODR_NULLVAL;
96 else if (isdigit(*value))
98 t->which = Z_Triple_integer;
99 t->value.integer = odr_malloc(o, sizeof(*t->value.integer));
100 *t->value.integer = atoi(value);
104 t->which = Z_Triple_internationalString;
105 t->value.internationalString = odr_malloc(o, strlen(value)+1);
106 strcpy(t->value.internationalString, value);
112 static Z_Occurrences *read_occurrences(char *occ, ODR o)
114 Z_Occurrences *op = odr_malloc(o, sizeof(*op));
119 op->which = Z_Occurrences_values;
120 op->u.values = odr_malloc(o, sizeof(Z_OccurValues));
121 op->u.values->start = odr_malloc(o, sizeof(int));
122 *op->u.values->start = 1;
123 op->u.values->howMany = 0;
125 else if (!strcmp(occ, "all"))
127 op->which = Z_Occurrences_all;
128 op->u.all = ODR_NULLVAL;
130 else if (!strcmp(occ, "last"))
132 op->which = Z_Occurrences_last;
133 op->u.all = ODR_NULLVAL;
137 Z_OccurValues *ov = odr_malloc(o, sizeof(*ov));
141 logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
144 op->which = Z_Occurrences_values;
146 ov->start = odr_malloc(o, sizeof(*ov->start));
147 *ov->start = atoi(occ);
148 if ((p = strchr(occ, '+')))
150 ov->howMany = odr_malloc(o, sizeof(*ov->howMany));
151 *ov->howMany = atoi(p + 1);
160 static Z_ETagUnit *read_tagunit(char *buf, ODR o)
162 Z_ETagUnit *u = odr_malloc(o, sizeof(*u));
165 char value[512], occ[512];
169 u->which = Z_ETagUnit_wildPath;
170 u->u.wildPath = ODR_NULLVAL;
172 else if (*buf == '?')
174 u->which = Z_ETagUnit_wildThing;
176 u->u.wildThing = read_occurrences(buf+2, o);
178 u->u.wildThing = read_occurrences(0, o);
180 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
186 int force_string = 0;
193 u->which = Z_ETagUnit_specificTag;
194 u->u.specificTag = t = odr_malloc(o, sizeof(*t));
195 t->tagType = odr_malloc(o, sizeof(*t->tagType));
197 t->tagValue = odr_malloc(o, sizeof(*t->tagValue));
198 if (!force_string && (numval = atoi(valp)))
200 t->tagValue->which = Z_StringOrNumeric_numeric;
201 t->tagValue->u.numeric = odr_malloc(o, sizeof(int));
202 *t->tagValue->u.numeric = numval;
206 t->tagValue->which = Z_StringOrNumeric_string;
207 t->tagValue->u.string = odr_malloc(o, strlen(valp)+1);
208 strcpy(t->tagValue->u.string, valp);
210 if (terms > 2) /* an occurrences-spec exists */
211 t->occurrences = read_occurrences(occ, o);
219 * Read an element-set specification from a file.
220 * NOTE: If !o, memory is allocated directly from the heap by odr_malloc().
222 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file, ODR o)
225 int argc, size_esn = 0;
226 char *argv[50], line[512];
227 Z_Espec1 *res = odr_malloc(o, sizeof(*res));
229 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
231 logf(LOG_WARN|LOG_ERRNO, "%s", file);
235 res->num_elementSetNames = 0;
236 res->elementSetNames = 0;
237 res->defaultVariantSetId = 0;
238 res->defaultVariantRequest = 0;
239 res->defaultTagType = 0;
240 res->num_elements = 0;
243 while ((argc = readconf_line(f, line, 512, argv, 50)))
244 if (!strcmp(argv[0], "elementsetnames"))
246 int nnames = argc-1, i;
250 logf(LOG_WARN, "%s: Empty elementsetnames directive",
255 res->elementSetNames = odr_malloc(o, sizeof(char**)*nnames);
256 for (i = 0; i < nnames; i++)
258 res->elementSetNames[i] = odr_malloc(o, strlen(argv[i+1])+1);
259 strcpy(res->elementSetNames[i], argv[i+1]);
261 res->num_elementSetNames = nnames;
263 else if (!strcmp(argv[0], "defaultvariantsetid"))
265 if (argc != 2 || !(res->defaultVariantSetId =
266 odr_getoidbystr(o, argv[1])))
268 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
272 else if (!strcmp(argv[0], "defaulttagtype"))
276 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
279 res->defaultTagType = odr_malloc(o, sizeof(int));
280 *res->defaultTagType = atoi(argv[1]);
282 else if (!strcmp(argv[0], "defaultvariantrequest"))
284 if (!(res->defaultVariantRequest = read_variant(argc-1, argv+1, o)))
286 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
290 else if (!strcmp(argv[0], "simpleelement"))
292 Z_ElementRequest *er;
295 char *path = argv[1];
300 res->elements = odr_malloc(o, size_esn = 24*sizeof(er));
301 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
306 Z_ElementRequest **oe = res->elements;
308 res->elements = odr_malloc (o, size_esn*sizeof(er));
309 memcpy (res->elements, oe, size_esn/2);
313 xrealloc(res->elements, size_esn*sizeof(er));
317 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
321 res->elements[res->num_elements++] = er =
322 odr_malloc(o, sizeof(*er));
323 er->which = Z_ERequest_simpleElement;
324 er->u.simpleElement = se = odr_malloc(o, sizeof(*se));
325 se->variantRequest = 0;
326 se->path = tp = odr_malloc(o, sizeof(*tp));
329 * Parse the element selector.
331 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
333 tp->tags = odr_malloc(o, sizeof(Z_ETagUnit*)*num);
335 for ((ep = strchr(path, '/')) ; path ;
336 (void)((path = ep) && (ep = strchr(path, '/'))))
342 tp->tags[tp->num_tags++] = read_tagunit(path, o);
345 if (argc > 2 && !strcmp(argv[2], "variant"))
346 se->variantRequest= read_variant(argc-3, argv+3, o);
350 logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);