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