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