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