Member and_not in Z_Operator is kept for backwards compatibility.
[yaz-moved-to-github.git] / ccl / cclqual.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /* CCL qualifiers
45  * Europagate, 1995
46  *
47  * $Log: cclqual.c,v $
48  * Revision 1.15  2001-03-07 13:24:40  adam
49  * Member and_not in Z_Operator is kept for backwards compatibility.
50  * Added support for definition of CCL operators in field spec file.
51  *
52  * Revision 1.14  2000/11/16 09:58:02  adam
53  * Implemented local AttributeSet setting for CCL field maps.
54  *
55  * Revision 1.13  2000/01/31 13:15:21  adam
56  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
57  * that some characters are not surrounded by spaces in resulting term.
58  * ILL-code updates.
59  *
60  * Revision 1.12  1999/11/30 13:47:11  adam
61  * Improved installation. Moved header files to include/yaz.
62  *
63  * Revision 1.11  1999/03/31 11:15:37  adam
64  * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
65  *
66  * Revision 1.10  1998/07/07 15:49:40  adam
67  * Added braces to avoid warning.
68  *
69  * Revision 1.9  1998/02/11 11:53:33  adam
70  * Changed code so that it compiles as C++.
71  *
72  * Revision 1.8  1997/09/29 08:56:38  adam
73  * Changed CCL parser to be thread safe. New type, CCL_parser, declared
74  * and a create/destructers ccl_parser_create/ccl_parser/destory has
75  * been added.
76  *
77  * Revision 1.7  1997/09/01 08:48:12  adam
78  * New windows NT/95 port using MSV5.0. Only a few changes made
79  * to avoid warnings.
80  *
81  * Revision 1.6  1997/04/30 08:52:07  quinn
82  * Null
83  *
84  * Revision 1.5  1996/10/11  15:00:25  adam
85  * CCL parser from Europagate Email gateway 1.0.
86  *
87  * Revision 1.9  1995/05/16  09:39:27  adam
88  * LICENSE.
89  *
90  * Revision 1.8  1995/05/11  14:03:57  adam
91  * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
92  * New variable ccl_case_sensitive, which controls whether reserved
93  * words and field names are case sensitive or not.
94  *
95  * Revision 1.7  1995/04/17  09:31:46  adam
96  * Improved handling of qualifiers. Aliases or reserved words.
97  *
98  * Revision 1.6  1995/02/23  08:32:00  adam
99  * Changed header.
100  *
101  * Revision 1.4  1995/02/14  19:55:12  adam
102  * Header files ccl.h/cclp.h are gone! They have been merged an
103  * moved to ../include/ccl.h.
104  * Node kind(s) in ccl_rpn_node have changed names.
105  *
106  * Revision 1.3  1995/02/14  16:20:56  adam
107  * Qualifiers are read from a file now.
108  *
109  * Revision 1.2  1995/02/14  10:25:56  adam
110  * The constructions 'qualifier rel term ...' implemented.
111  *
112  * Revision 1.1  1995/02/13  15:15:07  adam
113  * Added handling of qualifiers. Not finished yet.
114  *
115  */
116
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120
121 #include <yaz/ccl.h>
122
123 /* Definition of CCL_bibset pointer */
124 struct ccl_qualifiers {
125     struct ccl_qualifier *list;
126     struct ccl_qualifier_special *special;
127 };
128
129
130 /* CCL Qualifier special */
131 struct ccl_qualifier_special {
132     char *name;
133     char *value;
134     struct ccl_qualifier_special *next;
135 };
136
137 void ccl_qual_add_special (CCL_bibset bibset, const char *n, const char *v)
138 {
139     struct ccl_qualifier_special *p;
140     const char *pe;
141
142     for (p = bibset->special; p && strcmp(p->name, n); p = p->next)
143         ;
144     if (p)
145         free (p->value);
146     else
147     {
148         p = (struct ccl_qualifier_special *) malloc (sizeof(*p));
149         p->name = ccl_strdup (n);
150         p->value = 0;
151         p->next = bibset->special;
152         bibset->special = p;
153     }
154     while (strchr(" \t", *v))
155         ++v;
156     for (pe = v + strlen(v); pe != v; --pe)
157         if (!strchr(" \n\r\t", pe[-1]))
158             break;
159     p->value = (char*) malloc (pe - v + 1);
160     if (pe - v)
161         memcpy (p->value, v, pe - v);
162     p->value[pe - v] = '\0';
163 }
164
165
166 /*
167  * ccl_qual_add: Add qualifier to Bibset. If qualifier already
168  *               exists, then attributes are appendend to old
169  *               definition.
170  * name:    name of qualifier
171  * no:      No of attribute type/value pairs.
172  * pairs:   Attributes. pairs[0] first type, pair[1] first value,
173  *          ... pair[2*no-2] last type, pair[2*no-1] last value.
174  */
175 void ccl_qual_add_set (CCL_bibset b, const char *name, int no, int *pairs,
176                        char **attsets)
177 {
178     struct ccl_qualifier *q;
179     struct ccl_rpn_attr **attrp;
180
181     ccl_assert (b);
182     for (q = b->list; q; q = q->next)
183         if (!strcmp (name, q->name))
184             break;
185     if (!q)
186     {
187         struct ccl_qualifier *new_qual =
188             (struct ccl_qualifier *)malloc (sizeof(*new_qual));
189         ccl_assert (new_qual);
190         
191         new_qual->next = b->list;
192         b->list = new_qual;
193         
194         new_qual->name = (char *)malloc (strlen(name)+1);
195         ccl_assert (new_qual->name);
196         strcpy (new_qual->name, name);
197         attrp = &new_qual->attr_list;
198     }
199     else
200     {
201         attrp = &q->attr_list;
202         while (*attrp)
203             attrp = &(*attrp)->next;
204     }
205     while (--no >= 0)
206     {
207         struct ccl_rpn_attr *attr;
208
209         attr = (struct ccl_rpn_attr *)malloc (sizeof(*attr));
210         ccl_assert (attr);
211         attr->set = *attsets++;
212         attr->type = *pairs++;
213         attr->value = *pairs++;
214         *attrp = attr;
215         attrp = &attr->next;
216     }
217     *attrp = NULL;
218 }
219
220 /*
221  * ccl_qual_mk: Make new (empty) bibset.
222  * return:   empty bibset.
223  */
224 CCL_bibset ccl_qual_mk (void)
225 {
226     CCL_bibset b = (CCL_bibset)malloc (sizeof(*b));
227     ccl_assert (b);
228     b->list = NULL;     
229     b->special = NULL;
230     return b;
231 }
232
233 /*
234  * ccl_qual_rm: Delete bibset.
235  * b:        pointer to bibset
236  */
237 void ccl_qual_rm (CCL_bibset *b)
238 {
239     struct ccl_qualifier *q, *q1;
240     struct ccl_qualifier_special *sp, *sp1;
241
242     if (!*b)
243         return;
244     for (q = (*b)->list; q; q = q1)
245     {
246         struct ccl_rpn_attr *attr, *attr1;
247
248         for (attr = q->attr_list; attr; attr = attr1)
249         {
250             attr1 = attr->next;
251             if (attr->set)
252                 free (attr->set);
253             free (attr);
254         }
255         q1 = q->next;
256         free (q->name);
257         free (q);
258     }
259     for (sp = (*b)->special; sp; sp = sp1)
260     {
261         sp1 = sp->next;
262         free (sp->name);
263         free (sp->value);
264         free (sp);
265     }
266     free (*b);
267     *b = NULL;
268 }
269
270 /*
271  * ccl_qual_search: Search for qualifier in bibset.
272  * b:      Bibset
273  * name:   Name of qualifier to search for (need no null-termination)
274  * len:    Length of name.
275  * return: Attribute info. NULL if not found.
276  */
277 struct ccl_rpn_attr *ccl_qual_search (CCL_parser cclp,
278                                       const char *name, size_t len)
279 {
280     struct ccl_qualifier *q;
281
282     ccl_assert (cclp);
283     if (!cclp->bibset)
284         return NULL;
285     for (q = cclp->bibset->list; q; q = q->next)
286         if (strlen(q->name) == len)
287         {
288             if (cclp->ccl_case_sensitive)
289             {
290                 if (!memcmp (name, q->name, len))
291                     return q->attr_list;
292             }
293             else
294             {
295                 if (!ccl_memicmp (name, q->name, len))
296                     return q->attr_list;
297             }
298         }
299     return NULL;
300 }
301
302 const char *ccl_qual_search_special (CCL_bibset b,
303                                      const char *name)
304 {
305     struct ccl_qualifier_special *q;
306     if (!b)
307         return 0;
308     for (q = b->special; q && strcmp(q->name, name); q = q->next)
309         ;
310     if (q)
311         return q->value;
312     return 0;
313 }