1 /* $Id: cqlutil.c,v 1.10 2006-09-27 11:39:01 adam Exp $
2 Copyright (C) 1995-2005, 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;
74 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
76 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
77 p->which = CQL_NODE_BOOL;
78 p->u.boolean.value = 0;
80 p->u.boolean.value = nmem_strdup(nmem, op);
81 p->u.boolean.left = 0;
82 p->u.boolean.right = 0;
83 p->u.boolean.modifiers = 0;
87 const char *cql_uri(void)
89 return "info:srw/cql-context-set/1/cql-v1.1";
92 struct cql_node *cql_apply_prefix(NMEM nmem,
93 struct cql_node *n, const char *prefix,
96 if (n->which == CQL_NODE_ST)
98 if (!n->u.st.index_uri && n->u.st.index)
99 { /* not yet resolved.. */
100 const char *cp = strchr(n->u.st.index, '.');
102 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
103 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
105 char *nval = nmem_strdup(nmem, cp+1);
106 n->u.st.index_uri = nmem_strdup(nmem, uri);
107 n->u.st.index = nval;
109 else if (!prefix && !cp)
111 n->u.st.index_uri = nmem_strdup(nmem, uri);
114 if (!n->u.st.relation_uri && n->u.st.relation)
116 const char *cp = strchr(n->u.st.relation, '.');
118 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
119 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
121 char *nval = nmem_strdup(nmem, cp+1);
122 n->u.st.relation_uri = nmem_strdup(nmem, uri);
123 n->u.st.relation = nval;
127 else if (n->which == CQL_NODE_BOOL)
129 cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
130 cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
135 void cql_node_destroy(struct cql_node *cn)
142 cql_node_destroy(cn->u.st.modifiers);
145 cql_node_destroy(cn->u.boolean.left);
146 cql_node_destroy(cn->u.boolean.right);
147 cql_node_destroy(cn->u.boolean.modifiers);
151 int cql_strcmp(const char *s1, const char *s2)
157 if (c1 >= 'A' && c1 <= 'Z')
158 c1 = c1 + ('a' - 'A');
159 if (c2 >= 'A' && c2 <= 'Z')
160 c2 = c2 + ('a' - 'A');
167 int cql_strncmp(const char *s1, const char *s2, size_t n)
169 while (*s1 && *s2 && n)
173 if (c1 >= 'A' && c1 <= 'Z')
174 c1 = c1 + ('a' - 'A');
175 if (c2 >= 'A' && c2 <= 'Z')
176 c2 = c2 + ('a' - 'A');
189 * indent-tabs-mode: nil
191 * vim: shiftwidth=4 tabstop=8 expandtab