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