2 * Copyright (C) 1995-2008, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: cclqual.c,v 1.13 2008-01-09 21:32:28 adam Exp $
9 * \brief Implements CCL qualifier utilities
14 #include <yaz/snprintf.h>
15 #include <yaz/tokenizer.h>
19 struct ccl_qualifier {
22 struct ccl_qualifier **sub;
23 struct ccl_rpn_attr *attr_list;
24 struct ccl_qualifier *next;
28 /** Definition of CCL_bibset pointer */
29 struct ccl_qualifiers {
30 struct ccl_qualifier *list;
31 struct ccl_qualifier_special *special;
35 /** CCL Qualifier special */
36 struct ccl_qualifier_special {
39 struct ccl_qualifier_special *next;
43 static struct ccl_qualifier *ccl_qual_lookup(CCL_bibset b,
44 const char *n, size_t len)
46 struct ccl_qualifier *q;
47 for (q = b->list; q; q = q->next)
48 if (len == strlen(q->name) && !memcmp(q->name, n, len))
53 void ccl_qual_add_special_ar(CCL_bibset bibset, const char *n,
56 struct ccl_qualifier_special *p;
57 for (p = bibset->special; p && strcmp(p->name, n); p = p->next)
64 for (i = 0; p->values[i]; i++)
65 xfree((char *) p->values[i]);
66 xfree((char **)p->values);
71 p = (struct ccl_qualifier_special *) xmalloc(sizeof(*p));
73 p->next = bibset->special;
79 void ccl_qual_add_special(CCL_bibset bibset, const char *n, const char *cp)
82 char **vlist = (char **) xmalloc(no * sizeof(*vlist));
83 yaz_tok_cfg_t yt = yaz_tok_cfg_create();
87 yaz_tok_parse_t tp = yaz_tok_parse_buf(yt, cp);
89 yaz_tok_cfg_destroy(yt);
92 while (t == YAZ_TOK_STRING)
95 vlist = (char **) xrealloc(vlist, (no = no * 2) * sizeof(*vlist));
96 vlist[i++] = xstrdup(yaz_tok_parse_string(tp));
100 ccl_qual_add_special_ar(bibset, n, (const char **) vlist);
102 yaz_tok_parse_destroy(tp);
106 /** \brief adds specifies qualifier aliases
109 \param n qualifier name
110 \param names list of qualifier aliases
112 void ccl_qual_add_combi(CCL_bibset b, const char *n, const char **names)
115 struct ccl_qualifier *q;
116 for (q = b->list; q && strcmp(q->name, n); q = q->next)
120 q = (struct ccl_qualifier *) xmalloc(sizeof(*q));
121 q->name = xstrdup(n);
126 for (i = 0; names[i]; i++)
129 q->sub = (struct ccl_qualifier **)
130 xmalloc(sizeof(*q->sub) * (1+q->no_sub));
131 for (i = 0; names[i]; i++)
132 q->sub[i] = ccl_qual_lookup(b, names[i], strlen(names[i]));
135 /** \brief adds specifies attributes for qualifier
138 \param name qualifier name
139 \param no number of attribute type+value pairs
140 \param type_ar attributes type of size no
141 \param value_ar attribute value of size no
142 \param svalue_ar attribute string values ([i] only used if != NULL)
143 \param attsets attribute sets of size no
146 void ccl_qual_add_set(CCL_bibset b, const char *name, int no,
147 int *type_ar, int *value_ar, char **svalue_ar,
150 struct ccl_qualifier *q;
151 struct ccl_rpn_attr **attrp;
154 for (q = b->list; q; q = q->next)
155 if (!strcmp(name, q->name))
159 q = (struct ccl_qualifier *)xmalloc(sizeof(*q));
165 q->name = xstrdup(name);
171 attrp = &q->attr_list;
173 attrp = &(*attrp)->next;
176 struct ccl_rpn_attr *attr;
178 attr = (struct ccl_rpn_attr *)xmalloc(sizeof(*attr));
180 attr->set = *attsets++;
181 attr->type = *type_ar++;
184 attr->kind = CCL_RPN_ATTR_STRING;
185 attr->value.str = *svalue_ar;
189 attr->kind = CCL_RPN_ATTR_NUMERIC;
190 attr->value.numeric = *value_ar;
200 /** \brief creates Bibset
203 CCL_bibset ccl_qual_mk(void)
205 CCL_bibset b = (CCL_bibset)xmalloc(sizeof(*b));
212 /** \brief destroys Bibset
213 \param b pointer to Bibset
215 *b will be set to NULL.
217 void ccl_qual_rm(CCL_bibset *b)
219 struct ccl_qualifier *q, *q1;
220 struct ccl_qualifier_special *sp, *sp1;
224 for (q = (*b)->list; q; q = q1)
226 struct ccl_rpn_attr *attr, *attr1;
228 for (attr = q->attr_list; attr; attr = attr1)
233 if (attr->kind == CCL_RPN_ATTR_STRING)
234 xfree(attr->value.str);
243 for (sp = (*b)->special; sp; sp = sp1)
250 for (i = 0; sp->values[i]; i++)
251 xfree((char*) sp->values[i]);
252 xfree((char **)sp->values);
260 ccl_qualifier_t ccl_qual_search(CCL_parser cclp, const char *name,
261 size_t name_len, int seq)
263 struct ccl_qualifier *q = 0;
264 const char **aliases;
265 int case_sensitive = cclp->ccl_case_sensitive;
271 aliases = ccl_qual_search_special(cclp->bibset, "case");
273 case_sensitive = atoi(aliases[0]);
275 for (q = cclp->bibset->list; q; q = q->next)
276 if (strlen(q->name) == name_len)
280 if (!memcmp(name, q->name, name_len))
285 if (!ccl_memicmp(name, q->name, name_len))
304 struct ccl_rpn_attr *ccl_qual_get_attr(ccl_qualifier_t q)
309 const char *ccl_qual_get_name(ccl_qualifier_t q)
314 const char **ccl_qual_search_special(CCL_bibset b, const char *name)
316 struct ccl_qualifier_special *q;
319 for (q = b->special; q && strcmp(q->name, name); q = q->next)
326 int ccl_search_stop(CCL_bibset bibset, const char *qname,
327 const char *src_str, size_t src_len)
329 const char **slist = 0;
333 yaz_snprintf(qname_buf, sizeof(qname_buf)-1, "stop.%s",
335 slist = ccl_qual_search_special(bibset, qname_buf);
338 slist = ccl_qual_search_special(bibset, "stop.*");
342 for (i = 0; slist[i]; i++)
343 if (src_len == strlen(slist[i])
344 && ccl_memicmp(slist[i], src_str, src_len) == 0)
353 * indent-tabs-mode: nil
355 * vim: shiftwidth=4 tabstop=8 expandtab