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