Renamed logf function to yaz_log. Removed VC++ project files.
[yaz-moved-to-github.git] / retrieval / d1_attset.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_attset.c,v $
7  * Revision 1.12  1999-08-27 09:40:32  adam
8  * Renamed logf function to yaz_log. Removed VC++ project files.
9  *
10  * Revision 1.11  1998/10/14 13:31:56  adam
11  * Bug fix. Bug introduced by previous commit.
12  *
13  * Revision 1.10  1998/10/13 16:09:48  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.9  1998/05/18 13:07:03  adam
23  * Changed the way attribute sets are handled by the retriaval module.
24  * Extended Explain conversion / schema.
25  * Modified server and client to work with ASN.1 compiled protocol handlers.
26  *
27  * Revision 1.8  1998/02/11 11:53:35  adam
28  * Changed code so that it compiles as C++.
29  *
30  * Revision 1.7  1997/09/17 12:10:34  adam
31  * YAZ version 1.4.
32  *
33  * Revision 1.6  1997/09/05 09:50:56  adam
34  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
35  *
36  * Revision 1.5  1996/05/09 07:27:43  quinn
37  * Multiple local attributes values supported.
38  *
39  * Revision 1.4  1996/02/21  15:23:36  quinn
40  * Reversed fclose and return;
41  *
42  * Revision 1.3  1995/12/13  17:14:26  quinn
43  * *** empty log message ***
44  *
45  * Revision 1.2  1995/11/01  16:34:55  quinn
46  * Making data1 look for tables in data1_tabpath
47  *
48  * Revision 1.1  1995/11/01  11:56:07  quinn
49  * Added Retrieval (data management) functions en masse.
50  *
51  *
52  */
53
54 #include <ctype.h>
55 #include <stdio.h>
56 #include <assert.h>
57 #include <stdlib.h>
58
59 #include <log.h>
60 #include <d1_attset.h>
61 #include <data1.h>
62
63 data1_att *data1_getattbyname(data1_handle dh, data1_attset *s, char *name)
64 {
65     data1_att *r;
66     data1_attset_child *c;
67     
68     /* scan local set */
69     for (r = s->atts; r; r = r->next)
70         if (!data1_matchstr(r->name, name))
71             return r;
72     for (c = s->children; c; c = c->next)
73     {
74         assert (c->child);
75         /* scan included sets */
76         if ((r = data1_getattbyname (dh, c->child, name)))
77             return r;
78     }
79     return 0;
80 }
81
82 data1_attset *data1_empty_attset(data1_handle dh)
83 {
84     NMEM mem = data1_nmem_get (dh);
85     data1_attset *res = (data1_attset*) nmem_malloc(mem,sizeof(*res));
86
87     res->name = 0;
88     res->reference = VAL_NONE;
89     res->atts = 0;
90     res->children = 0;
91     res->next = 0;
92     return res;
93 }
94
95 data1_attset *data1_read_attset(data1_handle dh, const char *file)
96 {
97     data1_attset *res = 0;
98     data1_attset_child **childp;
99     data1_att **attp;
100     FILE *f;
101     NMEM mem = data1_nmem_get (dh);
102     int lineno = 0;
103     int argc;
104     char *argv[50], line[512];
105
106     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
107         return NULL;
108     res = data1_empty_attset (dh);
109
110     childp = &res->children;
111     attp = &res->atts;
112
113     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
114     {
115         char *cmd = argv[0];
116         if (!strcmp(cmd, "att"))
117         {
118             int num;
119             char *name;
120             data1_att *t;
121             data1_local_attribute *locals;
122             
123             if (argc < 3)
124             {
125                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to att", file, lineno);
126                 continue;
127             }
128             num = atoi (argv[1]);
129             name = argv[2];
130             
131             if (argc == 3) /* no local attributes given */
132             {
133                 locals = (data1_local_attribute *)
134                     nmem_malloc(mem, sizeof(*locals));
135                 locals->local = num;
136                 locals->next = 0;
137             }
138             else /* parse the string "local{,local}" */
139             {
140                 char *p = argv[3];
141                 data1_local_attribute **ap = &locals;
142                 do
143                 {
144                     *ap = (data1_local_attribute *)
145                         nmem_malloc(mem, sizeof(**ap));
146                     (*ap)->local = atoi(p);
147                     (*ap)->next = 0;
148                     ap = &(*ap)->next;
149                 }
150                 while ((p = strchr(p, ',')) && *(++p));
151             }
152             t = *attp = (data1_att *)nmem_malloc(mem, sizeof(*t));
153             t->parent = res;
154             t->name = nmem_strdup(mem, name);
155             t->value = num;
156             t->locals = locals;
157             t->next = 0;
158             attp = &t->next;
159         }
160         else if (!strcmp(cmd, "name"))
161         {
162             if (argc != 2)
163             {
164                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name", file, lineno);
165                 continue;
166             }
167         }
168         else if (!strcmp(cmd, "reference"))
169         {
170             char *name;
171
172             if (argc != 2)
173             {
174                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
175                         file, lineno);
176                 continue;
177             }
178             name = argv[1];
179             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
180             {
181                 yaz_log(LOG_WARN, "%s:%d: Unknown reference oid '%s'",
182                         file, lineno, name);
183                 fclose(f);
184                 return 0;
185             }
186         }
187         else if (!strcmp(cmd, "ordinal"))
188         {
189             yaz_log (LOG_WARN, "%s:%d: Directive ordinal ignored",
190                      file, lineno);
191         }
192         else if (!strcmp(cmd, "include"))
193         {
194             char *name;
195             data1_attset *attset;
196
197             if (argc != 2)
198             {
199                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to include",
200                         file, lineno);
201                 continue;
202             }
203             name = argv[1];
204
205             if (!(attset = data1_get_attset (dh, name)))
206             {
207                 yaz_log(LOG_WARN, "%s:%d: Include of attset %s failed",
208                         file, lineno, name);
209                 continue;
210                 
211             }
212             *childp = (data1_attset_child *)
213                 nmem_malloc (mem, sizeof(**childp));
214             (*childp)->child = attset;
215             (*childp)->next = 0;
216             childp = &(*childp)->next;
217         }
218         else
219         {
220             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
221                     file, lineno, cmd);
222         }
223     }
224     fclose(f);
225     return res;
226 }