1 /* $Id: cqlutil.c,v 1.5 2004-10-03 22:34:07 adam Exp $
2 Copyright (C) 2002-2004
5 This file is part of the YAZ toolkit.
7 See the file LICENSE for details.
12 * \brief Implements CQL tree node utilities.
18 #include <yaz/xmalloc.h>
21 void cql_fputs(const char *buf, void *client_data)
23 FILE *f = (FILE *) client_data;
27 struct cql_node *cql_node_dup (struct cql_node *cp)
29 struct cql_node *cn = 0;
36 cn = cql_node_mk_sc(cp->u.st.index,
39 cn->u.st.modifiers = cql_node_dup(cp->u.st.modifiers);
40 cn->u.st.index_uri = cp->u.st.index_uri ?
41 xstrdup(cp->u.st.index_uri) : 0;
42 cn->u.st.relation_uri = cp->u.st.relation_uri ?
43 xstrdup(cp->u.st.relation_uri) : 0;
46 cn = cql_node_mk_boolean(cp->u.boolean.value);
47 cn->u.boolean.left = cql_node_dup(cp->u.boolean.left);
48 cn->u.boolean.right = cql_node_dup(cp->u.boolean.right);
53 struct cql_node *cql_node_mk_sc(const char *index,
57 struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
58 p->which = CQL_NODE_ST;
61 p->u.st.index = xstrdup(index);
62 p->u.st.index_uri = 0;
65 p->u.st.term = xstrdup(term);
68 p->u.st.relation = xstrdup(relation);
69 p->u.st.relation_uri = 0;
70 p->u.st.modifiers = 0;
74 struct cql_node *cql_node_mk_boolean(const char *op)
76 struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
77 p->which = CQL_NODE_BOOL;
78 p->u.boolean.value = 0;
80 p->u.boolean.value = xstrdup(op);
81 p->u.boolean.left = 0;
82 p->u.boolean.right = 0;
83 p->u.boolean.modifiers = 0;
89 return "info:srw/cql-context-set/1/cql-v1.1";
92 struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix,
95 if (n->which == CQL_NODE_ST)
97 if (!n->u.st.index_uri && n->u.st.index)
98 { /* not yet resolved.. */
99 const char *cp = strchr(n->u.st.index, '.');
101 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
102 !memcmp(n->u.st.index, prefix, strlen(prefix)))
104 char *nval = xstrdup(cp+1);
105 n->u.st.index_uri = xstrdup(uri);
106 xfree (n->u.st.index);
107 n->u.st.index = nval;
109 else if (!prefix && !cp)
111 n->u.st.index_uri = xstrdup(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 !memcmp(n->u.st.relation, prefix, strlen(prefix)))
121 char *nval = xstrdup(cp+1);
122 n->u.st.relation_uri = xstrdup(uri);
123 xfree (n->u.st.relation);
124 n->u.st.relation = nval;
128 else if (n->which == CQL_NODE_BOOL)
130 cql_apply_prefix(n->u.boolean.left, prefix, uri);
131 cql_apply_prefix(n->u.boolean.right, prefix, uri);
136 void cql_node_destroy(struct cql_node *cn)
143 xfree (cn->u.st.index);
144 xfree (cn->u.st.relation);
145 xfree (cn->u.st.term);
146 xfree (cn->u.st.index_uri);
147 xfree (cn->u.st.relation_uri);
148 cql_node_destroy(cn->u.st.modifiers);
151 xfree (cn->u.boolean.value);
152 cql_node_destroy(cn->u.boolean.left);
153 cql_node_destroy(cn->u.boolean.right);
154 cql_node_destroy(cn->u.boolean.modifiers);