data1 part of zebra
[idzebra-moved-to-github.git] / data1 / d1_varset.c
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_varset.c,v $
7  * Revision 1.1  2002-10-22 12:53:33  adam
8  * data1 part of zebra
9  *
10  * Revision 1.11  2002/04/04 20:49:46  adam
11  * New functions yaz_is_abspath, yaz_path_fopen_base
12  *
13  * Revision 1.10  1999/11/30 13:47:12  adam
14  * Improved installation. Moved header files to include/yaz.
15  *
16  * Revision 1.9  1999/08/27 09:40:32  adam
17  * Renamed logf function to yaz_log. Removed VC++ project files.
18  *
19  * Revision 1.8  1998/10/13 16:09:54  adam
20  * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
21  * Added support for multiple attribute set references and tagset references
22  * from an abstract syntax file.
23  * Fixed many bad logs-calls in routines that read the various
24  * specifications regarding data1 (*.abs,*.att,...) and made the messages
25  * consistent whenever possible.
26  * Added extra 'lineno' argument to function readconf_line.
27  *
28  * Revision 1.7  1998/02/11 11:53:35  adam
29  * Changed code so that it compiles as C++.
30  *
31  * Revision 1.6  1997/09/17 12:10:39  adam
32  * YAZ version 1.4.
33  *
34  * Revision 1.5  1997/09/05 09:50:58  adam
35  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
36  *
37  * Revision 1.4  1997/05/14 06:54:04  adam
38  * C++ support.
39  *
40  * Revision 1.3  1995/11/01 16:34:58  quinn
41  * Making data1 look for tables in data1_tabpath
42  *
43  * Revision 1.2  1995/11/01  13:54:50  quinn
44  * Minor adjustments
45  *
46  * Revision 1.1  1995/11/01  11:56:09  quinn
47  * Added Retrieval (data management) functions en masse.
48  *
49  *
50  */
51
52 #include <string.h>
53 #include <stdlib.h>
54
55 #include <yaz/oid.h>
56 #include <yaz/log.h>
57 #include <data1.h>
58
59 data1_vartype *data1_getvartypebyct (data1_handle dh, data1_varset *set,
60                                      char *zclass, char *type)
61 {
62     data1_varclass *c;
63     data1_vartype *t;
64
65     for (c = set->classes; c; c = c->next)
66         if (!data1_matchstr(c->name, zclass))
67         {
68             for (t = c->types; t; t = t->next)
69                 if (!data1_matchstr(t->name, type))
70                     return t;
71             yaz_log(LOG_WARN, "Unknown variant type %s in class %s",
72                     type, zclass);
73             return 0;
74         }
75     yaz_log(LOG_WARN, "Unknown variant class %s", zclass);
76     return 0;
77 }
78
79 data1_varset *data1_read_varset (data1_handle dh, const char *file)
80 {
81     NMEM mem = data1_nmem_get (dh);
82     data1_varset *res = (data1_varset *)nmem_malloc(mem, sizeof(*res));
83     data1_varclass **classp = &res->classes, *zclass = 0;
84     data1_vartype **typep = 0;
85     FILE *f;
86     int lineno = 0;
87     int argc;
88     char *argv[50],line[512];
89
90     res->name = 0;
91     res->reference = VAL_NONE;
92     res->classes = 0;
93
94     if (!(f = data1_path_fopen(dh, file, "r")))
95     {
96         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
97         return 0;
98     }
99     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
100         if (!strcmp(argv[0], "class"))
101         {
102             data1_varclass *r;
103             
104             if (argc != 3)
105             {
106                 yaz_log(LOG_WARN, "%s:%d: Bad # or args to class",
107                         file, lineno);
108                 continue;
109             }
110             *classp = r = zclass = (data1_varclass *)
111                 nmem_malloc(mem, sizeof(*r));
112             r->set = res;
113             r->zclass = atoi(argv[1]);
114             r->name = nmem_strdup(mem, argv[2]);
115             r->types = 0;
116             typep = &r->types;
117             r->next = 0;
118             classp = &r->next;
119         }
120         else if (!strcmp(argv[0], "type"))
121         {
122             data1_vartype *r;
123
124             if (!typep)
125             {
126                 yaz_log(LOG_WARN, "%s:%d: Directive class must precede type",
127                         file, lineno);
128                 continue;
129             }
130             if (argc != 4)
131             {
132                 yaz_log(LOG_WARN, "%s:%d: Bad # or args to type",
133                         file, lineno);
134                 continue;
135             }
136             *typep = r = (data1_vartype *)nmem_malloc(mem, sizeof(*r));
137             r->name = nmem_strdup(mem, argv[2]);
138             r->zclass = zclass;
139             r->type = atoi(argv[1]);
140             if (!(r->datatype = data1_maptype (dh, argv[3])))
141             {
142                 yaz_log(LOG_WARN, "%s:%d: Unknown datatype '%s'",
143                         file, lineno, argv[3]);
144                 fclose(f);
145                 return 0;
146             }
147             r->next = 0;
148             typep = &r->next;
149         }
150         else if (!strcmp(argv[0], "name"))
151         {
152             if (argc != 2)
153             {
154                 yaz_log(LOG_WARN, "%s:%d: Bad # args for name",
155                         file, lineno);
156                 continue;
157             }
158             res->name = nmem_strdup(mem, argv[1]);
159         }
160         else if (!strcmp(argv[0], "reference"))
161         {
162             if (argc != 2)
163             {
164                 yaz_log(LOG_WARN, "%s:%d: Bad # args for reference",
165                         file, lineno);
166                 continue;
167             }
168             if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
169             {
170                 yaz_log(LOG_WARN, "%s:%d: Unknown reference '%s'",
171                         file, lineno, argv[1]);
172                 continue;
173             }
174         }
175         else 
176             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
177                     file, lineno, argv[0]);
178     
179     fclose(f);
180     return res;
181 }