1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CCL qualifier utilities
12 #include <yaz/snprintf.h>
13 #include <yaz/tokenizer.h>
17 struct ccl_qualifier {
20 struct ccl_qualifier **sub;
21 struct ccl_rpn_attr *attr_list;
22 struct ccl_qualifier *next;
26 /** Definition of CCL_bibset pointer */
27 struct ccl_qualifiers {
28 struct ccl_qualifier *list;
29 struct ccl_qualifier_special *special;
33 /** CCL Qualifier special */
34 struct ccl_qualifier_special {
37 struct ccl_qualifier_special *next;
41 static struct ccl_qualifier *ccl_qual_lookup(CCL_bibset b,
42 const char *n, size_t len)
44 struct ccl_qualifier *q;
45 for (q = b->list; q; q = q->next)
46 if (len == strlen(q->name) && !memcmp(q->name, n, len))
51 void ccl_qual_add_special_ar(CCL_bibset bibset, const char *n,
54 struct ccl_qualifier_special *p;
55 for (p = bibset->special; p && strcmp(p->name, n); p = p->next)
62 for (i = 0; p->values[i]; i++)
63 xfree((char *) p->values[i]);
64 xfree((char **)p->values);
69 p = (struct ccl_qualifier_special *) xmalloc(sizeof(*p));
71 p->next = bibset->special;
77 void ccl_qual_add_special(CCL_bibset bibset, const char *n, const char *cp)
80 char **vlist = (char **) xmalloc(no * sizeof(*vlist));
81 yaz_tok_cfg_t yt = yaz_tok_cfg_create();
85 yaz_tok_parse_t tp = yaz_tok_parse_buf(yt, cp);
87 yaz_tok_cfg_destroy(yt);
90 while (t == YAZ_TOK_STRING)
93 vlist = (char **) xrealloc(vlist, (no = no * 2) * sizeof(*vlist));
94 vlist[i++] = xstrdup(yaz_tok_parse_string(tp));
98 ccl_qual_add_special_ar(bibset, n, (const char **) vlist);
100 yaz_tok_parse_destroy(tp);
104 /** \brief adds specifies qualifier aliases
107 \param n qualifier name
108 \param names list of qualifier aliases
110 void ccl_qual_add_combi(CCL_bibset b, const char *n, const char **names)
113 struct ccl_qualifier *q;
114 for (q = b->list; q && strcmp(q->name, n); q = q->next)
118 q = (struct ccl_qualifier *) xmalloc(sizeof(*q));
119 q->name = xstrdup(n);
124 for (i = 0; names[i]; i++)
127 q->sub = (struct ccl_qualifier **)
128 xmalloc(sizeof(*q->sub) * (1+q->no_sub));
129 for (i = 0; names[i]; i++)
130 q->sub[i] = ccl_qual_lookup(b, names[i], strlen(names[i]));
133 /** \brief adds specifies attributes for qualifier
136 \param name qualifier name
137 \param no number of attribute type+value pairs
138 \param type_ar attributes type of size no
139 \param value_ar attribute value of size no
140 \param svalue_ar attribute string values ([i] only used if != NULL)
141 \param attsets attribute sets of size no
144 void ccl_qual_add_set(CCL_bibset b, const char *name, int no,
145 int *type_ar, int *value_ar, char **svalue_ar,
148 struct ccl_qualifier *q;
149 struct ccl_rpn_attr **attrp;
152 for (q = b->list; q; q = q->next)
153 if (!strcmp(name, q->name))
157 q = (struct ccl_qualifier *)xmalloc(sizeof(*q));
163 q->name = xstrdup(name);
169 attrp = &q->attr_list;
171 attrp = &(*attrp)->next;
174 struct ccl_rpn_attr *attr;
176 attr = (struct ccl_rpn_attr *)xmalloc(sizeof(*attr));
178 attr->set = *attsets++;
179 attr->type = *type_ar++;
182 attr->kind = CCL_RPN_ATTR_STRING;
183 attr->value.str = *svalue_ar;
187 attr->kind = CCL_RPN_ATTR_NUMERIC;
188 attr->value.numeric = *value_ar;
198 /** \brief creates Bibset
201 CCL_bibset ccl_qual_mk(void)
203 CCL_bibset b = (CCL_bibset)xmalloc(sizeof(*b));
210 /** \brief destroys Bibset
211 \param b pointer to Bibset
213 *b will be set to NULL.
215 void ccl_qual_rm(CCL_bibset *b)
217 struct ccl_qualifier *q, *q1;
218 struct ccl_qualifier_special *sp, *sp1;
222 for (q = (*b)->list; q; q = q1)
224 struct ccl_rpn_attr *attr, *attr1;
226 for (attr = q->attr_list; attr; attr = attr1)
231 if (attr->kind == CCL_RPN_ATTR_STRING)
232 xfree(attr->value.str);
241 for (sp = (*b)->special; sp; sp = sp1)
248 for (i = 0; sp->values[i]; i++)
249 xfree((char*) sp->values[i]);
250 xfree((char **)sp->values);
258 ccl_qualifier_t ccl_qual_search(CCL_parser cclp, const char *name,
259 size_t name_len, int seq)
261 struct ccl_qualifier *q = 0;
262 const char **aliases;
263 int case_sensitive = cclp->ccl_case_sensitive;
269 aliases = ccl_qual_search_special(cclp->bibset, "case");
271 case_sensitive = atoi(aliases[0]);
273 for (q = cclp->bibset->list; q; q = q->next)
274 if (strlen(q->name) == name_len)
278 if (!memcmp(name, q->name, name_len))
283 if (!ccl_memicmp(name, q->name, name_len))
302 struct ccl_rpn_attr *ccl_qual_get_attr(ccl_qualifier_t q)
307 const char *ccl_qual_get_name(ccl_qualifier_t q)
312 const char **ccl_qual_search_special(CCL_bibset b, const char *name)
314 struct ccl_qualifier_special *q;
317 for (q = b->special; q && strcmp(q->name, name); q = q->next)
324 int ccl_search_stop(CCL_bibset bibset, const char *qname,
325 const char *src_str, size_t src_len)
327 const char **slist = 0;
331 yaz_snprintf(qname_buf, sizeof(qname_buf)-1, "stop.%s",
333 slist = ccl_qual_search_special(bibset, qname_buf);
336 slist = ccl_qual_search_special(bibset, "stop.*");
340 for (i = 0; slist[i]; i++)
341 if (src_len == strlen(slist[i])
342 && ccl_memicmp(slist[i], src_str, src_len) == 0)
351 * indent-tabs-mode: nil
353 * vim: shiftwidth=4 tabstop=8 expandtab