2 * Copyright (c) 1995-2001, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: d1_espec.c,v 1.20 2001-09-30 20:15:49 adam Exp $
14 #include <yaz/proto.h>
16 #include <yaz/data1.h>
18 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
19 const char *file, int lineno)
21 Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
26 var1.proto = PROTO_Z3950;
27 var1.oclass = CLASS_VARSET;
28 var1.value = VAL_VAR1;
29 r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
32 r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
35 r->num_triples = argc;
36 for (i = 0; i < argc; i++)
42 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
44 yaz_log(LOG_WARN, "%s:%d: Syntax error in variant component '%s'",
45 file, lineno, argv[i]);
48 t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
50 t->zclass = (int *)nmem_malloc(nmem, sizeof(int));
52 t->type = (int *)nmem_malloc(nmem, sizeof(int));
55 * This is wrong.. we gotta look up the correct type for the
56 * variant, I guess... damn this stuff.
60 t->which = Z_Triple_null;
61 t->value.null = odr_nullval();
63 else if (d1_isdigit(*value))
65 t->which = Z_Triple_integer;
66 t->value.integer = (int *)
67 nmem_malloc(nmem, sizeof(*t->value.integer));
68 *t->value.integer = atoi(value);
72 t->which = Z_Triple_internationalString;
73 t->value.internationalString = (char *)
74 nmem_malloc(nmem, strlen(value)+1);
75 strcpy(t->value.internationalString, value);
81 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
82 const char *file, int lineno)
84 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
89 op->which = Z_Occurrences_values;
90 op->u.values = (Z_OccurValues *)
91 nmem_malloc(nmem, sizeof(Z_OccurValues));
92 op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
93 *op->u.values->start = 1;
94 op->u.values->howMany = 0;
96 else if (!strcmp(occ, "all"))
98 op->which = Z_Occurrences_all;
99 op->u.all = odr_nullval();
101 else if (!strcmp(occ, "last"))
103 op->which = Z_Occurrences_last;
104 op->u.all = odr_nullval();
108 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
110 if (!d1_isdigit(*occ))
112 yaz_log(LOG_WARN, "%s:%d: Bad occurrences-spec %s",
116 op->which = Z_Occurrences_values;
118 ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
119 *ov->start = atoi(occ);
120 if ((p = strchr(occ, '+')))
122 ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
123 *ov->howMany = atoi(p + 1);
132 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
133 const char *file, int lineno)
135 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
138 char value[512], occ[512];
142 u->which = Z_ETagUnit_wildPath;
143 u->u.wildPath = odr_nullval();
145 else if (*buf == '?')
147 u->which = Z_ETagUnit_wildThing;
149 u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
151 u->u.wildThing = read_occurrences(0, nmem, file, lineno);
153 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-zA-Z0-9+]", &type, value,
159 int force_string = 0;
166 u->which = Z_ETagUnit_specificTag;
167 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
168 t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
170 t->tagValue = (Z_StringOrNumeric *)
171 nmem_malloc(nmem, sizeof(*t->tagValue));
172 if (!force_string && (numval = atoi(valp)))
174 t->tagValue->which = Z_StringOrNumeric_numeric;
175 t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
176 *t->tagValue->u.numeric = numval;
180 t->tagValue->which = Z_StringOrNumeric_string;
181 t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
182 strcpy(t->tagValue->u.string, valp);
184 if (terms > 2) /* an occurrences-spec exists */
185 t->occurrences = read_occurrences(occ, nmem, file, lineno);
193 * Read an element-set specification from a file.
194 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
196 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
199 NMEM nmem = data1_nmem_get (dh);
201 int argc, size_esn = 0;
202 char *argv[50], line[512];
203 Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
205 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
207 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
211 res->num_elementSetNames = 0;
212 res->elementSetNames = 0;
213 res->defaultVariantSetId = 0;
214 res->defaultVariantRequest = 0;
215 res->defaultTagType = 0;
216 res->num_elements = 0;
219 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
220 if (!strcmp(argv[0], "elementsetnames"))
222 int nnames = argc-1, i;
226 yaz_log(LOG_WARN, "%s:%d: Empty elementsetnames directive",
231 res->elementSetNames =
232 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
233 for (i = 0; i < nnames; i++)
235 res->elementSetNames[i] = (char *)
236 nmem_malloc(nmem, strlen(argv[i+1])+1);
237 strcpy(res->elementSetNames[i], argv[i+1]);
239 res->num_elementSetNames = nnames;
241 else if (!strcmp(argv[0], "defaultvariantsetid"))
245 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
246 file, lineno, argv[0]);
249 if (!(res->defaultVariantSetId =
250 odr_getoidbystr_nmem(nmem, argv[1])))
252 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantsetid",
257 else if (!strcmp(argv[0], "defaulttagtype"))
261 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
262 file, lineno, argv[0]);
265 res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
266 *res->defaultTagType = atoi(argv[1]);
268 else if (!strcmp(argv[0], "defaultvariantrequest"))
270 if (!(res->defaultVariantRequest =
271 read_variant(argc-1, argv+1, nmem, file, lineno)))
273 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantrequest",
278 else if (!strcmp(argv[0], "simpleelement"))
280 Z_ElementRequest *er;
283 char *path = argv[1];
288 res->elements = (Z_ElementRequest **)
289 nmem_malloc(nmem, size_esn = 24*sizeof(er));
290 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
292 Z_ElementRequest **oe = res->elements;
294 res->elements = (Z_ElementRequest **)
295 nmem_malloc (nmem, size_esn*sizeof(er));
296 memcpy (res->elements, oe, size_esn/2);
300 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
301 file, lineno, argv[0]);
305 res->elements[res->num_elements++] = er =
306 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
307 er->which = Z_ERequest_simpleElement;
308 er->u.simpleElement = se = (Z_SimpleElement *)
309 nmem_malloc(nmem, sizeof(*se));
310 se->variantRequest = 0;
311 se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
314 * Parse the element selector.
316 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
318 tp->tags = (Z_ETagUnit **)
319 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
321 for ((ep = strchr(path, '/')) ; path ;
322 (void)((path = ep) && (ep = strchr(path, '/'))))
329 tagunit = read_tagunit(path, nmem, file, lineno);
332 yaz_log (LOG_WARN, "%s%d: Bad tag unit at %s",
336 tp->tags[tp->num_tags++] = tagunit;
339 if (argc > 2 && !strcmp(argv[2], "variant"))
341 read_variant(argc-3, argv+3, nmem, file, lineno);
344 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
345 file, lineno, argv[0]);