index set -> context set
[yaz-moved-to-github.git] / src / 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  * $Id: cclqual.c,v 1.1 2003-10-27 12:21:30 adam Exp $
48  *
49  * Old Europagate Log:
50  *
51  * Revision 1.9  1995/05/16  09:39:27  adam
52  * LICENSE.
53  *
54  * Revision 1.8  1995/05/11  14:03:57  adam
55  * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
56  * New variable ccl_case_sensitive, which controls whether reserved
57  * words and field names are case sensitive or not.
58  *
59  * Revision 1.7  1995/04/17  09:31:46  adam
60  * Improved handling of qualifiers. Aliases or reserved words.
61  *
62  * Revision 1.6  1995/02/23  08:32:00  adam
63  * Changed header.
64  *
65  * Revision 1.4  1995/02/14  19:55:12  adam
66  * Header files ccl.h/cclp.h are gone! They have been merged an
67  * moved to ../include/ccl.h.
68  * Node kind(s) in ccl_rpn_node have changed names.
69  *
70  * Revision 1.3  1995/02/14  16:20:56  adam
71  * Qualifiers are read from a file now.
72  *
73  * Revision 1.2  1995/02/14  10:25:56  adam
74  * The constructions 'qualifier rel term ...' implemented.
75  *
76  * Revision 1.1  1995/02/13  15:15:07  adam
77  * Added handling of qualifiers. Not finished yet.
78  *
79  */
80
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84
85 #include <yaz/ccl.h>
86
87 /* Definition of CCL_bibset pointer */
88 struct ccl_qualifiers {
89     struct ccl_qualifier *list;
90     struct ccl_qualifier_special *special;
91 };
92
93
94 /* CCL Qualifier special */
95 struct ccl_qualifier_special {
96     char *name;
97     char *value;
98     struct ccl_qualifier_special *next;
99 };
100
101
102 static struct ccl_qualifier *ccl_qual_lookup (CCL_bibset b,
103                                               const char *n, size_t len)
104 {
105     struct ccl_qualifier *q;
106     for (q = b->list; q; q = q->next)
107         if (len == strlen(q->name) && !memcmp (q->name, n, len))
108             break;
109     return q;
110 }
111
112
113 void ccl_qual_add_special (CCL_bibset bibset, const char *n, const char *v)
114 {
115     struct ccl_qualifier_special *p;
116     const char *pe;
117
118     for (p = bibset->special; p && strcmp(p->name, n); p = p->next)
119         ;
120     if (p)
121         xfree (p->value);
122     else
123     {
124         p = (struct ccl_qualifier_special *) xmalloc (sizeof(*p));
125         p->name = ccl_strdup (n);
126         p->value = 0;
127         p->next = bibset->special;
128         bibset->special = p;
129     }
130     while (strchr(" \t", *v))
131         ++v;
132     for (pe = v + strlen(v); pe != v; --pe)
133         if (!strchr(" \n\r\t", pe[-1]))
134             break;
135     p->value = (char*) xmalloc (pe - v + 1);
136     if (pe - v)
137         memcpy (p->value, v, pe - v);
138     p->value[pe - v] = '\0';
139 }
140
141 static int next_token(const char **cpp, const char **dst)
142 {
143     int len = 0;
144     const char *cp = *cpp;
145     while (*cp && strchr(" \r\n\t\f", *cp))
146         cp++;
147     if (dst)
148         *dst = cp;
149     len = 0;
150     while (*cp && !strchr(" \r\n\t\f", *cp))
151     {
152         cp++;
153         len++;
154     }
155     *cpp = cp;
156     return len;
157 }
158
159 void ccl_qual_add_combi (CCL_bibset b, const char *n, const char *names)
160 {
161     const char *cp, *cp1;
162     int i, len;
163     struct ccl_qualifier *q;
164     for (q = b->list; q && strcmp(q->name, n); q = q->next)
165         ;
166     if (q)
167         return ;
168     q = (struct ccl_qualifier *) xmalloc (sizeof(*q));
169     q->name = ccl_strdup (n);
170     q->attr_list = 0;
171     q->next = b->list;
172     b->list = q;
173     
174     cp = names;
175     for (i = 0; next_token(&cp, 0); i++)
176         ;
177     q->no_sub = i;
178     q->sub = (struct ccl_qualifier **) xmalloc (sizeof(*q->sub) *
179                                                (1+q->no_sub));
180     cp = names;
181     for (i = 0; (len = next_token(&cp, &cp1)); i++)
182     {
183         q->sub[i] = ccl_qual_lookup (b, cp1, len);
184     }
185 }
186
187 /*
188  * ccl_qual_add: Add qualifier to Bibset. If qualifier already
189  *               exists, then attributes are appendend to old
190  *               definition.
191  * name:    name of qualifier
192  * no:      No of attribute type/value pairs.
193  * pairs:   Attributes. pairs[0] first type, pair[1] first value,
194  *          ... pair[2*no-2] last type, pair[2*no-1] last value.
195  */
196
197 void ccl_qual_add_set (CCL_bibset b, const char *name, int no,
198                        int *type_ar, int *value_ar, char **svalue_ar,
199                        char **attsets)
200 {
201     struct ccl_qualifier *q;
202     struct ccl_rpn_attr **attrp;
203
204     ccl_assert (b);
205     for (q = b->list; q; q = q->next)
206         if (!strcmp (name, q->name))
207             break;
208     if (!q)
209     {
210         struct ccl_qualifier *new_qual =
211             (struct ccl_qualifier *)xmalloc (sizeof(*new_qual));
212         ccl_assert (new_qual);
213         
214         new_qual->next = b->list;
215         b->list = new_qual;
216         
217         new_qual->name = ccl_strdup (name);
218         attrp = &new_qual->attr_list;
219
220         new_qual->no_sub = 0;
221         new_qual->sub = 0;
222     }
223     else
224     {
225         if (q->sub)  /* suspect.. */
226             xfree (q->sub);
227         attrp = &q->attr_list;
228         while (*attrp)
229             attrp = &(*attrp)->next;
230     }
231     while (--no >= 0)
232     {
233         struct ccl_rpn_attr *attr;
234
235         attr = (struct ccl_rpn_attr *)xmalloc (sizeof(*attr));
236         ccl_assert (attr);
237         attr->set = *attsets++;
238         attr->type = *type_ar++;
239         if (*svalue_ar)
240         {
241             attr->kind = CCL_RPN_ATTR_STRING;
242             attr->value.str = *svalue_ar;
243         }
244         else
245         {
246             attr->kind = CCL_RPN_ATTR_NUMERIC;
247             attr->value.numeric = *value_ar;
248         }
249         svalue_ar++;
250         value_ar++;
251         *attrp = attr;
252         attrp = &attr->next;
253     }
254     *attrp = NULL;
255 }
256
257 /*
258  * ccl_qual_mk: Make new (empty) bibset.
259  * return:   empty bibset.
260  */
261 CCL_bibset ccl_qual_mk (void)
262 {
263     CCL_bibset b = (CCL_bibset)xmalloc (sizeof(*b));
264     ccl_assert (b);
265     b->list = NULL;     
266     b->special = NULL;
267     return b;
268 }
269
270 /*
271  * ccl_qual_rm: Delete bibset.
272  * b:        pointer to bibset
273  */
274 void ccl_qual_rm (CCL_bibset *b)
275 {
276     struct ccl_qualifier *q, *q1;
277     struct ccl_qualifier_special *sp, *sp1;
278
279     if (!*b)
280         return;
281     for (q = (*b)->list; q; q = q1)
282     {
283         struct ccl_rpn_attr *attr, *attr1;
284
285         for (attr = q->attr_list; attr; attr = attr1)
286         {
287             attr1 = attr->next;
288             if (attr->set)
289                 xfree(attr->set);
290             if (attr->kind == CCL_RPN_ATTR_STRING)
291                 xfree(attr->value.str);
292             xfree (attr);
293         }
294         q1 = q->next;
295         xfree (q->name);
296         if (q->sub)
297             xfree (q->sub);
298         xfree (q);
299     }
300     for (sp = (*b)->special; sp; sp = sp1)
301     {
302         sp1 = sp->next;
303         xfree (sp->name);
304         xfree (sp->value);
305         xfree (sp);
306     }
307     xfree (*b);
308     *b = NULL;
309 }
310
311 /*
312  * ccl_qual_search: Search for qualifier in bibset.
313  * b:      Bibset
314  * name:   Name of qualifier to search for (need no null-termination)
315  * len:    Length of name.
316  * return: Attribute info. NULL if not found.
317  */
318 struct ccl_rpn_attr *ccl_qual_search (CCL_parser cclp,
319                                       const char *name, size_t len,
320                                       int seq)
321 {
322     struct ccl_qualifier *q;
323     const char *aliases;
324     int case_sensitive = cclp->ccl_case_sensitive;
325
326     ccl_assert (cclp);
327     if (!cclp->bibset)
328         return NULL;
329
330     aliases = ccl_qual_search_special(cclp->bibset, "case");
331     if (aliases)
332         case_sensitive = atoi(aliases);
333
334     for (q = cclp->bibset->list; q; q = q->next)
335         if (strlen(q->name) == len)
336         {
337             if (case_sensitive)
338             {
339                 if (!memcmp (name, q->name, len))
340                     break;
341             }
342             else
343             {
344                 if (!ccl_memicmp (name, q->name, len))
345                     break;
346             }
347         }
348     if (q)
349     {
350         if (q->attr_list && seq == 0)
351             return q->attr_list;
352         if (seq < q->no_sub && q->sub[seq])
353         {
354             return q->sub[seq]->attr_list;
355         }
356     }
357     return 0;
358 }
359
360 const char *ccl_qual_search_special (CCL_bibset b,
361                                      const char *name)
362 {
363     struct ccl_qualifier_special *q;
364     if (!b)
365         return 0;
366     for (q = b->special; q && strcmp(q->name, name); q = q->next)
367         ;
368     if (q)
369         return q->value;
370     return 0;
371 }