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