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