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