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