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