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 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;
104 const char *cql_uri(void)
106 return "info:srw/cql-context-set/1/cql-v1.2";
109 struct cql_node *cql_apply_prefix(NMEM nmem,
110 struct cql_node *n, const char *prefix,
113 if (n->which == CQL_NODE_ST)
115 if (!n->u.st.index_uri && n->u.st.index)
116 { /* not yet resolved.. */
117 const char *cp = strchr(n->u.st.index, '.');
119 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
120 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
122 char *nval = nmem_strdup(nmem, cp+1);
123 n->u.st.index_uri = nmem_strdup(nmem, uri);
124 n->u.st.index = nval;
126 else if (!prefix && !cp)
128 n->u.st.index_uri = nmem_strdup(nmem, uri);
131 if (!n->u.st.relation_uri && n->u.st.relation)
133 const char *cp = strchr(n->u.st.relation, '.');
135 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
136 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
138 char *nval = nmem_strdup(nmem, cp+1);
139 n->u.st.relation_uri = nmem_strdup(nmem, uri);
140 n->u.st.relation = nval;
144 else if (n->which == CQL_NODE_BOOL)
146 cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
147 cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
149 else if (n->which == CQL_NODE_SORT)
151 cql_apply_prefix(nmem, n->u.sort.search, prefix, uri);
156 void cql_node_destroy(struct cql_node *cn)
163 cql_node_destroy(cn->u.st.modifiers);
166 cql_node_destroy(cn->u.boolean.left);
167 cql_node_destroy(cn->u.boolean.right);
168 cql_node_destroy(cn->u.boolean.modifiers);
171 cql_node_destroy(cn->u.sort.search);
172 cql_node_destroy(cn->u.sort.next);
173 cql_node_destroy(cn->u.sort.modifiers);
177 int cql_strcmp(const char *s1, const char *s2)
183 if (c1 >= 'A' && c1 <= 'Z')
184 c1 = c1 + ('a' - 'A');
185 if (c2 >= 'A' && c2 <= 'Z')
186 c2 = c2 + ('a' - 'A');
193 int cql_strncmp(const char *s1, const char *s2, size_t n)
195 while (*s1 && *s2 && n)
199 if (c1 >= 'A' && c1 <= 'Z')
200 c1 = c1 + ('a' - 'A');
201 if (c2 >= 'A' && c2 <= 'Z')
202 c2 = c2 + ('a' - 'A');
215 * c-file-style: "Stroustrup"
216 * indent-tabs-mode: nil
218 * vim: shiftwidth=4 tabstop=8 expandtab