Implemented local AttributeSet setting for CCL field maps.
[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.14  2000-11-16 09:58:02  adam
49  * Implemented local AttributeSet setting for CCL field maps.
50  *
51  * Revision 1.13  2000/01/31 13:15:21  adam
52  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
53  * that some characters are not surrounded by spaces in resulting term.
54  * ILL-code updates.
55  *
56  * Revision 1.12  1999/11/30 13:47:11  adam
57  * Improved installation. Moved header files to include/yaz.
58  *
59  * Revision 1.11  1999/03/31 11:15:37  adam
60  * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
61  *
62  * Revision 1.10  1998/07/07 15:49:40  adam
63  * Added braces to avoid warning.
64  *
65  * Revision 1.9  1998/02/11 11:53:33  adam
66  * Changed code so that it compiles as C++.
67  *
68  * Revision 1.8  1997/09/29 08:56:38  adam
69  * Changed CCL parser to be thread safe. New type, CCL_parser, declared
70  * and a create/destructers ccl_parser_create/ccl_parser/destory has
71  * been added.
72  *
73  * Revision 1.7  1997/09/01 08:48:12  adam
74  * New windows NT/95 port using MSV5.0. Only a few changes made
75  * to avoid warnings.
76  *
77  * Revision 1.6  1997/04/30 08:52:07  quinn
78  * Null
79  *
80  * Revision 1.5  1996/10/11  15:00:25  adam
81  * CCL parser from Europagate Email gateway 1.0.
82  *
83  * Revision 1.9  1995/05/16  09:39:27  adam
84  * LICENSE.
85  *
86  * Revision 1.8  1995/05/11  14:03:57  adam
87  * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
88  * New variable ccl_case_sensitive, which controls whether reserved
89  * words and field names are case sensitive or not.
90  *
91  * Revision 1.7  1995/04/17  09:31:46  adam
92  * Improved handling of qualifiers. Aliases or reserved words.
93  *
94  * Revision 1.6  1995/02/23  08:32:00  adam
95  * Changed header.
96  *
97  * Revision 1.4  1995/02/14  19:55:12  adam
98  * Header files ccl.h/cclp.h are gone! They have been merged an
99  * moved to ../include/ccl.h.
100  * Node kind(s) in ccl_rpn_node have changed names.
101  *
102  * Revision 1.3  1995/02/14  16:20:56  adam
103  * Qualifiers are read from a file now.
104  *
105  * Revision 1.2  1995/02/14  10:25:56  adam
106  * The constructions 'qualifier rel term ...' implemented.
107  *
108  * Revision 1.1  1995/02/13  15:15:07  adam
109  * Added handling of qualifiers. Not finished yet.
110  *
111  */
112
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <string.h>
116
117 #include <yaz/ccl.h>
118
119 /* Definition of CCL_bibset pointer */
120 struct ccl_qualifiers {
121     struct ccl_qualifier *list;
122 };
123
124 /*
125  * ccl_qual_add: Add qualifier to Bibset. If qualifier already
126  *               exists, then attributes are appendend to old
127  *               definition.
128  * name:    name of qualifier
129  * no:      No of attribute type/value pairs.
130  * pairs:   Attributes. pairs[0] first type, pair[1] first value,
131  *          ... pair[2*no-2] last type, pair[2*no-1] last value.
132  */
133 void ccl_qual_add_set (CCL_bibset b, const char *name, int no, int *pairs,
134                        char **attsets)
135 {
136     struct ccl_qualifier *q;
137     struct ccl_rpn_attr **attrp;
138
139     ccl_assert (b);
140     for (q = b->list; q; q = q->next)
141         if (!strcmp (name, q->name))
142             break;
143     if (!q)
144     {
145         struct ccl_qualifier *new_qual =
146             (struct ccl_qualifier *)malloc (sizeof(*new_qual));
147         ccl_assert (new_qual);
148         
149         new_qual->next = b->list;
150         b->list = new_qual;
151         
152         new_qual->name = (char *)malloc (strlen(name)+1);
153         ccl_assert (new_qual->name);
154         strcpy (new_qual->name, name);
155         attrp = &new_qual->attr_list;
156     }
157     else
158     {
159         attrp = &q->attr_list;
160         while (*attrp)
161             attrp = &(*attrp)->next;
162     }
163     while (--no >= 0)
164     {
165         struct ccl_rpn_attr *attr;
166
167         attr = (struct ccl_rpn_attr *)malloc (sizeof(*attr));
168         ccl_assert (attr);
169         attr->set = *attsets++;
170         attr->type = *pairs++;
171         attr->value = *pairs++;
172         *attrp = attr;
173         attrp = &attr->next;
174     }
175     *attrp = NULL;
176 }
177
178 /*
179  * ccl_qual_mk: Make new (empty) bibset.
180  * return:   empty bibset.
181  */
182 CCL_bibset ccl_qual_mk (void)
183 {
184     CCL_bibset b = (CCL_bibset)malloc (sizeof(*b));
185     ccl_assert (b);
186     b->list = NULL;     
187     return b;
188 }
189
190 /*
191  * ccl_qual_rm: Delete bibset.
192  * b:        pointer to bibset
193  */
194 void ccl_qual_rm (CCL_bibset *b)
195 {
196     struct ccl_qualifier *q, *q1;
197
198     if (!*b)
199         return;
200     for (q = (*b)->list; q; q = q1)
201     {
202         struct ccl_rpn_attr *attr, *attr1;
203
204         for (attr = q->attr_list; attr; attr = attr1)
205         {
206             attr1 = attr->next;
207             if (attr->set)
208                 free (attr->set);
209             free (attr);
210         }
211         q1 = q->next;
212         free (q->name);
213         free (q);
214     }
215     free (*b);
216     *b = NULL;
217 }
218
219 /*
220  * ccl_qual_search: Search for qualifier in bibset.
221  * b:      Bibset
222  * name:   Name of qualifier to search for (need no null-termination)
223  * len:    Length of name.
224  * return: Attribute info. NULL if not found.
225  */
226 struct ccl_rpn_attr *ccl_qual_search (CCL_parser cclp,
227                                       const char *name, size_t len)
228 {
229     struct ccl_qualifier *q;
230
231     ccl_assert (cclp);
232     if (!cclp->bibset)
233         return NULL;
234     for (q = cclp->bibset->list; q; q = q->next)
235         if (strlen(q->name) == len)
236         {
237             if (cclp->ccl_case_sensitive)
238             {
239                 if (!memcmp (name, q->name, len))
240                     return q->attr_list;
241             }
242             else
243             {
244                 if (!ccl_memicmp (name, q->name, len))
245                     return q->attr_list;
246             }
247         }
248     return NULL;
249 }
250