2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1997-11-24 11:33:56 adam
8 * Using function odr_nullval() instead of global ODR_NULLVAL when
11 * Revision 1.12 1997/10/31 12:20:09 adam
12 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
13 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
14 * Bug fix: missing fclose in data1_read_espec1.
16 * Revision 1.11 1997/09/29 13:18:59 adam
17 * Added function, oid_ent_to_oid, to replace the function
18 * oid_getoidbyent, which is not thread safe.
20 * Revision 1.10 1997/09/29 07:21:10 adam
21 * Added typecast to avoid warnings on MSVC.
23 * Revision 1.9 1997/09/17 12:10:35 adam
26 * Revision 1.8 1997/09/05 09:50:56 adam
27 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
29 * Revision 1.7 1997/05/14 06:54:02 adam
32 * Revision 1.6 1996/07/06 19:58:34 quinn
33 * System headerfiles gathered in yconfig
35 * Revision 1.5 1996/01/02 08:57:44 quinn
36 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
38 * Revision 1.4 1995/12/05 11:16:10 quinn
41 * Revision 1.3 1995/11/13 09:27:34 quinn
42 * Fiddling with the variant stuff.
44 * Revision 1.2 1995/11/01 16:34:56 quinn
45 * Making data1 look for tables in data1_tabpath
47 * Revision 1.1 1995/11/01 11:56:07 quinn
48 * Added Retrieval (data management) functions en masse.
65 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem)
67 Z_Variant *r = nmem_malloc(nmem, sizeof(*r));
72 var1.proto = PROTO_Z3950;
73 var1.oclass = CLASS_VARSET;
74 var1.value = VAL_VAR1;
75 r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
78 r->triples = nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
81 r->num_triples = argc;
82 for (i = 0; i < argc; i++)
88 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
90 logf(LOG_WARN, "Syntax error in variant component '%s'",
94 t = r->triples[i] = nmem_malloc(nmem, sizeof(Z_Triple));
96 t->zclass = nmem_malloc(nmem, sizeof(int));
98 t->type = nmem_malloc(nmem, sizeof(int));
101 * This is wrong.. we gotta look up the correct type for the
102 * variant, I guess... damn this stuff.
106 t->which = Z_Triple_null;
107 t->value.null = odr_nullval();
109 else if (isdigit(*value))
111 t->which = Z_Triple_integer;
112 t->value.integer = nmem_malloc(nmem, sizeof(*t->value.integer));
113 *t->value.integer = atoi(value);
117 t->which = Z_Triple_internationalString;
118 t->value.internationalString = nmem_malloc(nmem, strlen(value)+1);
119 strcpy(t->value.internationalString, value);
125 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem)
127 Z_Occurrences *op = nmem_malloc(nmem, sizeof(*op));
132 op->which = Z_Occurrences_values;
133 op->u.values = nmem_malloc(nmem, sizeof(Z_OccurValues));
134 op->u.values->start = nmem_malloc(nmem, sizeof(int));
135 *op->u.values->start = 1;
136 op->u.values->howMany = 0;
138 else if (!strcmp(occ, "all"))
140 op->which = Z_Occurrences_all;
141 op->u.all = odr_nullval();
143 else if (!strcmp(occ, "last"))
145 op->which = Z_Occurrences_last;
146 op->u.all = odr_nullval();
150 Z_OccurValues *ov = nmem_malloc(nmem, sizeof(*ov));
154 logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
157 op->which = Z_Occurrences_values;
159 ov->start = nmem_malloc(nmem, sizeof(*ov->start));
160 *ov->start = atoi(occ);
161 if ((p = strchr(occ, '+')))
163 ov->howMany = nmem_malloc(nmem, sizeof(*ov->howMany));
164 *ov->howMany = atoi(p + 1);
173 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem)
175 Z_ETagUnit *u = nmem_malloc(nmem, sizeof(*u));
178 char value[512], occ[512];
182 u->which = Z_ETagUnit_wildPath;
183 u->u.wildPath = odr_nullval();
185 else if (*buf == '?')
187 u->which = Z_ETagUnit_wildThing;
189 u->u.wildThing = read_occurrences(buf+2, nmem);
191 u->u.wildThing = read_occurrences(0, nmem);
193 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
199 int force_string = 0;
206 u->which = Z_ETagUnit_specificTag;
207 u->u.specificTag = t = nmem_malloc(nmem, sizeof(*t));
208 t->tagType = nmem_malloc(nmem, sizeof(*t->tagType));
210 t->tagValue = nmem_malloc(nmem, sizeof(*t->tagValue));
211 if (!force_string && (numval = atoi(valp)))
213 t->tagValue->which = Z_StringOrNumeric_numeric;
214 t->tagValue->u.numeric = nmem_malloc(nmem, sizeof(int));
215 *t->tagValue->u.numeric = numval;
219 t->tagValue->which = Z_StringOrNumeric_string;
220 t->tagValue->u.string = nmem_malloc(nmem, strlen(valp)+1);
221 strcpy(t->tagValue->u.string, valp);
223 if (terms > 2) /* an occurrences-spec exists */
224 t->occurrences = read_occurrences(occ, nmem);
232 * Read an element-set specification from a file.
233 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
235 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
238 NMEM nmem = data1_nmem_get (dh);
239 int argc, size_esn = 0;
240 char *argv[50], line[512];
241 Z_Espec1 *res = nmem_malloc(nmem, sizeof(*res));
243 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
245 logf(LOG_WARN|LOG_ERRNO, "%s", file);
249 res->num_elementSetNames = 0;
250 res->elementSetNames = 0;
251 res->defaultVariantSetId = 0;
252 res->defaultVariantRequest = 0;
253 res->defaultTagType = 0;
254 res->num_elements = 0;
257 while ((argc = readconf_line(f, line, 512, argv, 50)))
258 if (!strcmp(argv[0], "elementsetnames"))
260 int nnames = argc-1, i;
264 logf(LOG_WARN, "%s: Empty elementsetnames directive",
269 res->elementSetNames = nmem_malloc(nmem, sizeof(char**)*nnames);
270 for (i = 0; i < nnames; i++)
272 res->elementSetNames[i] = nmem_malloc(nmem,
273 strlen(argv[i+1])+1);
274 strcpy(res->elementSetNames[i], argv[i+1]);
276 res->num_elementSetNames = nnames;
278 else if (!strcmp(argv[0], "defaultvariantsetid"))
280 if (argc != 2 || !(res->defaultVariantSetId =
281 odr_getoidbystr_nmem(nmem, argv[1])))
283 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
287 else if (!strcmp(argv[0], "defaulttagtype"))
291 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
294 res->defaultTagType = nmem_malloc(nmem, sizeof(int));
295 *res->defaultTagType = atoi(argv[1]);
297 else if (!strcmp(argv[0], "defaultvariantrequest"))
299 if (!(res->defaultVariantRequest = read_variant(argc-1,
302 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
306 else if (!strcmp(argv[0], "simpleelement"))
308 Z_ElementRequest *er;
311 char *path = argv[1];
316 res->elements = nmem_malloc(nmem, size_esn = 24*sizeof(er));
317 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
319 Z_ElementRequest **oe = res->elements;
321 res->elements = nmem_malloc (nmem, size_esn*sizeof(er));
322 memcpy (res->elements, oe, size_esn/2);
326 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
330 res->elements[res->num_elements++] = er =
331 nmem_malloc(nmem, sizeof(*er));
332 er->which = Z_ERequest_simpleElement;
333 er->u.simpleElement = se = nmem_malloc(nmem, sizeof(*se));
334 se->variantRequest = 0;
335 se->path = tp = nmem_malloc(nmem, sizeof(*tp));
338 * Parse the element selector.
340 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
342 tp->tags = nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
344 for ((ep = strchr(path, '/')) ; path ;
345 (void)((path = ep) && (ep = strchr(path, '/'))))
351 tp->tags[tp->num_tags++] = read_tagunit(path, nmem);
354 if (argc > 2 && !strcmp(argv[2], "variant"))
355 se->variantRequest= read_variant(argc-3, argv+3, nmem);
359 logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);