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