CQL: handle SORT node in a few places
[yaz-moved-to-github.git] / src / cqlutil.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file cqlutil.c
7  * \brief Implements CQL tree node utilities.
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <yaz/cql.h>
17
18 void cql_fputs(const char *buf, void *client_data)
19 {
20     FILE *f = (FILE *) client_data;
21     fputs(buf, f);
22 }
23
24 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp)
25 {
26     struct cql_node *cn = 0;
27
28     if (!cp)
29         return 0;
30     switch (cp->which)
31     {
32     case CQL_NODE_ST:
33         cn = cql_node_mk_sc(nmem, cp->u.st.index,
34                             cp->u.st.relation,
35                             cp->u.st.term);
36         cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers);
37         cn->u.st.index_uri = cp->u.st.index_uri ? 
38             nmem_strdup(nmem, cp->u.st.index_uri) : 0;
39         cn->u.st.relation_uri = cp->u.st.relation_uri ?
40             nmem_strdup(nmem, cp->u.st.relation_uri) : 0;
41         break;
42     case CQL_NODE_BOOL:
43         cn = cql_node_mk_boolean(nmem, cp->u.boolean.value);
44         cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left);
45         cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right);
46         break;
47     case CQL_NODE_SORT:
48         cn = cql_node_mk_sort(nmem, cp->u.sort.index, cp->u.sort.modifiers);
49         cn->u.sort.next = cql_node_dup(nmem, cp->u.sort.next);
50         cn->u.sort.search = cql_node_dup(nmem, cp->u.sort.search);
51     }
52     return cn;
53 }
54
55 struct cql_node *cql_node_mk_sc(NMEM nmem,
56                                 const char *index,
57                                 const char *relation,
58                                 const char *term)
59 {
60     struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
61     p->which = CQL_NODE_ST;
62     p->u.st.index = 0;
63     if (index)
64         p->u.st.index = nmem_strdup(nmem, index);
65     p->u.st.index_uri = 0;
66     p->u.st.term = 0;
67     if (term)
68         p->u.st.term = nmem_strdup(nmem, term);
69     p->u.st.relation = 0;
70     if (relation)
71         p->u.st.relation = nmem_strdup(nmem, relation);
72     p->u.st.relation_uri = 0;
73     p->u.st.modifiers = 0;
74     p->u.st.extra_terms = 0;
75     return p;
76 }
77
78 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
79 {
80     struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
81     p->which = CQL_NODE_BOOL;
82     p->u.boolean.value = 0;
83     if (op)
84         p->u.boolean.value = nmem_strdup(nmem, op);
85     p->u.boolean.left = 0;
86     p->u.boolean.right = 0;
87     p->u.boolean.modifiers = 0;
88     return p;
89 }
90
91 struct cql_node *cql_node_mk_sort(NMEM nmem, const char *index,
92     struct cql_node *modifiers)
93 {
94     struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
95     p->which = CQL_NODE_SORT;
96     p->u.sort.index = 0;
97     if (index)
98         p->u.sort.index = nmem_strdup(nmem, index);
99     p->u.sort.modifiers = modifiers;
100     p->u.sort.next = 0;
101     return p;
102 }
103
104 const char *cql_uri(void)
105 {
106     return "info:srw/cql-context-set/1/cql-v1.2";
107 }
108
109 struct cql_node *cql_apply_prefix(NMEM nmem,
110                                   struct cql_node *n, const char *prefix,
111                                   const char *uri)
112 {
113     if (n->which == CQL_NODE_ST)
114     {
115         if (!n->u.st.index_uri && n->u.st.index)
116         {   /* not yet resolved.. */
117             const char *cp = strchr(n->u.st.index, '.');
118             if (prefix && cp && 
119                 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
120                 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
121             {
122                 char *nval = nmem_strdup(nmem, cp+1);
123                 n->u.st.index_uri = nmem_strdup(nmem, uri);
124                 n->u.st.index = nval;
125             }
126             else if (!prefix && !cp)
127             {
128                 n->u.st.index_uri = nmem_strdup(nmem, uri);
129             }
130         }
131         if (!n->u.st.relation_uri && n->u.st.relation)
132         {
133             const char *cp = strchr(n->u.st.relation, '.');
134             if (prefix && cp &&
135                 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
136                 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
137             {
138                 char *nval = nmem_strdup(nmem, cp+1);
139                 n->u.st.relation_uri = nmem_strdup(nmem, uri);
140                 n->u.st.relation = nval;
141             }
142         }
143     }
144     else if (n->which == CQL_NODE_BOOL)
145     {
146         cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
147         cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
148     }
149     else if (n->which == CQL_NODE_SORT)
150     {
151         cql_apply_prefix(nmem, n->u.sort.search, prefix, uri);
152     }
153     return n;
154 }
155
156 void cql_node_destroy(struct cql_node *cn)
157 {
158     if (!cn)
159         return;
160     switch (cn->which)
161     {
162     case CQL_NODE_ST:
163         cql_node_destroy(cn->u.st.modifiers);
164         break;
165     case CQL_NODE_BOOL:
166         cql_node_destroy(cn->u.boolean.left);
167         cql_node_destroy(cn->u.boolean.right);
168         cql_node_destroy(cn->u.boolean.modifiers);
169         break;
170     case CQL_NODE_SORT:
171         cql_node_destroy(cn->u.sort.search);
172         cql_node_destroy(cn->u.sort.next);
173         cql_node_destroy(cn->u.sort.modifiers);
174     }
175 }
176
177 int cql_strcmp(const char *s1, const char *s2)
178 {
179     while (*s1 && *s2)
180     {
181         int c1 = *s1++;
182         int c2 = *s2++;
183         if (c1 >= 'A' && c1 <= 'Z')
184             c1 = c1 + ('a' - 'A');
185         if (c2 >= 'A' && c2 <= 'Z')
186             c2 = c2 + ('a' - 'A');
187         if (c1 != c2)
188             return c1 - c2;
189     }
190     return *s1 - *s2;
191 }
192
193 int cql_strncmp(const char *s1, const char *s2, size_t n)
194 {
195     while (*s1 && *s2 && n)
196     {
197         int c1 = *s1++;
198         int c2 = *s2++;
199         if (c1 >= 'A' && c1 <= 'Z')
200             c1 = c1 + ('a' - 'A');
201         if (c2 >= 'A' && c2 <= 'Z')
202             c2 = c2 + ('a' - 'A');
203         if (c1 != c2)
204             return c1 - c2;
205         --n;
206     }
207     if (!n)
208         return 0;
209     return *s1 - *s2;
210 }
211
212 /*
213  * Local variables:
214  * c-basic-offset: 4
215  * c-file-style: "Stroustrup"
216  * indent-tabs-mode: nil
217  * End:
218  * vim: shiftwidth=4 tabstop=8 expandtab
219  */
220