Updated footer comment
[idzebra-moved-to-github.git] / data1 / d1_varset.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include <yaz/oid_db.h>
24 #include <yaz/log.h>
25 #include <d1_absyn.h>
26
27 data1_vartype *data1_getvartypebyct (data1_handle dh, data1_varset *set,
28                                      const char *zclass, const char *type)
29 {
30     data1_varclass *c;
31     data1_vartype *t;
32
33     for (c = set->classes; c; c = c->next)
34         if (!data1_matchstr(c->name, zclass))
35         {
36             for (t = c->types; t; t = t->next)
37                 if (!data1_matchstr(t->name, type))
38                     return t;
39             yaz_log(YLOG_WARN, "Unknown variant type %s in class %s",
40                     type, zclass);
41             return 0;
42         }
43     yaz_log(YLOG_WARN, "Unknown variant class %s", zclass);
44     return 0;
45 }
46
47 data1_vartype *data1_getvartypeby_absyn (data1_handle dh, data1_absyn *absyn,
48                                            char *zclass, char *type)
49 {
50     return data1_getvartypebyct(dh, absyn->varset, zclass, type);
51 }
52
53 data1_varset *data1_read_varset (data1_handle dh, const char *file)
54 {
55     NMEM mem = data1_nmem_get (dh);
56     data1_varset *res = (data1_varset *)nmem_malloc(mem, sizeof(*res));
57     data1_varclass **classp = &res->classes, *zclass = 0;
58     data1_vartype **typep = 0;
59     FILE *f;
60     int lineno = 0;
61     int argc;
62     char *argv[50],line[512];
63
64     res->name = 0;
65     res->oid = 0;
66     res->classes = 0;
67
68     if (!(f = data1_path_fopen(dh, file, "r")))
69     {
70         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", file);
71         return 0;
72     }
73     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
74         if (!strcmp(argv[0], "class"))
75         {
76             data1_varclass *r;
77             
78             if (argc != 3)
79             {
80                 yaz_log(YLOG_WARN, "%s:%d: Bad # or args to class",
81                         file, lineno);
82                 continue;
83             }
84             *classp = r = zclass = (data1_varclass *)
85                 nmem_malloc(mem, sizeof(*r));
86             r->set = res;
87             r->zclass = atoi(argv[1]);
88             r->name = nmem_strdup(mem, argv[2]);
89             r->types = 0;
90             typep = &r->types;
91             r->next = 0;
92             classp = &r->next;
93         }
94         else if (!strcmp(argv[0], "type"))
95         {
96             data1_vartype *r;
97
98             if (!typep)
99             {
100                 yaz_log(YLOG_WARN, "%s:%d: Directive class must precede type",
101                         file, lineno);
102                 continue;
103             }
104             if (argc != 4)
105             {
106                 yaz_log(YLOG_WARN, "%s:%d: Bad # or args to type",
107                         file, lineno);
108                 continue;
109             }
110             *typep = r = (data1_vartype *)nmem_malloc(mem, sizeof(*r));
111             r->name = nmem_strdup(mem, argv[2]);
112             r->zclass = zclass;
113             r->type = atoi(argv[1]);
114             if (!(r->datatype = data1_maptype (dh, argv[3])))
115             {
116                 yaz_log(YLOG_WARN, "%s:%d: Unknown datatype '%s'",
117                         file, lineno, argv[3]);
118                 fclose(f);
119                 return 0;
120             }
121             r->next = 0;
122             typep = &r->next;
123         }
124         else if (!strcmp(argv[0], "name"))
125         {
126             if (argc != 2)
127             {
128                 yaz_log(YLOG_WARN, "%s:%d: Bad # args for name",
129                         file, lineno);
130                 continue;
131             }
132             res->name = nmem_strdup(mem, argv[1]);
133         }
134         else if (!strcmp(argv[0], "reference"))
135         {
136             if (argc != 2)
137             {
138                 yaz_log(YLOG_WARN, "%s:%d: Bad # args for reference",
139                         file, lineno);
140                 continue;
141             }
142             res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
143                                               CLASS_VARSET, argv[1], mem);
144             if (!res->oid)
145             {
146                 yaz_log(YLOG_WARN, "%s:%d: Unknown reference '%s'",
147                         file, lineno, argv[1]);
148                 continue;
149             }
150         }
151         else 
152             yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
153                     file, lineno, argv[0]);
154     
155     fclose(f);
156     return res;
157 }
158 /*
159  * Local variables:
160  * c-basic-offset: 4
161  * c-file-style: "Stroustrup"
162  * indent-tabs-mode: nil
163  * End:
164  * vim: shiftwidth=4 tabstop=8 expandtab
165  */
166