2c25dea26797d1f3fc2bdba71972fa7227b4938d
[yaz-moved-to-github.git] / src / cqlutil.c
1 /* $Id: cqlutil.c,v 1.13 2008-01-06 16:22:02 adam Exp $
2    Copyright (C) 1995-2007, Index Data ApS
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/cql.h>
19
20 void cql_fputs(const char *buf, void *client_data)
21 {
22     FILE *f = (FILE *) client_data;
23     fputs(buf, f);
24 }
25
26 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp)
27 {
28     struct cql_node *cn = 0;
29
30     if (!cp)
31         return 0;
32     switch (cp->which)
33     {
34     case CQL_NODE_ST:
35         cn = cql_node_mk_sc(nmem, cp->u.st.index,
36                             cp->u.st.relation,
37                             cp->u.st.term);
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;
43         break;
44     case CQL_NODE_BOOL:
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);
48     }
49     return cn;
50 }
51
52 struct cql_node *cql_node_mk_sc(NMEM nmem,
53                                 const char *index,
54                                 const char *relation,
55                                 const char *term)
56 {
57     struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
58     p->which = CQL_NODE_ST;
59     p->u.st.index = 0;
60     if (index)
61         p->u.st.index = nmem_strdup(nmem, index);
62     p->u.st.index_uri = 0;
63     p->u.st.term = 0;
64     if (term)
65         p->u.st.term = nmem_strdup(nmem, term);
66     p->u.st.relation = 0;
67     if (relation)
68         p->u.st.relation = nmem_strdup(nmem, relation);
69     p->u.st.relation_uri = 0;
70     p->u.st.modifiers = 0;
71     p->u.st.extra_terms = 0;
72     return p;
73 }
74
75 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
76 {
77     struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
78     p->which = CQL_NODE_BOOL;
79     p->u.boolean.value = 0;
80     if (op)
81         p->u.boolean.value = nmem_strdup(nmem, op);
82     p->u.boolean.left = 0;
83     p->u.boolean.right = 0;
84     p->u.boolean.modifiers = 0;
85     return p;
86 }
87
88 const char *cql_uri(void)
89 {
90     return "info:srw/cql-context-set/1/cql-v1.2";
91 }
92
93 struct cql_node *cql_apply_prefix(NMEM nmem,
94                                   struct cql_node *n, const char *prefix,
95                                   const char *uri)
96 {
97     if (n->which == CQL_NODE_ST)
98     {
99         if (!n->u.st.index_uri && n->u.st.index)
100         {   /* not yet resolved.. */
101             const char *cp = strchr(n->u.st.index, '.');
102             if (prefix && cp && 
103                 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
104                 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
105             {
106                 char *nval = nmem_strdup(nmem, cp+1);
107                 n->u.st.index_uri = nmem_strdup(nmem, uri);
108                 n->u.st.index = nval;
109             }
110             else if (!prefix && !cp)
111             {
112                 n->u.st.index_uri = nmem_strdup(nmem, uri);
113             }
114         }
115         if (!n->u.st.relation_uri && n->u.st.relation)
116         {
117             const char *cp = strchr(n->u.st.relation, '.');
118             if (prefix && cp &&
119                 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
120                 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
121             {
122                 char *nval = nmem_strdup(nmem, cp+1);
123                 n->u.st.relation_uri = nmem_strdup(nmem, uri);
124                 n->u.st.relation = nval;
125             }
126         }
127     }
128     else if (n->which == CQL_NODE_BOOL)
129     {
130         cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
131         cql_apply_prefix(nmem, 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         cql_node_destroy(cn->u.st.modifiers);
144         break;
145     case CQL_NODE_BOOL:
146         cql_node_destroy(cn->u.boolean.left);
147         cql_node_destroy(cn->u.boolean.right);
148         cql_node_destroy(cn->u.boolean.modifiers);
149     }
150 }
151
152 int cql_strcmp(const char *s1, const char *s2)
153 {
154     while (*s1 && *s2)
155     {
156         int c1 = *s1++;
157         int c2 = *s2++;
158         if (c1 >= 'A' && c1 <= 'Z')
159             c1 = c1 + ('a' - 'A');
160         if (c2 >= 'A' && c2 <= 'Z')
161             c2 = c2 + ('a' - 'A');
162         if (c1 != c2)
163             return c1 - c2;
164     }
165     return *s1 - *s2;
166 }
167
168 int cql_strncmp(const char *s1, const char *s2, size_t n)
169 {
170     while (*s1 && *s2 && n)
171     {
172         int c1 = *s1++;
173         int c2 = *s2++;
174         if (c1 >= 'A' && c1 <= 'Z')
175             c1 = c1 + ('a' - 'A');
176         if (c2 >= 'A' && c2 <= 'Z')
177             c2 = c2 + ('a' - 'A');
178         if (c1 != c2)
179             return c1 - c2;
180         --n;
181     }
182     if (!n)
183         return 0;
184     return *s1 - *s2;
185 }
186
187 /*
188  * Local variables:
189  * c-basic-offset: 4
190  * indent-tabs-mode: nil
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */
194