2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1998-02-11 11:53:35 adam
8 * Changed code so that it compiles as C++.
10 * Revision 1.13 1997/11/24 11:33:56 adam
11 * Using function odr_nullval() instead of global ODR_NULLVAL when
14 * Revision 1.12 1997/10/31 12:20:09 adam
15 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
16 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
17 * Bug fix: missing fclose in data1_read_espec1.
19 * Revision 1.11 1997/09/29 13:18:59 adam
20 * Added function, oid_ent_to_oid, to replace the function
21 * oid_getoidbyent, which is not thread safe.
23 * Revision 1.10 1997/09/29 07:21:10 adam
24 * Added typecast to avoid warnings on MSVC.
26 * Revision 1.9 1997/09/17 12:10:35 adam
29 * Revision 1.8 1997/09/05 09:50:56 adam
30 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
32 * Revision 1.7 1997/05/14 06:54:02 adam
35 * Revision 1.6 1996/07/06 19:58:34 quinn
36 * System headerfiles gathered in yconfig
38 * Revision 1.5 1996/01/02 08:57:44 quinn
39 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
41 * Revision 1.4 1995/12/05 11:16:10 quinn
44 * Revision 1.3 1995/11/13 09:27:34 quinn
45 * Fiddling with the variant stuff.
47 * Revision 1.2 1995/11/01 16:34:56 quinn
48 * Making data1 look for tables in data1_tabpath
50 * Revision 1.1 1995/11/01 11:56:07 quinn
51 * Added Retrieval (data management) functions en masse.
68 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem)
70 Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
75 var1.proto = PROTO_Z3950;
76 var1.oclass = CLASS_VARSET;
77 var1.value = VAL_VAR1;
78 r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
81 r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
84 r->num_triples = argc;
85 for (i = 0; i < argc; i++)
91 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
93 logf(LOG_WARN, "Syntax error in variant component '%s'",
97 t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
99 t->zclass = (int *)nmem_malloc(nmem, sizeof(int));
101 t->type = (int *)nmem_malloc(nmem, sizeof(int));
104 * This is wrong.. we gotta look up the correct type for the
105 * variant, I guess... damn this stuff.
109 t->which = Z_Triple_null;
110 t->value.null = odr_nullval();
112 else if (isdigit(*value))
114 t->which = Z_Triple_integer;
115 t->value.integer = (int *)nmem_malloc(nmem, sizeof(*t->value.integer));
116 *t->value.integer = atoi(value);
120 t->which = Z_Triple_internationalString;
121 t->value.internationalString = (char *)nmem_malloc(nmem, strlen(value)+1);
122 strcpy(t->value.internationalString, value);
128 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem)
130 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
135 op->which = Z_Occurrences_values;
136 op->u.values = (Z_OccurValues *)nmem_malloc(nmem, sizeof(Z_OccurValues));
137 op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
138 *op->u.values->start = 1;
139 op->u.values->howMany = 0;
141 else if (!strcmp(occ, "all"))
143 op->which = Z_Occurrences_all;
144 op->u.all = odr_nullval();
146 else if (!strcmp(occ, "last"))
148 op->which = Z_Occurrences_last;
149 op->u.all = odr_nullval();
153 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
157 logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
160 op->which = Z_Occurrences_values;
162 ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
163 *ov->start = atoi(occ);
164 if ((p = strchr(occ, '+')))
166 ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
167 *ov->howMany = atoi(p + 1);
176 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem)
178 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
181 char value[512], occ[512];
185 u->which = Z_ETagUnit_wildPath;
186 u->u.wildPath = odr_nullval();
188 else if (*buf == '?')
190 u->which = Z_ETagUnit_wildThing;
192 u->u.wildThing = read_occurrences(buf+2, nmem);
194 u->u.wildThing = read_occurrences(0, nmem);
196 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
202 int force_string = 0;
209 u->which = Z_ETagUnit_specificTag;
210 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
211 t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
213 t->tagValue = (Z_StringOrNumeric *)nmem_malloc(nmem, sizeof(*t->tagValue));
214 if (!force_string && (numval = atoi(valp)))
216 t->tagValue->which = Z_StringOrNumeric_numeric;
217 t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
218 *t->tagValue->u.numeric = numval;
222 t->tagValue->which = Z_StringOrNumeric_string;
223 t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
224 strcpy(t->tagValue->u.string, valp);
226 if (terms > 2) /* an occurrences-spec exists */
227 t->occurrences = read_occurrences(occ, nmem);
235 * Read an element-set specification from a file.
236 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
238 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
241 NMEM nmem = data1_nmem_get (dh);
242 int argc, size_esn = 0;
243 char *argv[50], line[512];
244 Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
246 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
248 logf(LOG_WARN|LOG_ERRNO, "%s", file);
252 res->num_elementSetNames = 0;
253 res->elementSetNames = 0;
254 res->defaultVariantSetId = 0;
255 res->defaultVariantRequest = 0;
256 res->defaultTagType = 0;
257 res->num_elements = 0;
260 while ((argc = readconf_line(f, line, 512, argv, 50)))
261 if (!strcmp(argv[0], "elementsetnames"))
263 int nnames = argc-1, i;
267 logf(LOG_WARN, "%s: Empty elementsetnames directive",
272 res->elementSetNames = (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
273 for (i = 0; i < nnames; i++)
275 res->elementSetNames[i] = (char *)nmem_malloc(nmem,
276 strlen(argv[i+1])+1);
277 strcpy(res->elementSetNames[i], argv[i+1]);
279 res->num_elementSetNames = nnames;
281 else if (!strcmp(argv[0], "defaultvariantsetid"))
283 if (argc != 2 || !(res->defaultVariantSetId =
284 odr_getoidbystr_nmem(nmem, argv[1])))
286 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
290 else if (!strcmp(argv[0], "defaulttagtype"))
294 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
297 res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
298 *res->defaultTagType = atoi(argv[1]);
300 else if (!strcmp(argv[0], "defaultvariantrequest"))
302 if (!(res->defaultVariantRequest = read_variant(argc-1,
305 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
309 else if (!strcmp(argv[0], "simpleelement"))
311 Z_ElementRequest *er;
314 char *path = argv[1];
319 res->elements = (Z_ElementRequest **)nmem_malloc(nmem, size_esn = 24*sizeof(er));
320 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
322 Z_ElementRequest **oe = res->elements;
324 res->elements = (Z_ElementRequest **)nmem_malloc (nmem, size_esn*sizeof(er));
325 memcpy (res->elements, oe, size_esn/2);
329 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
333 res->elements[res->num_elements++] = er =
334 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
335 er->which = Z_ERequest_simpleElement;
336 er->u.simpleElement = se = (Z_SimpleElement *)nmem_malloc(nmem, sizeof(*se));
337 se->variantRequest = 0;
338 se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
341 * Parse the element selector.
343 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
345 tp->tags = (Z_ETagUnit **)nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
347 for ((ep = strchr(path, '/')) ; path ;
348 (void)((path = ep) && (ep = strchr(path, '/'))))
354 tp->tags[tp->num_tags++] = read_tagunit(path, nmem);
357 if (argc > 2 && !strcmp(argv[2], "variant"))
358 se->variantRequest= read_variant(argc-3, argv+3, nmem);
362 logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);