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