Use term Windows rather than win32
[yaz-moved-to-github.git] / src / cclqfile.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file cclqfile.c
7  * \brief Implements parsing of CCL qualifier specs in files
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <yaz/tokenizer.h>
18 #include <yaz/ccl.h>
19 #include <yaz/log.h>
20
21 #define MAX_QUAL 128
22
23 int ccl_qual_field2(CCL_bibset bibset, const char *cp, const char *qual_name,
24                     const char **addinfo)
25 {
26     yaz_tok_cfg_t yt = yaz_tok_cfg_create();
27
28     int type_ar[MAX_QUAL];
29     int value_ar[MAX_QUAL];
30     char *svalue_ar[MAX_QUAL];
31     char *attsets[MAX_QUAL];
32     int pair_no = 0;
33     char *type_str = 0;
34     int t;
35     yaz_tok_parse_t tp;
36
37     yaz_tok_cfg_single_tokens(yt, ",=");
38
39     tp = yaz_tok_parse_buf(yt, cp);
40
41     yaz_tok_cfg_destroy(yt);
42     *addinfo = 0;
43
44     t = yaz_tok_move(tp);
45     while (t == YAZ_TOK_STRING)
46     {
47         /* we don't know what lead is yet */
48         char *lead_str = xstrdup(yaz_tok_parse_string(tp));
49         const char *value_str = 0;
50         int type = 0, value = 0; /* indicates attribute value UNSET  */
51
52         t = yaz_tok_move(tp);
53         if (t == ',')
54         {
55             /* full attribute spec: set, type = value */
56             /* lead is attribute set */
57             attsets[pair_no] = lead_str;
58             t = yaz_tok_move(tp);
59             if (t != YAZ_TOK_STRING)
60             {
61                 *addinfo = "token expected";
62                 goto out;
63             }
64             xfree(type_str);
65             type_str = xstrdup(yaz_tok_parse_string(tp));
66             if (yaz_tok_move(tp) != '=')
67             {
68                 *addinfo = "= expected";
69                 goto out;
70             }
71         }
72         else if (t == '=')
73         {
74             /* lead is attribute type */
75             /* attribute set omitted: type = value */
76             attsets[pair_no] = 0;
77             xfree(type_str);
78             type_str = lead_str;
79         }
80         else
81         {
82             /* lead is first of a list of qualifier aliaeses */
83             /* qualifier alias: q1 q2 ... */
84             char *qlist[10];
85             size_t i = 0;
86
87             qlist[i++] = lead_str;
88
89             while (t == YAZ_TOK_STRING)
90             {
91                 if (i < sizeof(qlist)/sizeof(*qlist)-1)
92                     qlist[i++] = xstrdup(yaz_tok_parse_string(tp));
93                 t = yaz_tok_move(tp);
94             }
95             qlist[i] = 0;
96             yaz_tok_parse_destroy(tp);
97             ccl_qual_add_combi (bibset, qual_name, (const char **) qlist);
98             for (i = 0; qlist[i]; i++)
99                 xfree(qlist[i]);
100             return 0;
101         }
102         while (1) /* comma separated attribute value list */
103         {
104             t = yaz_tok_move(tp);
105             /* must have a value now */
106             if (t != YAZ_TOK_STRING)
107             {
108                 *addinfo = "value token expected";
109                 goto out;
110             }
111             value_str = yaz_tok_parse_string(tp);
112
113             if (sscanf(type_str, "%d", &type) == 1)
114                 ;
115             else if (strlen(type_str) != 1)
116             {
117                 *addinfo = "bad attribute type";
118                 goto out;
119             }
120             else
121             {
122                 switch (*type_str)
123                 {
124                 case 'u':
125                 case 'U':
126                     type = CCL_BIB1_USE;
127                     break;
128                 case 'r':
129                 case 'R':
130                     type = CCL_BIB1_REL;
131                     if (!ccl_stricmp (value_str, "o"))
132                         value = CCL_BIB1_REL_ORDER;
133                     else if (!ccl_stricmp (value_str, "r"))
134                         value = CCL_BIB1_REL_PORDER;
135                     else if (!ccl_stricmp (value_str, "omiteq"))
136                         value = CCL_BIB1_REL_OMIT_EQUALS;
137                     break;
138                 case 'p':
139                 case 'P':
140                     type = CCL_BIB1_POS;
141                     break;
142                 case 's':
143                 case 'S':
144                     type = CCL_BIB1_STR;
145                     if (!ccl_stricmp (value_str, "pw"))
146                         value = CCL_BIB1_STR_WP;
147                     if (!ccl_stricmp (value_str, "al"))
148                         value = CCL_BIB1_STR_AND_LIST;
149                     if (!ccl_stricmp (value_str, "ol"))
150                         value = CCL_BIB1_STR_OR_LIST;
151                     if (!ccl_stricmp (value_str, "ag"))
152                         value = CCL_BIB1_STR_AUTO_GROUP;
153                     break;
154                 case 't':
155                 case 'T':
156                     type = CCL_BIB1_TRU;
157                     if (!ccl_stricmp (value_str, "l"))
158                         value = CCL_BIB1_TRU_CAN_LEFT;
159                     else if (!ccl_stricmp (value_str, "r"))
160                         value = CCL_BIB1_TRU_CAN_RIGHT;
161                     else if (!ccl_stricmp (value_str, "b"))
162                         value = CCL_BIB1_TRU_CAN_BOTH;
163                     else if (!ccl_stricmp (value_str, "n"))
164                         value = CCL_BIB1_TRU_CAN_NONE;
165                     else if (!ccl_stricmp (value_str, "x"))
166                         value = CCL_BIB1_TRU_CAN_REGEX;
167                     else if (!ccl_stricmp (value_str, "z"))
168                         value = CCL_BIB1_TRU_CAN_Z3958;
169                     break;
170                 case 'c':
171                 case 'C':
172                     type = CCL_BIB1_COM;
173                     break;
174                 }
175             }
176             if (type == 0)
177             {
178                 /* type was not set in switch above */
179                 *addinfo = "bad attribute type";
180                 goto out;
181             }
182             type_ar[pair_no] = type;
183             if (value)
184             {
185                 value_ar[pair_no] = value;
186                 svalue_ar[pair_no] = 0;
187             }
188             else if (*value_str >= '0' && *value_str <= '9')
189             {
190                 value_ar[pair_no] = atoi (value_str);
191                 svalue_ar[pair_no] = 0;
192             }
193             else
194             {
195                 value_ar[pair_no] = 0;
196                 svalue_ar[pair_no] = xstrdup(value_str);
197             }
198             pair_no++;
199             if (pair_no == MAX_QUAL)
200             {
201                 *addinfo = "too many attribute values";
202                 goto out;
203             }
204             t = yaz_tok_move(tp);
205             if (t != ',')
206                 break;
207             attsets[pair_no] = attsets[pair_no-1];
208         }
209     }
210  out:
211     xfree(type_str);
212     type_str = 0;
213
214     yaz_tok_parse_destroy(tp);
215
216     if (*addinfo)
217     {
218         int i;
219         for (i = 0; i<pair_no; i++)
220         {
221             xfree(attsets[i]);
222             xfree(svalue_ar[i]);
223         }
224         return -1;
225     }
226     ccl_qual_add_set(bibset, qual_name, pair_no, type_ar, value_ar, svalue_ar,
227                      attsets);
228     return 0;
229 }
230
231 void ccl_qual_field(CCL_bibset bibset, const char *cp, const char *qual_name)
232 {
233     const char *addinfo;
234     ccl_qual_field2(bibset, cp, qual_name, &addinfo);
235     if (addinfo)
236         yaz_log(YLOG_WARN, "ccl_qual_field2 fail: %s", addinfo);
237 }
238
239 int ccl_qual_fitem2(CCL_bibset bibset, const char *cp, const char *qual_name,
240                     const char **addinfo)
241 {
242     if (*qual_name == '@')
243     {
244         /* ccl_qual_add_special can not return error (yet) */
245         ccl_qual_add_special(bibset, qual_name+1, cp);
246         *addinfo = 0;
247         return 0;
248     }
249     else
250         return ccl_qual_field2(bibset, cp, qual_name, addinfo);
251 }
252
253 void ccl_qual_fitem(CCL_bibset bibset, const char *cp, const char *qual_name)
254 {
255     const char *addinfo = 0;
256     ccl_qual_fitem2(bibset, cp, qual_name, &addinfo);
257 }
258
259 void ccl_qual_buf(CCL_bibset bibset, const char *buf)
260 {
261     const char *cp1 = buf;
262     char line[256];
263     while (1)
264     {
265         const char *cp2 = cp1;
266         size_t len;
267         while (*cp2 && !strchr("\r\n", *cp2))
268             cp2++;
269         len = cp2 - cp1;
270         if (len > 0)
271         {
272             if (len >= (sizeof(line)-1))
273                 len = sizeof(line)-1;
274             memcpy(line, cp1, len);
275             line[len] = '\0';
276             ccl_qual_line(bibset, line);
277         }
278         if (!*cp2)
279             break;
280         cp1 = cp2+1;
281     }
282 }
283
284 void ccl_qual_line(CCL_bibset bibset, char *line)
285 {
286     int  no_scan = 0;
287     char qual_name[128];
288     char *cp1, *cp = line;
289
290     if (*cp == '#')
291         return;        /* ignore lines starting with # */
292     if (sscanf (cp, "%100s%n", qual_name, &no_scan) < 1)
293         return;        /* also ignore empty lines */
294     cp += no_scan;
295     cp1 = strchr(cp, '#');
296     if (cp1)
297         *cp1 = '\0';
298     ccl_qual_fitem (bibset, cp, qual_name);
299 }
300
301 /*
302  * ccl_qual_file: Read bibset definition from file.
303  * bibset:  Bibset
304  * inf:     FILE pointer.
305  *
306  * Each line format is:
307  *  <name> <t>=<v> <t>=<v> ....
308  *  Where <name> is name of qualifier;
309  *  <t>=<v> is a attribute definition pair where <t> is one of:
310  *     u(use), r(relation), p(position), t(truncation), c(completeness)
311  *     or plain integer.
312  *  <v> is an integer or special pseudo-value.
313  */
314 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
315 {
316     char line[256];
317
318     while (fgets (line, 255, inf))
319         ccl_qual_line(bibset, line);
320 }
321
322 int ccl_qual_fname (CCL_bibset bibset, const char *fname)
323 {
324     FILE *inf;
325     inf = fopen (fname, "r");
326     if (!inf)
327         return -1;
328     ccl_qual_file (bibset, inf);
329     fclose (inf);
330     return 0;
331 }
332 /*
333  * Local variables:
334  * c-basic-offset: 4
335  * c-file-style: "Stroustrup"
336  * indent-tabs-mode: nil
337  * End:
338  * vim: shiftwidth=4 tabstop=8 expandtab
339  */
340