1b842b1c165abc4248357cfca8ce7b5f9e208c33
[yaz-moved-to-github.git] / src / cqlutil.c
1 /* $Id: cqlutil.c,v 1.2 2004-03-10 16:34:29 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.
8 */
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <yaz/cql.h>
14
15 void cql_fputs(const char *buf, void *client_data)
16 {
17     FILE *f = (FILE *) client_data;
18     fputs(buf, f);
19 }
20
21 struct cql_node *cql_node_dup (struct cql_node *cp)
22 {
23     struct cql_node *cn = 0;
24
25     if (!cp)
26         return 0;
27     switch (cp->which)
28     {
29     case CQL_NODE_ST:
30         cn = cql_node_mk_sc(cp->u.st.index,
31                             cp->u.st.relation,
32                             cp->u.st.term);
33         cn->u.st.modifiers = cql_node_dup(cp->u.st.modifiers);
34         cn->u.st.prefixes = cql_node_dup(cp->u.st.prefixes);
35         break;
36     case CQL_NODE_BOOL:
37         cn = cql_node_mk_boolean(cp->u.boolean.value);
38         cn->u.boolean.left = cql_node_dup(cp->u.boolean.left);
39         cn->u.boolean.right = cql_node_dup(cp->u.boolean.right);
40         cn->u.boolean.prefixes = cql_node_dup(cp->u.boolean.prefixes);
41     }
42     return cn;
43 }
44
45 struct cql_node *cql_node_mk_sc(const char *index,
46                                 const char *relation,
47                                 const char *term)
48 {
49     struct cql_node *p = (struct cql_node *) malloc(sizeof(*p));
50     p->which = CQL_NODE_ST;
51     p->u.st.index = 0;
52     if (index)
53         p->u.st.index = strdup(index);
54     p->u.st.term = 0;
55     if (term)
56         p->u.st.term = strdup(term);
57     p->u.st.relation = 0;
58     if (relation)
59         p->u.st.relation = strdup(relation);
60     p->u.st.modifiers = 0;
61     p->u.st.prefixes = 0;
62     return p;
63 }
64
65 struct cql_node *cql_node_mk_boolean(const char *op)
66 {
67     struct cql_node *p = (struct cql_node *) malloc(sizeof(*p));
68     p->which = CQL_NODE_BOOL;
69     p->u.boolean.value = 0;
70     if (op)
71         p->u.boolean.value = strdup(op);
72     p->u.boolean.left = 0;
73     p->u.boolean.right = 0;
74     p->u.boolean.modifiers = 0;
75     p->u.boolean.prefixes = 0;
76     return p;
77 }
78
79 struct cql_node *cql_node_prefix(struct cql_node *n, const char *prefix,
80                                  const char *uri)
81 {
82     struct cql_node **cpp = 0;
83     if (n->which == CQL_NODE_ST)
84     {
85         cpp = &n->u.st.prefixes;
86     }
87     else if (n->which == CQL_NODE_BOOL)
88     {
89         cpp = &n->u.boolean.prefixes;
90     }
91     if (cpp)
92     {
93         struct cql_node *cp = cql_node_mk_sc(prefix, "=", uri);
94         cp->u.st.modifiers = *cpp;
95         *cpp = cp;
96     }
97     return n;
98 }
99
100 void cql_node_destroy(struct cql_node *cn)
101 {
102     if (!cn)
103         return;
104     switch (cn->which)
105     {
106     case CQL_NODE_ST:
107         free (cn->u.st.index);
108         free (cn->u.st.relation);
109         free (cn->u.st.term);
110         cql_node_destroy(cn->u.st.modifiers);
111         cql_node_destroy(cn->u.st.prefixes);
112         break;
113     case CQL_NODE_BOOL:
114         free (cn->u.boolean.value);
115         cql_node_destroy(cn->u.boolean.left);
116         cql_node_destroy(cn->u.boolean.right);
117         cql_node_destroy(cn->u.boolean.prefixes);
118         cql_node_destroy(cn->u.boolean.modifiers);
119     }
120     free (cn);
121 }
122