2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1997-10-31 12:20:09 adam
8 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
9 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
10 * Bug fix: missing fclose in data1_read_espec1.
12 * Revision 1.11 1997/09/29 13:18:59 adam
13 * Added function, oid_ent_to_oid, to replace the function
14 * oid_getoidbyent, which is not thread safe.
16 * Revision 1.10 1997/09/29 07:21:10 adam
17 * Added typecast to avoid warnings on MSVC.
19 * Revision 1.9 1997/09/17 12:10:35 adam
22 * Revision 1.8 1997/09/05 09:50:56 adam
23 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
25 * Revision 1.7 1997/05/14 06:54:02 adam
28 * Revision 1.6 1996/07/06 19:58:34 quinn
29 * System headerfiles gathered in yconfig
31 * Revision 1.5 1996/01/02 08:57:44 quinn
32 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
34 * Revision 1.4 1995/12/05 11:16:10 quinn
37 * Revision 1.3 1995/11/13 09:27:34 quinn
38 * Fiddling with the variant stuff.
40 * Revision 1.2 1995/11/01 16:34:56 quinn
41 * Making data1 look for tables in data1_tabpath
43 * Revision 1.1 1995/11/01 11:56:07 quinn
44 * Added Retrieval (data management) functions en masse.
61 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem)
63 Z_Variant *r = nmem_malloc(nmem, sizeof(*r));
68 var1.proto = PROTO_Z3950;
69 var1.oclass = CLASS_VARSET;
70 var1.value = VAL_VAR1;
71 r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
74 r->triples = nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
77 r->num_triples = argc;
78 for (i = 0; i < argc; i++)
84 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
86 logf(LOG_WARN, "Syntax error in variant component '%s'",
90 t = r->triples[i] = nmem_malloc(nmem, sizeof(Z_Triple));
92 t->zclass = nmem_malloc(nmem, sizeof(int));
94 t->type = nmem_malloc(nmem, sizeof(int));
97 * This is wrong.. we gotta look up the correct type for the
98 * variant, I guess... damn this stuff.
102 t->which = Z_Triple_null;
103 t->value.null = ODR_NULLVAL;
105 else if (isdigit(*value))
107 t->which = Z_Triple_integer;
108 t->value.integer = nmem_malloc(nmem, sizeof(*t->value.integer));
109 *t->value.integer = atoi(value);
113 t->which = Z_Triple_internationalString;
114 t->value.internationalString = nmem_malloc(nmem, strlen(value)+1);
115 strcpy(t->value.internationalString, value);
121 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem)
123 Z_Occurrences *op = nmem_malloc(nmem, sizeof(*op));
128 op->which = Z_Occurrences_values;
129 op->u.values = nmem_malloc(nmem, sizeof(Z_OccurValues));
130 op->u.values->start = nmem_malloc(nmem, sizeof(int));
131 *op->u.values->start = 1;
132 op->u.values->howMany = 0;
134 else if (!strcmp(occ, "all"))
136 op->which = Z_Occurrences_all;
137 op->u.all = ODR_NULLVAL;
139 else if (!strcmp(occ, "last"))
141 op->which = Z_Occurrences_last;
142 op->u.all = ODR_NULLVAL;
146 Z_OccurValues *ov = nmem_malloc(nmem, sizeof(*ov));
150 logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
153 op->which = Z_Occurrences_values;
155 ov->start = nmem_malloc(nmem, sizeof(*ov->start));
156 *ov->start = atoi(occ);
157 if ((p = strchr(occ, '+')))
159 ov->howMany = nmem_malloc(nmem, sizeof(*ov->howMany));
160 *ov->howMany = atoi(p + 1);
169 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem)
171 Z_ETagUnit *u = nmem_malloc(nmem, sizeof(*u));
174 char value[512], occ[512];
178 u->which = Z_ETagUnit_wildPath;
179 u->u.wildPath = ODR_NULLVAL;
181 else if (*buf == '?')
183 u->which = Z_ETagUnit_wildThing;
185 u->u.wildThing = read_occurrences(buf+2, nmem);
187 u->u.wildThing = read_occurrences(0, nmem);
189 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
195 int force_string = 0;
202 u->which = Z_ETagUnit_specificTag;
203 u->u.specificTag = t = nmem_malloc(nmem, sizeof(*t));
204 t->tagType = nmem_malloc(nmem, sizeof(*t->tagType));
206 t->tagValue = nmem_malloc(nmem, sizeof(*t->tagValue));
207 if (!force_string && (numval = atoi(valp)))
209 t->tagValue->which = Z_StringOrNumeric_numeric;
210 t->tagValue->u.numeric = nmem_malloc(nmem, sizeof(int));
211 *t->tagValue->u.numeric = numval;
215 t->tagValue->which = Z_StringOrNumeric_string;
216 t->tagValue->u.string = nmem_malloc(nmem, strlen(valp)+1);
217 strcpy(t->tagValue->u.string, valp);
219 if (terms > 2) /* an occurrences-spec exists */
220 t->occurrences = read_occurrences(occ, nmem);
228 * Read an element-set specification from a file.
229 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
231 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
234 NMEM nmem = data1_nmem_get (dh);
235 int argc, size_esn = 0;
236 char *argv[50], line[512];
237 Z_Espec1 *res = nmem_malloc(nmem, sizeof(*res));
239 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
241 logf(LOG_WARN|LOG_ERRNO, "%s", file);
245 res->num_elementSetNames = 0;
246 res->elementSetNames = 0;
247 res->defaultVariantSetId = 0;
248 res->defaultVariantRequest = 0;
249 res->defaultTagType = 0;
250 res->num_elements = 0;
253 while ((argc = readconf_line(f, line, 512, argv, 50)))
254 if (!strcmp(argv[0], "elementsetnames"))
256 int nnames = argc-1, i;
260 logf(LOG_WARN, "%s: Empty elementsetnames directive",
265 res->elementSetNames = nmem_malloc(nmem, sizeof(char**)*nnames);
266 for (i = 0; i < nnames; i++)
268 res->elementSetNames[i] = nmem_malloc(nmem,
269 strlen(argv[i+1])+1);
270 strcpy(res->elementSetNames[i], argv[i+1]);
272 res->num_elementSetNames = nnames;
274 else if (!strcmp(argv[0], "defaultvariantsetid"))
276 if (argc != 2 || !(res->defaultVariantSetId =
277 odr_getoidbystr_nmem(nmem, argv[1])))
279 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
283 else if (!strcmp(argv[0], "defaulttagtype"))
287 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
290 res->defaultTagType = nmem_malloc(nmem, sizeof(int));
291 *res->defaultTagType = atoi(argv[1]);
293 else if (!strcmp(argv[0], "defaultvariantrequest"))
295 if (!(res->defaultVariantRequest = read_variant(argc-1,
298 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
302 else if (!strcmp(argv[0], "simpleelement"))
304 Z_ElementRequest *er;
307 char *path = argv[1];
312 res->elements = nmem_malloc(nmem, size_esn = 24*sizeof(er));
313 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
315 Z_ElementRequest **oe = res->elements;
317 res->elements = nmem_malloc (nmem, size_esn*sizeof(er));
318 memcpy (res->elements, oe, size_esn/2);
322 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
326 res->elements[res->num_elements++] = er =
327 nmem_malloc(nmem, sizeof(*er));
328 er->which = Z_ERequest_simpleElement;
329 er->u.simpleElement = se = nmem_malloc(nmem, sizeof(*se));
330 se->variantRequest = 0;
331 se->path = tp = nmem_malloc(nmem, sizeof(*tp));
334 * Parse the element selector.
336 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
338 tp->tags = nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
340 for ((ep = strchr(path, '/')) ; path ;
341 (void)((path = ep) && (ep = strchr(path, '/'))))
347 tp->tags[tp->num_tags++] = read_tagunit(path, nmem);
350 if (argc > 2 && !strcmp(argv[2], "variant"))
351 se->variantRequest= read_variant(argc-3, argv+3, nmem);
355 logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);