1 /* $Id: cqlutil.c,v 1.13 2008-01-06 16:22:02 adam Exp $
2 Copyright (C) 1995-2007, Index Data ApS
5 This file is part of the YAZ toolkit.
7 See the file LICENSE for details.
12 * \brief Implements CQL tree node utilities.
20 void cql_fputs(const char *buf, void *client_data)
22 FILE *f = (FILE *) client_data;
26 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp)
28 struct cql_node *cn = 0;
35 cn = cql_node_mk_sc(nmem, cp->u.st.index,
38 cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers);
39 cn->u.st.index_uri = cp->u.st.index_uri ?
40 nmem_strdup(nmem, cp->u.st.index_uri) : 0;
41 cn->u.st.relation_uri = cp->u.st.relation_uri ?
42 nmem_strdup(nmem, cp->u.st.relation_uri) : 0;
45 cn = cql_node_mk_boolean(nmem, cp->u.boolean.value);
46 cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left);
47 cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right);
52 struct cql_node *cql_node_mk_sc(NMEM nmem,
57 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
58 p->which = CQL_NODE_ST;
61 p->u.st.index = nmem_strdup(nmem, index);
62 p->u.st.index_uri = 0;
65 p->u.st.term = nmem_strdup(nmem, term);
68 p->u.st.relation = nmem_strdup(nmem, relation);
69 p->u.st.relation_uri = 0;
70 p->u.st.modifiers = 0;
71 p->u.st.extra_terms = 0;
75 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
77 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
78 p->which = CQL_NODE_BOOL;
79 p->u.boolean.value = 0;
81 p->u.boolean.value = nmem_strdup(nmem, op);
82 p->u.boolean.left = 0;
83 p->u.boolean.right = 0;
84 p->u.boolean.modifiers = 0;
88 const char *cql_uri(void)
90 return "info:srw/cql-context-set/1/cql-v1.2";
93 struct cql_node *cql_apply_prefix(NMEM nmem,
94 struct cql_node *n, const char *prefix,
97 if (n->which == CQL_NODE_ST)
99 if (!n->u.st.index_uri && n->u.st.index)
100 { /* not yet resolved.. */
101 const char *cp = strchr(n->u.st.index, '.');
103 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
104 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
106 char *nval = nmem_strdup(nmem, cp+1);
107 n->u.st.index_uri = nmem_strdup(nmem, uri);
108 n->u.st.index = nval;
110 else if (!prefix && !cp)
112 n->u.st.index_uri = nmem_strdup(nmem, uri);
115 if (!n->u.st.relation_uri && n->u.st.relation)
117 const char *cp = strchr(n->u.st.relation, '.');
119 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
120 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
122 char *nval = nmem_strdup(nmem, cp+1);
123 n->u.st.relation_uri = nmem_strdup(nmem, uri);
124 n->u.st.relation = nval;
128 else if (n->which == CQL_NODE_BOOL)
130 cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
131 cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
136 void cql_node_destroy(struct cql_node *cn)
143 cql_node_destroy(cn->u.st.modifiers);
146 cql_node_destroy(cn->u.boolean.left);
147 cql_node_destroy(cn->u.boolean.right);
148 cql_node_destroy(cn->u.boolean.modifiers);
152 int cql_strcmp(const char *s1, const char *s2)
158 if (c1 >= 'A' && c1 <= 'Z')
159 c1 = c1 + ('a' - 'A');
160 if (c2 >= 'A' && c2 <= 'Z')
161 c2 = c2 + ('a' - 'A');
168 int cql_strncmp(const char *s1, const char *s2, size_t n)
170 while (*s1 && *s2 && n)
174 if (c1 >= 'A' && c1 <= 'Z')
175 c1 = c1 + ('a' - 'A');
176 if (c2 >= 'A' && c2 <= 'Z')
177 c2 = c2 + ('a' - 'A');
190 * indent-tabs-mode: nil
192 * vim: shiftwidth=4 tabstop=8 expandtab