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