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