SRW, CQL, 2003
[yaz-moved-to-github.git] / ccl / 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 /* CCL qualifiers
45  * Europagate, 1995
46  *
47  * $Id: cclqfile.c,v 1.13 2002-06-06 12:54:24 adam Exp $
48  *
49  * Old Europagate Log:
50  *
51  * Revision 1.3  1995/05/16  09:39:26  adam
52  * LICENSE.
53  *
54  * Revision 1.2  1995/05/11  14:03:56  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.1  1995/04/17  09:31:45  adam
60  * Improved handling of qualifiers. Aliases or reserved words.
61  *
62  */
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67
68 #include <yaz/ccl.h>
69
70 void ccl_qual_field (CCL_bibset bibset, const char *cp, const char *qual_name)
71 {
72     char qual_spec[128];
73     int pair[256];
74     char *attsets[128];
75     int pair_no = 0;
76
77     while (pair_no < 128)
78     {
79         char *qual_value, *qual_type;
80         char *split, *setp;
81         int no_scan = 0;
82         
83         if (sscanf (cp, "%100s%n", qual_spec, &no_scan) < 1)
84             break;
85
86         if (!(split = strchr (qual_spec, '=')))
87         {
88             if (pair_no == 0)
89             {
90                 ccl_qual_add_combi (bibset, qual_name, cp);
91                 return;
92             }
93             break;
94         }
95         cp += no_scan;
96         
97         *split++ = '\0';
98
99         setp = strchr (qual_spec, ',');
100         if (setp)
101         {
102             *setp++ = '\0';
103             qual_type = setp;
104         }
105         else
106             qual_type = qual_spec;
107         while (pair_no < 128)
108         {
109             int type, value;
110
111             qual_value = split;
112             if ((split = strchr (qual_value, ',')))
113                 *split++ = '\0';
114             value = atoi (qual_value);
115             switch (qual_type[0])
116             {
117             case 'u':
118             case 'U':
119                 type = CCL_BIB1_USE;
120                 break;
121             case 'r':
122             case 'R':
123                 type = CCL_BIB1_REL;
124                 if (!ccl_stricmp (qual_value, "o"))
125                     value = CCL_BIB1_REL_ORDER;
126                 break;                
127             case 'p':
128             case 'P':
129                 type = CCL_BIB1_POS;
130                 break;
131             case 's':
132             case 'S':
133                 type = CCL_BIB1_STR;
134                 if (!ccl_stricmp (qual_value, "pw"))
135                     value = CCL_BIB1_STR_WP;
136                 if (!ccl_stricmp (qual_value, "al"))
137                     value = CCL_BIB1_STR_AND_LIST;
138                 if (!ccl_stricmp (qual_value, "ol"))
139                     value = CCL_BIB1_STR_OR_LIST;
140                 break;                
141             case 't':
142             case 'T':
143                 type = CCL_BIB1_TRU;
144                 if (!ccl_stricmp (qual_value, "l"))
145                     value = CCL_BIB1_TRU_CAN_LEFT;
146                 else if (!ccl_stricmp (qual_value, "r"))
147                     value = CCL_BIB1_TRU_CAN_RIGHT;
148                 else if (!ccl_stricmp (qual_value, "b"))
149                     value = CCL_BIB1_TRU_CAN_BOTH;
150                 else if (!ccl_stricmp (qual_value, "n"))
151                     value = CCL_BIB1_TRU_CAN_NONE;
152                 break;                
153             case 'c':
154             case 'C':
155                 type = CCL_BIB1_COM;
156                 break;
157             default:
158                 type = atoi (qual_type);
159             }
160             pair[pair_no*2] = type;
161             pair[pair_no*2+1] = value;
162             if (setp)
163             {
164                 attsets[pair_no] = (char*) xmalloc (strlen(qual_spec)+1);
165                 strcpy (attsets[pair_no], qual_spec);
166             }
167             else
168                 attsets[pair_no] = 0;
169             pair_no++;
170             if (!split)
171                 break;
172         }
173     }
174     ccl_qual_add_set (bibset, qual_name, pair_no, pair, attsets);
175 }
176
177 void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
178 {
179     if (*qual_name == '@')
180         ccl_qual_add_special(bibset, qual_name+1, cp);
181     else
182         ccl_qual_field(bibset, cp, qual_name);
183 }
184
185 /*
186  * ccl_qual_file: Read bibset definition from file.
187  * bibset:  Bibset
188  * inf:     FILE pointer.
189  *
190  * Each line format is:
191  *  <name> <t>=<v> <t>=<v> ....
192  *  Where <name> is name of qualifier;
193  *  <t>=<v> is a attribute definition pair where <t> is one of: 
194  *     u(use), r(relation), p(position), t(truncation), c(completeness) 
195  *     or plain integer.
196  *  <v> is an integer or special pseudo-value.
197  */
198 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
199 {
200     char line[256];
201     char *cp, *cp1;
202     char qual_name[128];
203
204     while (fgets (line, 255, inf))
205     {
206         int  no_scan = 0;
207
208         cp = line;
209         if (*cp == '#')
210             continue;        /* ignore lines starting with # */
211         if (sscanf (cp, "%100s%n", qual_name, &no_scan) < 1)
212             continue;        /* also ignore empty lines */
213         cp += no_scan;
214         cp1 = strchr(cp, '#');
215         if (cp1)
216             *cp1 = '\0';
217         ccl_qual_fitem (bibset, cp, qual_name);
218     }
219 }
220
221 int ccl_qual_fname (CCL_bibset bibset, const char *fname)
222 {
223     FILE *inf;
224     inf = fopen (fname, "r");
225     if (!inf)
226         return -1;
227     ccl_qual_file (bibset, inf);
228     fclose (inf);
229     return 0;
230 }