1 /* $Id: cqlutil.c,v 1.4 2004-03-16 13:22:16 adam Exp $
2 Copyright (C) 2002-2004
5 This file is part of the YAZ toolkit.
13 #include <yaz/xmalloc.h>
16 void cql_fputs(const char *buf, void *client_data)
18 FILE *f = (FILE *) client_data;
22 struct cql_node *cql_node_dup (struct cql_node *cp)
24 struct cql_node *cn = 0;
31 cn = cql_node_mk_sc(cp->u.st.index,
34 cn->u.st.modifiers = cql_node_dup(cp->u.st.modifiers);
35 cn->u.st.index_uri = cp->u.st.index_uri ?
36 xstrdup(cp->u.st.index_uri) : 0;
37 cn->u.st.relation_uri = cp->u.st.relation_uri ?
38 xstrdup(cp->u.st.relation_uri) : 0;
41 cn = cql_node_mk_boolean(cp->u.boolean.value);
42 cn->u.boolean.left = cql_node_dup(cp->u.boolean.left);
43 cn->u.boolean.right = cql_node_dup(cp->u.boolean.right);
48 struct cql_node *cql_node_mk_sc(const char *index,
52 struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
53 p->which = CQL_NODE_ST;
56 p->u.st.index = xstrdup(index);
57 p->u.st.index_uri = 0;
60 p->u.st.term = xstrdup(term);
63 p->u.st.relation = xstrdup(relation);
64 p->u.st.relation_uri = 0;
65 p->u.st.modifiers = 0;
69 struct cql_node *cql_node_mk_boolean(const char *op)
71 struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
72 p->which = CQL_NODE_BOOL;
73 p->u.boolean.value = 0;
75 p->u.boolean.value = xstrdup(op);
76 p->u.boolean.left = 0;
77 p->u.boolean.right = 0;
78 p->u.boolean.modifiers = 0;
84 return "info:srw/cql-context-set/1/cql-v1.1";
87 struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix,
90 if (n->which == CQL_NODE_ST)
92 if (!n->u.st.index_uri && n->u.st.index)
93 { /* not yet resolved.. */
94 const char *cp = strchr(n->u.st.index, '.');
96 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
97 !memcmp(n->u.st.index, prefix, strlen(prefix)))
99 char *nval = xstrdup(cp+1);
100 n->u.st.index_uri = xstrdup(uri);
101 xfree (n->u.st.index);
102 n->u.st.index = nval;
104 else if (!prefix && !cp)
106 n->u.st.index_uri = xstrdup(uri);
109 if (!n->u.st.relation_uri && n->u.st.relation)
111 const char *cp = strchr(n->u.st.relation, '.');
113 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
114 !memcmp(n->u.st.relation, prefix, strlen(prefix)))
116 char *nval = xstrdup(cp+1);
117 n->u.st.relation_uri = xstrdup(uri);
118 xfree (n->u.st.relation);
119 n->u.st.relation = nval;
123 else if (n->which == CQL_NODE_BOOL)
125 cql_apply_prefix(n->u.boolean.left, prefix, uri);
126 cql_apply_prefix(n->u.boolean.right, prefix, uri);
131 void cql_node_destroy(struct cql_node *cn)
138 xfree (cn->u.st.index);
139 xfree (cn->u.st.relation);
140 xfree (cn->u.st.term);
141 xfree (cn->u.st.index_uri);
142 xfree (cn->u.st.relation_uri);
143 cql_node_destroy(cn->u.st.modifiers);
146 xfree (cn->u.boolean.value);
147 cql_node_destroy(cn->u.boolean.left);
148 cql_node_destroy(cn->u.boolean.right);
149 cql_node_destroy(cn->u.boolean.modifiers);