Making data1 look for tables in data1_tabpath
[yaz-moved-to-github.git] / retrieval / d1_espec.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_espec.c,v $
7  * Revision 1.2  1995-11-01 16:34:56  quinn
8  * Making data1 look for tables in data1_tabpath
9  *
10  * Revision 1.1  1995/11/01  11:56:07  quinn
11  * Added Retrieval (data management) functions en masse.
12  *
13  *
14  */
15
16
17 #include <stdlib.h>
18 #include <assert.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <xmalloc.h>
22 #include <odr.h>
23 #include <proto.h>
24 #include <log.h>
25 #include <readconf.h>
26 #include <tpath.h>
27 #include <data1.h>
28
29 /*
30  * Read an element-set specification from a file. If !o, use xmalloc for
31  * memory allocation.
32  */
33 Z_Espec1 *data1_read_espec1(char *file, ODR o)
34 {
35     FILE *f;
36     int argc, size_esn = 0;
37     char *argv[50], line[512];
38     Z_Espec1 *res = odr_malloc(o, sizeof(*res));
39
40     if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
41     {
42         logf(LOG_WARN|LOG_ERRNO, "%s", file);
43         return 0;
44     }
45
46     res->num_elementSetNames = 0;
47     res->elementSetNames = 0;
48     res->defaultVariantSetId = 0;
49     res->defaultVariantRequest = 0;
50     res->defaultTagType = 0;
51     res->num_elements = 0;
52     res->elements = 0;
53
54     while ((argc = readconf_line(f, line, 512, argv, 50)))
55         if (!strcmp(argv[0], "elementsetnames"))
56         {
57             int nnames = argc-1, i;
58
59             if (!nnames)
60             {
61                 logf(LOG_WARN, "%s: Empty elementsetnames directive",
62                     file);
63                 continue;
64             }
65
66             res->elementSetNames = odr_malloc(o, sizeof(char*)*nnames);
67             for (i = 0; i < nnames; i++)
68             {
69                 res->elementSetNames[i] = odr_malloc(o, strlen(argv[i+1])+1);
70                 strcpy(res->elementSetNames[i], argv[i+1]);
71             }
72             res->num_elementSetNames = nnames;
73         }
74         else if (!strcmp(argv[0], "defaultvariantsetid"))
75         {
76             if (argc != 2 || !(res->defaultVariantSetId =
77                 odr_getoidbystr(o, argv[1])))
78             {
79                 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
80                 continue;
81             }
82         }
83         else if (!strcmp(argv[0], "defaulttagtype"))
84         {
85             if (argc != 2)
86             {
87                 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
88                 continue;
89             }
90             res->defaultTagType = odr_malloc(o, sizeof(int));
91             *res->defaultTagType = atoi(argv[1]);
92         }
93         else if (!strcmp(argv[0], "defaultvariantrequest"))
94         {
95             abort();
96         }
97         else if (!strcmp(argv[0], "simpleelement"))
98         {
99             Z_ElementRequest *er;
100             Z_SimpleElement *se;
101             Z_ETagPath *tp;
102             char *path = argv[1];
103             char *ep;
104             int num, i = 0;
105
106             if (!res->elements)
107                 res->elements = odr_malloc(o, size_esn = 24*sizeof(*er));
108             else if (res->num_elements >= size_esn)
109             {
110                 size_esn *= 2;
111                 res->elements = o ? odr_malloc(o, size_esn) :
112                     xrealloc(res->elements, size_esn);
113             }
114             if (argc < 2)
115             {
116                 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
117                 continue;
118             }
119             res->elements[res->num_elements++] = er =
120                 odr_malloc(o, sizeof(*er));
121             er->which = Z_ERequest_simpleElement;
122             er->u.simpleElement = se = odr_malloc(o, sizeof(*se));
123             se->variantRequest = 0;
124             se->path = tp = odr_malloc(o, sizeof(*tp));
125             tp->num_tags = 0;
126             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++);
127             tp->tags = odr_malloc(o, sizeof(Z_ETagUnit*)*num);
128
129             for ((ep = strchr(path, '/')) ; path ; (void)((path = ep) &&
130                 (ep = strchr(path, '/'))))
131             {
132                 int type;
133                 char value[512];
134                 Z_ETagUnit *u;
135
136                 if (ep)
137                     ep++;
138
139                 assert(i<num);
140                 tp->tags[tp->num_tags++] = u = odr_malloc(o, sizeof(*u));
141                 if (sscanf(path, "(%d,%[^)])", &type, value) == 2)
142                 {
143                     int numval;
144                     Z_SpecificTag *t;
145                     char *valp = value;
146                     int force_string = 0;
147
148                     if (*valp == '\'')
149                     {
150                         valp++;
151                         force_string = 1;
152                     }
153                     u->which = Z_ETagUnit_specificTag;
154                     u->u.specificTag = t = odr_malloc(o, sizeof(*t));
155                     t->tagType = odr_malloc(o, sizeof(*t->tagType));
156                     *t->tagType = type;
157                     t->tagValue = odr_malloc(o, sizeof(*t->tagValue));
158                     if (!force_string && (numval = atoi(valp)))
159                     {
160                         t->tagValue->which = Z_StringOrNumeric_numeric;
161                         t->tagValue->u.numeric = odr_malloc(o, sizeof(int));
162                         *t->tagValue->u.numeric = numval;
163                     }
164                     else
165                     {
166                         t->tagValue->which = Z_StringOrNumeric_string;
167                         t->tagValue->u.string = odr_malloc(o, strlen(valp)+1);
168                         strcpy(t->tagValue->u.string, valp);
169                     }
170                     t->occurrences = 0; /* for later */
171                 }
172             }
173         }
174         else
175         {
176             logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);
177             fclose(f);
178             return 0;
179         }
180
181     return res;
182 }