Initial work for Doxygen based YAZ reference
[yaz-moved-to-github.git] / src / cqlutil.c
1 /* $Id: cqlutil.c,v 1.5 2004-10-03 22:34:07 adam Exp $
2    Copyright (C) 2002-2004
3    Index Data Aps
4
5 This file is part of the YAZ toolkit.
6
7 See the file LICENSE for details.
8 */
9
10 /**
11  * \file cqlutil.c
12  * \brief Implements CQL tree node utilities.
13  */
14
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include <yaz/xmalloc.h>
19 #include <yaz/cql.h>
20
21 void cql_fputs(const char *buf, void *client_data)
22 {
23     FILE *f = (FILE *) client_data;
24     fputs(buf, f);
25 }
26
27 struct cql_node *cql_node_dup (struct cql_node *cp)
28 {
29     struct cql_node *cn = 0;
30
31     if (!cp)
32         return 0;
33     switch (cp->which)
34     {
35     case CQL_NODE_ST:
36         cn = cql_node_mk_sc(cp->u.st.index,
37                             cp->u.st.relation,
38                             cp->u.st.term);
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;
44         break;
45     case CQL_NODE_BOOL:
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);
49     }
50     return cn;
51 }
52
53 struct cql_node *cql_node_mk_sc(const char *index,
54                                 const char *relation,
55                                 const char *term)
56 {
57     struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
58     p->which = CQL_NODE_ST;
59     p->u.st.index = 0;
60     if (index)
61         p->u.st.index = xstrdup(index);
62     p->u.st.index_uri = 0;
63     p->u.st.term = 0;
64     if (term)
65         p->u.st.term = xstrdup(term);
66     p->u.st.relation = 0;
67     if (relation)
68         p->u.st.relation = xstrdup(relation);
69     p->u.st.relation_uri = 0;
70     p->u.st.modifiers = 0;
71     return p;
72 }
73
74 struct cql_node *cql_node_mk_boolean(const char *op)
75 {
76     struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
77     p->which = CQL_NODE_BOOL;
78     p->u.boolean.value = 0;
79     if (op)
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;
84     return p;
85 }
86
87 const char *cql_uri()
88 {
89     return "info:srw/cql-context-set/1/cql-v1.1";
90 }
91
92 struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix,
93                                   const char *uri)
94 {
95     if (n->which == CQL_NODE_ST)
96     {
97         if (!n->u.st.index_uri && n->u.st.index)
98         {   /* not yet resolved.. */
99             const char *cp = strchr(n->u.st.index, '.');
100             if (prefix && cp && 
101                 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
102                 !memcmp(n->u.st.index, prefix, strlen(prefix)))
103             {
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;
108             }
109             else if (!prefix && !cp)
110             {
111                 n->u.st.index_uri = xstrdup(uri);
112             }
113         }
114         if (!n->u.st.relation_uri && n->u.st.relation)
115         {
116             const char *cp = strchr(n->u.st.relation, '.');
117             if (prefix && cp &&
118                 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
119                 !memcmp(n->u.st.relation, prefix, strlen(prefix)))
120             {
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;
125             }
126         }
127     }
128     else if (n->which == CQL_NODE_BOOL)
129     {
130         cql_apply_prefix(n->u.boolean.left, prefix, uri);
131         cql_apply_prefix(n->u.boolean.right, prefix, uri);
132     }
133     return n;
134 }
135
136 void cql_node_destroy(struct cql_node *cn)
137 {
138     if (!cn)
139         return;
140     switch (cn->which)
141     {
142     case CQL_NODE_ST:
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);
149         break;
150     case CQL_NODE_BOOL:
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);
155     }
156     xfree (cn);
157 }