Added functions to create CCL RPN nodes. Added small tokenizer
[yaz-moved-to-github.git] / src / cclqfile.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 cclqfile.c
46  * \brief Implements parsing of CCL qualifier specs in files
47  */
48 /* CCL qualifiers
49  * Europagate, 1995
50  *
51  * $Id: cclqfile.c,v 1.9 2007-04-26 21:45:17 adam Exp $
52  *
53  * Old Europagate Log:
54  *
55  * Revision 1.3  1995/05/16  09:39:26  adam
56  * LICENSE.
57  *
58  * Revision 1.2  1995/05/11  14:03:56  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.1  1995/04/17  09:31:45  adam
64  * Improved handling of qualifiers. Aliases or reserved words.
65  *
66  */
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71
72 #include <yaz/tokenizer.h>
73 #include <yaz/ccl.h>
74 #include <yaz/log.h>
75
76 #define MAX_QUAL 128
77
78 int ccl_qual_field2(CCL_bibset bibset, const char *cp, const char *qual_name,
79                     const char **addinfo)
80 {
81     yaz_tokenizer_t yt = yaz_tokenizer_create();
82
83     int type_ar[MAX_QUAL];
84     int value_ar[MAX_QUAL];
85     char *svalue_ar[MAX_QUAL];
86     char *attsets[MAX_QUAL];
87     int pair_no = 0;
88     char *type_str = 0;
89     int t;
90
91     yaz_tokenizer_single_tokens(yt, ",=");
92     yaz_tokenizer_read_buf(yt, cp);
93     *addinfo = 0;
94     
95     t = yaz_tokenizer_move(yt);
96     while (t == YAZ_TOKENIZER_STRING)
97     {
98         /* we don't know what lead is yet */
99         char *lead_str = xstrdup(yaz_tokenizer_string(yt));
100         const char *value_str = 0;
101         int type = 0, value = 0; /* indicates attribute value UNSET  */
102
103         t = yaz_tokenizer_move(yt);
104         if (t == ',')
105         {
106             /* full attribute spec: set, type = value */
107             /* lead is attribute set */
108             attsets[pair_no] = lead_str;
109             t = yaz_tokenizer_move(yt);
110             if (t != YAZ_TOKENIZER_STRING)
111             {
112                 *addinfo = "token expected";
113                 goto out;
114             }
115             xfree(type_str);
116             type_str = xstrdup(yaz_tokenizer_string(yt));
117             if (yaz_tokenizer_move(yt) != '=')
118             {
119                 *addinfo = "= expected";
120                 goto out;
121             }
122         }
123         else if (t == '=')
124         {
125             /* lead is attribute type */
126             /* attribute set omitted: type = value */
127             attsets[pair_no] = 0;
128             xfree(type_str);
129             type_str = lead_str;
130         }
131         else
132         {
133             /* lead is first of a list of qualifier aliaeses */
134             /* qualifier alias: q1 q2 ... */
135             xfree(lead_str);
136             yaz_tokenizer_destroy(yt);
137             ccl_qual_add_combi (bibset, qual_name, cp);
138             return 0;
139         }
140         while (1) /* comma separated attribute value list */
141         {
142             t = yaz_tokenizer_move(yt);
143             /* must have a value now */
144             if (t != YAZ_TOKENIZER_STRING)
145             {
146                 *addinfo = "value token expected";
147                 goto out;
148             }
149             value_str = yaz_tokenizer_string(yt);
150             
151             if (sscanf(type_str, "%d", &type) == 1)
152                 ;
153             else if (strlen(type_str) != 1)
154             {
155                 *addinfo = "bad attribute type";
156                 goto out;
157             }
158             else
159             {
160                 switch (*type_str)
161                 {
162                 case 'u':
163                 case 'U':
164                     type = CCL_BIB1_USE;
165                     break;
166                 case 'r':
167                 case 'R':
168                     type = CCL_BIB1_REL;
169                     if (!ccl_stricmp (value_str, "o"))
170                         value = CCL_BIB1_REL_ORDER;
171                     else if (!ccl_stricmp (value_str, "r"))
172                         value = CCL_BIB1_REL_PORDER;
173                     break;                
174                 case 'p':
175                 case 'P':
176                     type = CCL_BIB1_POS;
177                     break;
178                 case 's':
179                 case 'S':
180                     type = CCL_BIB1_STR;
181                     if (!ccl_stricmp (value_str, "pw"))
182                         value = CCL_BIB1_STR_WP;
183                     if (!ccl_stricmp (value_str, "al"))
184                         value = CCL_BIB1_STR_AND_LIST;
185                     if (!ccl_stricmp (value_str, "ol"))
186                         value = CCL_BIB1_STR_OR_LIST;
187                     break;                
188                 case 't':
189                 case 'T':
190                     type = CCL_BIB1_TRU;
191                     if (!ccl_stricmp (value_str, "l"))
192                         value = CCL_BIB1_TRU_CAN_LEFT;
193                     else if (!ccl_stricmp (value_str, "r"))
194                         value = CCL_BIB1_TRU_CAN_RIGHT;
195                     else if (!ccl_stricmp (value_str, "b"))
196                         value = CCL_BIB1_TRU_CAN_BOTH;
197                     else if (!ccl_stricmp (value_str, "n"))
198                         value = CCL_BIB1_TRU_CAN_NONE;
199                     break;                
200                 case 'c':
201                 case 'C':
202                     type = CCL_BIB1_COM;
203                     break;
204                 }
205             }
206             if (type == 0)
207             {
208                 /* type was not set in switch above */
209                 *addinfo = "bad attribute type";
210                 goto out;
211             }
212             type_ar[pair_no] = type;
213             if (value)
214             {
215                 value_ar[pair_no] = value;
216                 svalue_ar[pair_no] = 0;
217             }
218             else if (*value_str >= '0' && *value_str <= '9')
219             {
220                 value_ar[pair_no] = atoi (value_str);
221                 svalue_ar[pair_no] = 0;
222             }
223             else
224             {
225                 value_ar[pair_no] = 0;
226                 svalue_ar[pair_no] = xstrdup(value_str);
227             }
228             pair_no++;
229             if (pair_no == MAX_QUAL)
230             {
231                 *addinfo = "too many attribute values";
232                 goto out;
233             }
234             t = yaz_tokenizer_move(yt);
235             if (t != ',')
236                 break;
237             attsets[pair_no] = attsets[pair_no-1];
238         }
239     }
240  out:
241     xfree(type_str);
242     type_str = 0;
243
244     yaz_tokenizer_destroy(yt);
245
246     if (*addinfo)
247     {
248         int i;
249         for (i = 0; i<pair_no; i++)
250         {
251             xfree(attsets[i]);
252             xfree(svalue_ar[i]);
253         }
254         return -1;
255     }
256     ccl_qual_add_set(bibset, qual_name, pair_no, type_ar, value_ar, svalue_ar,
257                      attsets);
258     return 0;
259 }
260
261 void ccl_qual_field(CCL_bibset bibset, const char *cp, const char *qual_name)
262 {
263     const char *addinfo;
264     ccl_qual_field2(bibset, cp, qual_name, &addinfo);
265     if (addinfo)
266         yaz_log(YLOG_WARN, "ccl_qual_field2 fail: %s", addinfo);
267 }
268
269 void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
270 {
271     if (*qual_name == '@')
272         ccl_qual_add_special(bibset, qual_name+1, cp);
273     else
274         ccl_qual_field(bibset, cp, qual_name);
275 }
276
277 void ccl_qual_buf(CCL_bibset bibset, const char *buf)
278 {
279     const char *cp1 = buf;
280     char line[256];
281     while (1)
282     {
283         const char *cp2 = cp1;
284         int len;
285         while (*cp2 && !strchr("\r\n", *cp2))
286             cp2++;
287         len = cp2 - cp1;
288         if (len > 0)
289         {
290             if (len >= (sizeof(line)-1))
291                 len = sizeof(line)-1;
292             memcpy(line, cp1, len);
293             line[len] = '\0';
294             ccl_qual_line(bibset, line);
295         }
296         if (!*cp2)
297             break;
298         cp1 = cp2+1;
299     }
300 }
301
302 void ccl_qual_line(CCL_bibset bibset, char *line)
303 {
304     int  no_scan = 0;
305     char qual_name[128];
306     char *cp1, *cp = line;
307     
308     if (*cp == '#')
309         return;        /* ignore lines starting with # */
310     if (sscanf (cp, "%100s%n", qual_name, &no_scan) < 1)
311         return;        /* also ignore empty lines */
312     cp += no_scan;
313     cp1 = strchr(cp, '#');
314     if (cp1)
315         *cp1 = '\0';
316     ccl_qual_fitem (bibset, cp, qual_name);
317 }
318
319 /*
320  * ccl_qual_file: Read bibset definition from file.
321  * bibset:  Bibset
322  * inf:     FILE pointer.
323  *
324  * Each line format is:
325  *  <name> <t>=<v> <t>=<v> ....
326  *  Where <name> is name of qualifier;
327  *  <t>=<v> is a attribute definition pair where <t> is one of: 
328  *     u(use), r(relation), p(position), t(truncation), c(completeness) 
329  *     or plain integer.
330  *  <v> is an integer or special pseudo-value.
331  */
332 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
333 {
334     char line[256];
335
336     while (fgets (line, 255, inf))
337         ccl_qual_line(bibset, line);
338 }
339
340 int ccl_qual_fname (CCL_bibset bibset, const char *fname)
341 {
342     FILE *inf;
343     inf = fopen (fname, "r");
344     if (!inf)
345         return -1;
346     ccl_qual_file (bibset, inf);
347     fclose (inf);
348     return 0;
349 }
350 /*
351  * Local variables:
352  * c-basic-offset: 4
353  * indent-tabs-mode: nil
354  * End:
355  * vim: shiftwidth=4 tabstop=8 expandtab
356  */
357