1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CQL tree node utilities.
18 void cql_fputs(const char *buf, void *client_data)
20 FILE *f = (FILE *) client_data;
24 struct cql_node *cql_node_dup(NMEM nmem, struct cql_node *cp)
26 struct cql_node *cn = 0;
33 cn = cql_node_mk_sc(nmem, cp->u.st.index,
36 cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers);
37 cn->u.st.index_uri = cp->u.st.index_uri ?
38 nmem_strdup(nmem, cp->u.st.index_uri) : 0;
39 cn->u.st.relation_uri = cp->u.st.relation_uri ?
40 nmem_strdup(nmem, cp->u.st.relation_uri) : 0;
43 cn = cql_node_mk_boolean(nmem, cp->u.boolean.value);
44 cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left);
45 cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right);
48 cn = cql_node_mk_sort(nmem, cp->u.sort.index, cp->u.sort.modifiers);
49 cn->u.sort.next = cql_node_dup(nmem, cp->u.sort.next);
50 cn->u.sort.search = cql_node_dup(nmem, cp->u.sort.search);
55 struct cql_node *cql_node_mk_sc(NMEM nmem,
60 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
61 p->which = CQL_NODE_ST;
64 p->u.st.index = nmem_strdup(nmem, index);
65 p->u.st.index_uri = 0;
68 p->u.st.term = nmem_strdup(nmem, term);
71 p->u.st.relation = nmem_strdup(nmem, relation);
72 p->u.st.relation_uri = 0;
73 p->u.st.modifiers = 0;
74 p->u.st.extra_terms = 0;
78 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
80 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
81 p->which = CQL_NODE_BOOL;
82 p->u.boolean.value = 0;
84 p->u.boolean.value = nmem_strdup(nmem, op);
85 p->u.boolean.left = 0;
86 p->u.boolean.right = 0;
87 p->u.boolean.modifiers = 0;
91 struct cql_node *cql_node_mk_sort(NMEM nmem, const char *index,
92 struct cql_node *modifiers)
94 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
95 p->which = CQL_NODE_SORT;
98 p->u.sort.index = nmem_strdup(nmem, index);
99 p->u.sort.modifiers = modifiers;
101 p->u.sort.search = 0;
105 const char *cql_uri(void)
107 return "info:srw/cql-context-set/1/cql-v1.2";
110 struct cql_node *cql_apply_prefix(NMEM nmem,
111 struct cql_node *n, const char *prefix,
114 if (n->which == CQL_NODE_ST)
116 if (!n->u.st.index_uri && n->u.st.index)
117 { /* not yet resolved.. */
118 const char *cp = strchr(n->u.st.index, '.');
120 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
121 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
123 char *nval = nmem_strdup(nmem, cp+1);
124 n->u.st.index_uri = nmem_strdup(nmem, uri);
125 n->u.st.index = nval;
127 else if (!prefix && !cp)
129 n->u.st.index_uri = nmem_strdup(nmem, uri);
132 if (!n->u.st.relation_uri && n->u.st.relation)
134 const char *cp = strchr(n->u.st.relation, '.');
136 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
137 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
139 char *nval = nmem_strdup(nmem, cp+1);
140 n->u.st.relation_uri = nmem_strdup(nmem, uri);
141 n->u.st.relation = nval;
145 else if (n->which == CQL_NODE_BOOL)
147 cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
148 cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
150 else if (n->which == CQL_NODE_SORT)
152 cql_apply_prefix(nmem, n->u.sort.search, prefix, uri);
157 void cql_node_destroy(struct cql_node *cn)
164 cql_node_destroy(cn->u.st.modifiers);
167 cql_node_destroy(cn->u.boolean.left);
168 cql_node_destroy(cn->u.boolean.right);
169 cql_node_destroy(cn->u.boolean.modifiers);
172 cql_node_destroy(cn->u.sort.search);
173 cql_node_destroy(cn->u.sort.next);
174 cql_node_destroy(cn->u.sort.modifiers);
178 int cql_strcmp(const char *s1, const char *s2)
184 if (c1 >= 'A' && c1 <= 'Z')
185 c1 = c1 + ('a' - 'A');
186 if (c2 >= 'A' && c2 <= 'Z')
187 c2 = c2 + ('a' - 'A');
194 int cql_strncmp(const char *s1, const char *s2, size_t n)
196 while (*s1 && *s2 && n)
200 if (c1 >= 'A' && c1 <= 'Z')
201 c1 = c1 + ('a' - 'A');
202 if (c2 >= 'A' && c2 <= 'Z')
203 c2 = c2 + ('a' - 'A');
216 * c-file-style: "Stroustrup"
217 * indent-tabs-mode: nil
219 * vim: shiftwidth=4 tabstop=8 expandtab