1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements parsing of CCL qualifier specs in files
14 #include <yaz/tokenizer.h>
20 int ccl_qual_field2(CCL_bibset bibset, const char *cp, const char *qual_name,
23 yaz_tok_cfg_t yt = yaz_tok_cfg_create();
25 int type_ar[MAX_QUAL];
26 int value_ar[MAX_QUAL];
27 char *svalue_ar[MAX_QUAL];
28 char *attsets[MAX_QUAL];
34 yaz_tok_cfg_single_tokens(yt, ",=");
36 tp = yaz_tok_parse_buf(yt, cp);
38 yaz_tok_cfg_destroy(yt);
42 while (t == YAZ_TOK_STRING)
44 /* we don't know what lead is yet */
45 char *lead_str = xstrdup(yaz_tok_parse_string(tp));
46 const char *value_str = 0;
47 int type = 0, value = 0; /* indicates attribute value UNSET */
52 /* full attribute spec: set, type = value */
53 /* lead is attribute set */
54 attsets[pair_no] = lead_str;
56 if (t != YAZ_TOK_STRING)
58 *addinfo = "token expected";
62 type_str = xstrdup(yaz_tok_parse_string(tp));
63 if (yaz_tok_move(tp) != '=')
65 *addinfo = "= expected";
71 /* lead is attribute type */
72 /* attribute set omitted: type = value */
79 /* lead is first of a list of qualifier aliaeses */
80 /* qualifier alias: q1 q2 ... */
84 qlist[i++] = lead_str;
86 while ((t=yaz_tok_move(tp)) == YAZ_TOK_STRING)
88 if (i < sizeof(qlist)/sizeof(*qlist)-1)
89 qlist[i++] = xstrdup(yaz_tok_parse_string(tp));
92 yaz_tok_parse_destroy(tp);
93 ccl_qual_add_combi (bibset, qual_name, (const char **) qlist);
94 for (i = 0; qlist[i]; i++)
98 while (1) /* comma separated attribute value list */
100 t = yaz_tok_move(tp);
101 /* must have a value now */
102 if (t != YAZ_TOK_STRING)
104 *addinfo = "value token expected";
107 value_str = yaz_tok_parse_string(tp);
109 if (sscanf(type_str, "%d", &type) == 1)
111 else if (strlen(type_str) != 1)
113 *addinfo = "bad attribute type";
127 if (!ccl_stricmp (value_str, "o"))
128 value = CCL_BIB1_REL_ORDER;
129 else if (!ccl_stricmp (value_str, "r"))
130 value = CCL_BIB1_REL_PORDER;
139 if (!ccl_stricmp (value_str, "pw"))
140 value = CCL_BIB1_STR_WP;
141 if (!ccl_stricmp (value_str, "al"))
142 value = CCL_BIB1_STR_AND_LIST;
143 if (!ccl_stricmp (value_str, "ol"))
144 value = CCL_BIB1_STR_OR_LIST;
149 if (!ccl_stricmp (value_str, "l"))
150 value = CCL_BIB1_TRU_CAN_LEFT;
151 else if (!ccl_stricmp (value_str, "r"))
152 value = CCL_BIB1_TRU_CAN_RIGHT;
153 else if (!ccl_stricmp (value_str, "b"))
154 value = CCL_BIB1_TRU_CAN_BOTH;
155 else if (!ccl_stricmp (value_str, "n"))
156 value = CCL_BIB1_TRU_CAN_NONE;
166 /* type was not set in switch above */
167 *addinfo = "bad attribute type";
170 type_ar[pair_no] = type;
173 value_ar[pair_no] = value;
174 svalue_ar[pair_no] = 0;
176 else if (*value_str >= '0' && *value_str <= '9')
178 value_ar[pair_no] = atoi (value_str);
179 svalue_ar[pair_no] = 0;
183 value_ar[pair_no] = 0;
184 svalue_ar[pair_no] = xstrdup(value_str);
187 if (pair_no == MAX_QUAL)
189 *addinfo = "too many attribute values";
192 t = yaz_tok_move(tp);
195 attsets[pair_no] = attsets[pair_no-1];
202 yaz_tok_parse_destroy(tp);
207 for (i = 0; i<pair_no; i++)
214 ccl_qual_add_set(bibset, qual_name, pair_no, type_ar, value_ar, svalue_ar,
219 void ccl_qual_field(CCL_bibset bibset, const char *cp, const char *qual_name)
222 ccl_qual_field2(bibset, cp, qual_name, &addinfo);
224 yaz_log(YLOG_WARN, "ccl_qual_field2 fail: %s", addinfo);
227 void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
229 if (*qual_name == '@')
230 ccl_qual_add_special(bibset, qual_name+1, cp);
232 ccl_qual_field(bibset, cp, qual_name);
235 void ccl_qual_buf(CCL_bibset bibset, const char *buf)
237 const char *cp1 = buf;
241 const char *cp2 = cp1;
243 while (*cp2 && !strchr("\r\n", *cp2))
248 if (len >= (sizeof(line)-1))
249 len = sizeof(line)-1;
250 memcpy(line, cp1, len);
252 ccl_qual_line(bibset, line);
260 void ccl_qual_line(CCL_bibset bibset, char *line)
264 char *cp1, *cp = line;
267 return; /* ignore lines starting with # */
268 if (sscanf (cp, "%100s%n", qual_name, &no_scan) < 1)
269 return; /* also ignore empty lines */
271 cp1 = strchr(cp, '#');
274 ccl_qual_fitem (bibset, cp, qual_name);
278 * ccl_qual_file: Read bibset definition from file.
282 * Each line format is:
283 * <name> <t>=<v> <t>=<v> ....
284 * Where <name> is name of qualifier;
285 * <t>=<v> is a attribute definition pair where <t> is one of:
286 * u(use), r(relation), p(position), t(truncation), c(completeness)
288 * <v> is an integer or special pseudo-value.
290 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
294 while (fgets (line, 255, inf))
295 ccl_qual_line(bibset, line);
298 int ccl_qual_fname (CCL_bibset bibset, const char *fname)
301 inf = fopen (fname, "r");
304 ccl_qual_file (bibset, inf);
311 * indent-tabs-mode: nil
313 * vim: shiftwidth=4 tabstop=8 expandtab