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