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