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