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