1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CCL qualifier utilities
16 #include <yaz/snprintf.h>
17 #include <yaz/tokenizer.h>
21 struct ccl_qualifier {
24 struct ccl_qualifier **sub;
25 struct ccl_rpn_attr *attr_list;
26 struct ccl_qualifier *next;
30 /** Definition of CCL_bibset pointer */
31 struct ccl_qualifiers {
32 struct ccl_qualifier *list;
33 struct ccl_qualifier_special *special;
37 /** CCL Qualifier special */
38 struct ccl_qualifier_special {
41 struct ccl_qualifier_special *next;
45 static struct ccl_qualifier *ccl_qual_lookup(CCL_bibset b,
46 const char *n, size_t len)
48 struct ccl_qualifier *q;
49 for (q = b->list; q; q = q->next)
50 if (len == strlen(q->name) && !memcmp(q->name, n, len))
55 void ccl_qual_add_special_ar(CCL_bibset bibset, const char *n,
58 struct ccl_qualifier_special *p;
59 for (p = bibset->special; p && strcmp(p->name, n); p = p->next)
66 for (i = 0; p->values[i]; i++)
67 xfree((char *) p->values[i]);
68 xfree((char **)p->values);
73 p = (struct ccl_qualifier_special *) xmalloc(sizeof(*p));
75 p->next = bibset->special;
81 void ccl_qual_add_special(CCL_bibset bibset, const char *n, const char *cp)
84 char **vlist = (char **) xmalloc(no * sizeof(*vlist));
85 yaz_tok_cfg_t yt = yaz_tok_cfg_create();
89 yaz_tok_parse_t tp = yaz_tok_parse_buf(yt, cp);
91 yaz_tok_cfg_destroy(yt);
94 while (t == YAZ_TOK_STRING)
97 vlist = (char **) xrealloc(vlist, (no = no * 2) * sizeof(*vlist));
98 vlist[i++] = xstrdup(yaz_tok_parse_string(tp));
102 ccl_qual_add_special_ar(bibset, n, (const char **) vlist);
104 yaz_tok_parse_destroy(tp);
108 /** \brief adds specifies qualifier aliases
111 \param n qualifier name
112 \param names list of qualifier aliases
114 void ccl_qual_add_combi(CCL_bibset b, const char *n, const char **names)
117 struct ccl_qualifier *q;
118 for (q = b->list; q && strcmp(q->name, n); q = q->next)
122 q = (struct ccl_qualifier *) xmalloc(sizeof(*q));
123 q->name = xstrdup(n);
128 for (i = 0; names[i]; i++)
131 q->sub = (struct ccl_qualifier **)
132 xmalloc(sizeof(*q->sub) * (1+q->no_sub));
133 for (i = 0; names[i]; i++)
134 q->sub[i] = ccl_qual_lookup(b, names[i], strlen(names[i]));
137 /** \brief adds specifies attributes for qualifier
140 \param name qualifier name
141 \param no number of attribute type+value pairs
142 \param type_ar attributes type of size no
143 \param value_ar attribute value of size no
144 \param svalue_ar attribute string values ([i] only used if != NULL)
145 \param attsets attribute sets of size no
148 void ccl_qual_add_set(CCL_bibset b, const char *name, int no,
149 int *type_ar, int *value_ar, char **svalue_ar,
152 struct ccl_qualifier *q;
153 struct ccl_rpn_attr **attrp;
156 for (q = b->list; q; q = q->next)
157 if (!strcmp(name, q->name))
161 q = (struct ccl_qualifier *)xmalloc(sizeof(*q));
167 q->name = xstrdup(name);
173 attrp = &q->attr_list;
175 attrp = &(*attrp)->next;
178 struct ccl_rpn_attr *attr;
180 attr = (struct ccl_rpn_attr *)xmalloc(sizeof(*attr));
182 attr->set = *attsets++;
183 attr->type = *type_ar++;
186 attr->kind = CCL_RPN_ATTR_STRING;
187 attr->value.str = *svalue_ar;
191 attr->kind = CCL_RPN_ATTR_NUMERIC;
192 attr->value.numeric = *value_ar;
202 /** \brief creates Bibset
205 CCL_bibset ccl_qual_mk(void)
207 CCL_bibset b = (CCL_bibset)xmalloc(sizeof(*b));
214 /** \brief destroys Bibset
215 \param b pointer to Bibset
217 *b will be set to NULL.
219 void ccl_qual_rm(CCL_bibset *b)
221 struct ccl_qualifier *q, *q1;
222 struct ccl_qualifier_special *sp, *sp1;
226 for (q = (*b)->list; q; q = q1)
228 struct ccl_rpn_attr *attr, *attr1;
230 for (attr = q->attr_list; attr; attr = attr1)
235 if (attr->kind == CCL_RPN_ATTR_STRING)
236 xfree(attr->value.str);
245 for (sp = (*b)->special; sp; sp = sp1)
252 for (i = 0; sp->values[i]; i++)
253 xfree((char*) sp->values[i]);
254 xfree((char **)sp->values);
262 ccl_qualifier_t ccl_qual_search(CCL_parser cclp, const char *name,
263 size_t name_len, int seq)
265 struct ccl_qualifier *q = 0;
266 const char **aliases;
267 int case_sensitive = cclp->ccl_case_sensitive;
273 aliases = ccl_qual_search_special(cclp->bibset, "case");
275 case_sensitive = atoi(aliases[0]);
277 for (q = cclp->bibset->list; q; q = q->next)
278 if (strlen(q->name) == name_len)
282 if (!memcmp(name, q->name, name_len))
287 if (!ccl_memicmp(name, q->name, name_len))
306 struct ccl_rpn_attr *ccl_qual_get_attr(ccl_qualifier_t q)
311 const char *ccl_qual_get_name(ccl_qualifier_t q)
316 const char **ccl_qual_search_special(CCL_bibset b, const char *name)
318 struct ccl_qualifier_special *q;
321 for (q = b->special; q && strcmp(q->name, name); q = q->next)
328 int ccl_search_stop(CCL_bibset bibset, const char *qname,
329 const char *src_str, size_t src_len)
331 const char **slist = 0;
335 yaz_snprintf(qname_buf, sizeof(qname_buf)-1, "stop.%s",
337 slist = ccl_qual_search_special(bibset, qname_buf);
340 slist = ccl_qual_search_special(bibset, "stop.*");
344 for (i = 0; slist[i]; i++)
345 if (src_len == strlen(slist[i])
346 && ccl_memicmp(slist[i], src_str, src_len) == 0)
355 * c-file-style: "Stroustrup"
356 * indent-tabs-mode: nil
358 * vim: shiftwidth=4 tabstop=8 expandtab