change license for data1 source
[idzebra-moved-to-github.git] / data1 / d1_varset.c
1 /* $Id: d1_varset.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <yaz/oid.h>
27 #include <yaz/log.h>
28 #include <data1.h>
29
30 data1_vartype *data1_getvartypebyct (data1_handle dh, data1_varset *set,
31                                      char *zclass, 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(LOG_WARN, "Unknown variant type %s in class %s",
43                     type, zclass);
44             return 0;
45         }
46     yaz_log(LOG_WARN, "Unknown variant class %s", zclass);
47     return 0;
48 }
49
50 data1_varset *data1_read_varset (data1_handle dh, const char *file)
51 {
52     NMEM mem = data1_nmem_get (dh);
53     data1_varset *res = (data1_varset *)nmem_malloc(mem, sizeof(*res));
54     data1_varclass **classp = &res->classes, *zclass = 0;
55     data1_vartype **typep = 0;
56     FILE *f;
57     int lineno = 0;
58     int argc;
59     char *argv[50],line[512];
60
61     res->name = 0;
62     res->reference = VAL_NONE;
63     res->classes = 0;
64
65     if (!(f = data1_path_fopen(dh, file, "r")))
66     {
67         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
68         return 0;
69     }
70     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
71         if (!strcmp(argv[0], "class"))
72         {
73             data1_varclass *r;
74             
75             if (argc != 3)
76             {
77                 yaz_log(LOG_WARN, "%s:%d: Bad # or args to class",
78                         file, lineno);
79                 continue;
80             }
81             *classp = r = zclass = (data1_varclass *)
82                 nmem_malloc(mem, sizeof(*r));
83             r->set = res;
84             r->zclass = atoi(argv[1]);
85             r->name = nmem_strdup(mem, argv[2]);
86             r->types = 0;
87             typep = &r->types;
88             r->next = 0;
89             classp = &r->next;
90         }
91         else if (!strcmp(argv[0], "type"))
92         {
93             data1_vartype *r;
94
95             if (!typep)
96             {
97                 yaz_log(LOG_WARN, "%s:%d: Directive class must precede type",
98                         file, lineno);
99                 continue;
100             }
101             if (argc != 4)
102             {
103                 yaz_log(LOG_WARN, "%s:%d: Bad # or args to type",
104                         file, lineno);
105                 continue;
106             }
107             *typep = r = (data1_vartype *)nmem_malloc(mem, sizeof(*r));
108             r->name = nmem_strdup(mem, argv[2]);
109             r->zclass = zclass;
110             r->type = atoi(argv[1]);
111             if (!(r->datatype = data1_maptype (dh, argv[3])))
112             {
113                 yaz_log(LOG_WARN, "%s:%d: Unknown datatype '%s'",
114                         file, lineno, argv[3]);
115                 fclose(f);
116                 return 0;
117             }
118             r->next = 0;
119             typep = &r->next;
120         }
121         else if (!strcmp(argv[0], "name"))
122         {
123             if (argc != 2)
124             {
125                 yaz_log(LOG_WARN, "%s:%d: Bad # args for name",
126                         file, lineno);
127                 continue;
128             }
129             res->name = nmem_strdup(mem, argv[1]);
130         }
131         else if (!strcmp(argv[0], "reference"))
132         {
133             if (argc != 2)
134             {
135                 yaz_log(LOG_WARN, "%s:%d: Bad # args for reference",
136                         file, lineno);
137                 continue;
138             }
139             if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
140             {
141                 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
142                         file, lineno, argv[1]);
143                 continue;
144             }
145         }
146         else 
147             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
148                     file, lineno, argv[0]);
149     
150     fclose(f);
151     return res;
152 }