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