7ba2a81e01d6a442d5e65d7fbce4a81a865338b5
[yaz-moved-to-github.git] / retrieval / d1_tagset.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_tagset.c,v $
7  * Revision 1.10  1998-10-15 08:29:17  adam
8  * Tag set type may be specified in reference to it using "tagset"
9  * directive in .abs-files and "include" directive in .tag-files.
10  *
11  * Revision 1.9  1998/10/13 16:09:53  adam
12  * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
13  * Added support for multiple attribute set references and tagset references
14  * from an abstract syntax file.
15  * Fixed many bad logs-calls in routines that read the various
16  * specifications regarding data1 (*.abs,*.att,...) and made the messages
17  * consistent whenever possible.
18  * Added extra 'lineno' argument to function readconf_line.
19  *
20  * Revision 1.8  1998/05/18 13:07:07  adam
21  * Changed the way attribute sets are handled by the retriaval module.
22  * Extended Explain conversion / schema.
23  * Modified server and client to work with ASN.1 compiled protocol handlers.
24  *
25  * Revision 1.7  1998/02/11 11:53:35  adam
26  * Changed code so that it compiles as C++.
27  *
28  * Revision 1.6  1997/09/17 12:10:38  adam
29  * YAZ version 1.4.
30  *
31  * Revision 1.5  1997/09/05 09:50:57  adam
32  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
33  *
34  * Revision 1.4  1995/11/13 09:27:38  quinn
35  * Fiddling with the variant stuff.
36  *
37  * Revision 1.3  1995/11/01  16:34:58  quinn
38  * Making data1 look for tables in data1_tabpath
39  *
40  * Revision 1.2  1995/11/01  13:54:49  quinn
41  * Minor adjustments
42  *
43  * Revision 1.1  1995/11/01  11:56:09  quinn
44  * Added Retrieval (data management) functions en masse.
45  *
46  *
47  */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <ctype.h>
52 #include <string.h>
53
54 #include <log.h>
55 #include <data1.h>
56
57 /*
58  * We'll probably want to add some sort of hashed index to these lookup-
59  * functions eventually.
60  */
61
62 data1_datatype data1_maptype (data1_handle dh, char *t)
63 {
64     static struct
65     {
66         char *tname;
67         data1_datatype type;
68     } types[] =
69     {
70         {"structured", DATA1K_structured},
71         {"string", DATA1K_string},
72         {"numeric", DATA1K_numeric},
73         {"oid", DATA1K_oid},
74         {"bool", DATA1K_bool},
75         {"generalizedtime", DATA1K_generalizedtime},
76         {"intunit", DATA1K_intunit},
77         {"int", DATA1K_int},
78         {"octetstring", DATA1K_octetstring},
79         {"null", DATA1K_null},
80         {NULL, (data1_datatype) -1}
81     };
82     int i;
83
84     for (i = 0; types[i].tname; i++)
85         if (!data1_matchstr(types[i].tname, t))
86             return types[i].type;
87     return DATA1K_unknown;
88 }
89
90 data1_tag *data1_gettagbynum (data1_handle dh, data1_tagset *s,
91                               int type, int value)
92 {
93     data1_tag *r;
94     
95     for (; s; s = s->next)
96     {
97         /* scan local set */
98         if (type == s->type)
99             for (r = s->tags; r; r = r->next)
100                 if (r->which == DATA1T_numeric && r->value.numeric == value)
101                     return r;
102         /* scan included sets */
103         if (s->children &&
104             (r = data1_gettagbynum (dh, s->children, type, value)))
105             return r;
106     }
107     return 0;
108 }
109
110 data1_tag *data1_gettagbyname (data1_handle dh, data1_tagset *s,
111                                const char *name)
112 {
113     data1_tag *r;
114
115     for (; s; s = s->next)
116     {
117         /* scan local set */
118         for (r = s->children->tags; r; r = r->next)
119         {
120             data1_name *np;
121
122             for (np = r->names; np; np = np->next)
123                 if (!data1_matchstr(np->name, name))
124                     return r;
125         }
126         /* scan included sets */
127         if (s->children && (r = data1_gettagbyname (dh, s->children, name)))
128             return r;
129     }
130     return 0;
131 }
132
133 data1_tagset *data1_empty_tagset (data1_handle dh)
134 {
135     data1_tagset *res =
136         (data1_tagset *) nmem_malloc(data1_nmem_get (dh), sizeof(*res));
137     res->name = 0;
138     res->reference = VAL_NONE;
139     res->tags = 0;
140     res->type = 0;
141     res->children = 0;
142     res->next = 0;
143     return res;
144 }
145
146 data1_tagset *data1_read_tagset (data1_handle dh, const char *file, int type)
147 {
148     NMEM mem = data1_nmem_get (dh);
149     data1_tagset *res = 0;
150     data1_tagset **childp;
151     data1_tag **tagp;
152     FILE *f;
153     int lineno = 0;
154     int argc;
155     char *argv[50], line[512];
156
157     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
158     {
159         logf(LOG_WARN|LOG_ERRNO, "%s", file);
160         return 0;
161     }
162     res = data1_empty_tagset (dh);
163     res->type = type;
164     childp = &res->children;
165     tagp = &res->tags;
166
167     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
168     {
169         char *cmd = *argv;
170         if (!strcmp(cmd, "tag"))
171         {
172             int value;
173             char *names, *type, *nm;
174             data1_tag *rr;
175             data1_name **npp;
176
177             if (argc != 4)
178             {
179                 logf(LOG_WARN, "%s:%d: Bad # args to tag", file, lineno);
180                 continue;
181             }
182             value = atoi(argv[1]);
183             names = argv[2];
184             type = argv[3];
185
186             rr = *tagp = (data1_tag *)nmem_malloc(mem, sizeof(*rr));
187             rr->tagset = res;
188             rr->next = 0;
189             rr->which = DATA1T_numeric;
190             rr->value.numeric = value;
191             /*
192              * how to deal with local numeric tags?
193              */
194
195             if (!(rr->kind = data1_maptype(dh, type)))
196             {
197                 logf(LOG_WARN, "%s:%d: Unknown datatype %s",
198                      file, lineno, type);
199                 fclose(f);
200                 return 0;
201             }
202             
203             /* read namelist */
204             nm = names;
205             npp = &rr->names;
206             do
207             {
208                 char *e;
209
210                 *npp = (data1_name *)nmem_malloc(mem, sizeof(**npp));
211                 if ((e = strchr(nm, '/')))
212                     *(e++) = '\0';
213                 (*npp)->name = nmem_strdup(mem, nm);
214                 (*npp)->next = 0;
215                 npp = &(*npp)->next;
216                 nm = e;
217             }
218             while (nm);
219             tagp = &rr->next;
220         }
221         else if (!strcmp(cmd, "name"))
222         {
223             if (argc != 2)
224             {
225                 logf(LOG_WARN, "%s:%d: Bad # args to name", file, lineno);
226                 continue;
227             }
228             res->name = nmem_strdup(mem, argv[1]);
229         }
230         else if (!strcmp(cmd, "reference"))
231         {
232             char *name;
233             
234             if (argc != 2)
235             {
236                 logf(LOG_WARN, "%s:%d: Bad # args to reference", file, lineno);
237                 continue;
238             }
239             name = argv[1];
240             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
241             {
242                 logf(LOG_WARN, "%s:%d: Unknown tagset ref '%s'",
243                      file, lineno, name);
244                 continue;
245             }
246         }
247         else if (!strcmp(cmd, "type"))
248         {
249             if (argc != 2)
250             {
251                 logf (LOG_WARN, "%s:%d: Bad # args to type", file, lineno);
252                 continue;
253             }
254             if (!res->type)
255                 res->type = atoi(argv[1]);
256         }
257         else if (!strcmp(cmd, "include"))
258         {
259             char *name;
260             int type = 0;
261
262             if (argc < 2)
263             {
264                 logf(LOG_WARN, "%s:%d: Bad # args to include",
265                      file, lineno);
266                 continue;
267             }
268             name = argv[1];
269             if (argc == 3)
270                 type = atoi(argv[2]);
271             *childp = data1_read_tagset (dh, name, type);
272             if (!(*childp))
273             {
274                 logf(LOG_WARN, "%s:%d: Inclusion failed for tagset %s",
275                      file, lineno, name);
276                 continue;
277             }
278             childp = &(*childp)->next;
279         }
280         else
281         {
282             logf(LOG_WARN, "%s:%d: Unknown directive '%s'",
283                  file, lineno, cmd);
284         }
285     }
286     fclose(f);
287     return res;
288 }