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