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