9d34229f4e601ccbb3434000a9d0e7d876d792ee
[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.8 2007-04-25 20:52:19 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/ccl.h>
73
74 #define MAX_QUAL 128
75
76 void ccl_qual_field (CCL_bibset bibset, const char *cp, const char *qual_name)
77 {
78     char qual_spec[128];
79     int type_ar[MAX_QUAL];
80     int value_ar[MAX_QUAL];
81     char *svalue_ar[MAX_QUAL];
82     char *attsets[MAX_QUAL];
83     int pair_no = 0;
84
85     while (pair_no < MAX_QUAL)
86     {
87         char *qual_value, *qual_type;
88         char *split, *setp;
89         int no_scan = 0;
90         
91         if (sscanf (cp, "%100s%n", qual_spec, &no_scan) < 1)
92             break;
93
94         if (!(split = strchr (qual_spec, '=')))
95         {
96             /* alias specification .. */
97             if (pair_no == 0)
98             {
99                 ccl_qual_add_combi (bibset, qual_name, cp);
100                 return;
101             }
102             break;
103         }
104         /* [set,]type=value ... */
105         cp += no_scan;
106         
107         *split++ = '\0';
108
109         setp = strchr (qual_spec, ',');
110         if (setp)
111         {
112             /* set,type=value ... */
113             *setp++ = '\0';
114             qual_type = setp;
115         }
116         else
117         {
118             /* type=value ... */
119             qual_type = qual_spec;
120         }
121         while (pair_no < MAX_QUAL)
122         {
123             int type, value;
124
125             qual_value = split;
126             if ((split = strchr (qual_value, ',')))
127                 *split++ = '\0';
128
129             value = 0;
130             switch (qual_type[0])
131             {
132             case 'u':
133             case 'U':
134                 type = CCL_BIB1_USE;
135                 break;
136             case 'r':
137             case 'R':
138                 type = CCL_BIB1_REL;
139                 if (!ccl_stricmp (qual_value, "o"))
140                     value = CCL_BIB1_REL_ORDER;
141                 else if (!ccl_stricmp (qual_value, "r"))
142                     value = CCL_BIB1_REL_PORDER;
143                 break;                
144             case 'p':
145             case 'P':
146                 type = CCL_BIB1_POS;
147                 break;
148             case 's':
149             case 'S':
150                 type = CCL_BIB1_STR;
151                 if (!ccl_stricmp (qual_value, "pw"))
152                     value = CCL_BIB1_STR_WP;
153                 if (!ccl_stricmp (qual_value, "al"))
154                     value = CCL_BIB1_STR_AND_LIST;
155                 if (!ccl_stricmp (qual_value, "ol"))
156                     value = CCL_BIB1_STR_OR_LIST;
157                 break;                
158             case 't':
159             case 'T':
160                 type = CCL_BIB1_TRU;
161                 if (!ccl_stricmp (qual_value, "l"))
162                     value = CCL_BIB1_TRU_CAN_LEFT;
163                 else if (!ccl_stricmp (qual_value, "r"))
164                     value = CCL_BIB1_TRU_CAN_RIGHT;
165                 else if (!ccl_stricmp (qual_value, "b"))
166                     value = CCL_BIB1_TRU_CAN_BOTH;
167                 else if (!ccl_stricmp (qual_value, "n"))
168                     value = CCL_BIB1_TRU_CAN_NONE;
169                 break;                
170             case 'c':
171             case 'C':
172                 type = CCL_BIB1_COM;
173                 break;
174             default:
175                 type = atoi (qual_type);
176             }
177
178             type_ar[pair_no] = type;
179
180             if (value)
181             {
182                 value_ar[pair_no] = value;
183                 svalue_ar[pair_no] = 0;
184             }
185             else if (*qual_value >= '0' && *qual_value <= '9')
186             {
187                 value_ar[pair_no] = atoi (qual_value);
188                 svalue_ar[pair_no] = 0;
189             }
190             else
191             {
192                 size_t len;
193                 if (split)
194                     len = split - qual_value;
195                 else
196                     len = strlen(qual_value);
197                 svalue_ar[pair_no] = (char *) xmalloc(len+1);
198                 memcpy(svalue_ar[pair_no], qual_value, len);
199                 svalue_ar[pair_no][len] = '\0';
200             }
201             if (setp)
202             {
203                 attsets[pair_no] = xstrdup (qual_spec);
204             }
205             else
206                 attsets[pair_no] = 0;
207             pair_no++;
208             if (!split)
209                 break;
210         }
211     }
212     ccl_qual_add_set (bibset, qual_name, pair_no, type_ar, value_ar, svalue_ar,
213                       attsets);
214 }
215
216 void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
217 {
218     if (*qual_name == '@')
219         ccl_qual_add_special(bibset, qual_name+1, cp);
220     else
221         ccl_qual_field(bibset, cp, qual_name);
222 }
223
224 void ccl_qual_buf(CCL_bibset bibset, const char *buf)
225 {
226     const char *cp1 = buf;
227     char line[256];
228     while (1)
229     {
230         const char *cp2 = cp1;
231         int len;
232         while (*cp2 && !strchr("\r\n", *cp2))
233             cp2++;
234         len = cp2 - cp1;
235         if (len > 0)
236         {
237             if (len >= (sizeof(line)-1))
238                 len = sizeof(line)-1;
239             memcpy(line, cp1, len);
240             line[len] = '\0';
241             ccl_qual_line(bibset, line);
242         }
243         if (!*cp2)
244             break;
245         cp1 = cp2+1;
246     }
247 }
248
249 void ccl_qual_line(CCL_bibset bibset, char *line)
250 {
251     int  no_scan = 0;
252     char qual_name[128];
253     char *cp1, *cp = line;
254     
255     if (*cp == '#')
256         return;        /* ignore lines starting with # */
257     if (sscanf (cp, "%100s%n", qual_name, &no_scan) < 1)
258         return;        /* also ignore empty lines */
259     cp += no_scan;
260     cp1 = strchr(cp, '#');
261     if (cp1)
262         *cp1 = '\0';
263     ccl_qual_fitem (bibset, cp, qual_name);
264 }
265
266 /*
267  * ccl_qual_file: Read bibset definition from file.
268  * bibset:  Bibset
269  * inf:     FILE pointer.
270  *
271  * Each line format is:
272  *  <name> <t>=<v> <t>=<v> ....
273  *  Where <name> is name of qualifier;
274  *  <t>=<v> is a attribute definition pair where <t> is one of: 
275  *     u(use), r(relation), p(position), t(truncation), c(completeness) 
276  *     or plain integer.
277  *  <v> is an integer or special pseudo-value.
278  */
279 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
280 {
281     char line[256];
282
283     while (fgets (line, 255, inf))
284         ccl_qual_line(bibset, line);
285 }
286
287 int ccl_qual_fname (CCL_bibset bibset, const char *fname)
288 {
289     FILE *inf;
290     inf = fopen (fname, "r");
291     if (!inf)
292         return -1;
293     ccl_qual_file (bibset, inf);
294     fclose (inf);
295     return 0;
296 }
297 /*
298  * Local variables:
299  * c-basic-offset: 4
300  * indent-tabs-mode: nil
301  * End:
302  * vim: shiftwidth=4 tabstop=8 expandtab
303  */
304