Always initialize libgcrypt for POSIX threads YAZ-751
[yaz-moved-to-github.git] / src / cqlutil.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 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     p->u.sort.search = 0;
102     return p;
103 }
104
105 const char *cql_uri(void)
106 {
107     return "info:srw/cql-context-set/1/cql-v1.2";
108 }
109
110 struct cql_node *cql_apply_prefix(NMEM nmem,
111                                   struct cql_node *n, const char *prefix,
112                                   const char *uri)
113 {
114     if (n->which == CQL_NODE_ST)
115     {
116         if (!n->u.st.index_uri && n->u.st.index)
117         {   /* not yet resolved.. */
118             const char *cp = strchr(n->u.st.index, '.');
119             if (prefix && cp &&
120                 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
121                 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
122             {
123                 char *nval = nmem_strdup(nmem, cp+1);
124                 n->u.st.index_uri = nmem_strdup(nmem, uri);
125                 n->u.st.index = nval;
126             }
127             else if (!prefix && !cp)
128             {
129                 n->u.st.index_uri = nmem_strdup(nmem, uri);
130             }
131         }
132         if (!n->u.st.relation_uri && n->u.st.relation)
133         {
134             const char *cp = strchr(n->u.st.relation, '.');
135             if (prefix && cp &&
136                 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
137                 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
138             {
139                 char *nval = nmem_strdup(nmem, cp+1);
140                 n->u.st.relation_uri = nmem_strdup(nmem, uri);
141                 n->u.st.relation = nval;
142             }
143         }
144     }
145     else if (n->which == CQL_NODE_BOOL)
146     {
147         cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
148         cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
149     }
150     else if (n->which == CQL_NODE_SORT)
151     {
152         cql_apply_prefix(nmem, n->u.sort.search, prefix, uri);
153     }
154     return n;
155 }
156
157 void cql_node_destroy(struct cql_node *cn)
158 {
159     if (!cn)
160         return;
161     switch (cn->which)
162     {
163     case CQL_NODE_ST:
164         cql_node_destroy(cn->u.st.modifiers);
165         break;
166     case CQL_NODE_BOOL:
167         cql_node_destroy(cn->u.boolean.left);
168         cql_node_destroy(cn->u.boolean.right);
169         cql_node_destroy(cn->u.boolean.modifiers);
170         break;
171     case CQL_NODE_SORT:
172         cql_node_destroy(cn->u.sort.search);
173         cql_node_destroy(cn->u.sort.next);
174         cql_node_destroy(cn->u.sort.modifiers);
175     }
176 }
177
178 int cql_strcmp(const char *s1, const char *s2)
179 {
180     while (*s1 && *s2)
181     {
182         int c1 = *s1++;
183         int c2 = *s2++;
184         if (c1 >= 'A' && c1 <= 'Z')
185             c1 = c1 + ('a' - 'A');
186         if (c2 >= 'A' && c2 <= 'Z')
187             c2 = c2 + ('a' - 'A');
188         if (c1 != c2)
189             return c1 - c2;
190     }
191     return *s1 - *s2;
192 }
193
194 int cql_strncmp(const char *s1, const char *s2, size_t n)
195 {
196     while (*s1 && *s2 && n)
197     {
198         int c1 = *s1++;
199         int c2 = *s2++;
200         if (c1 >= 'A' && c1 <= 'Z')
201             c1 = c1 + ('a' - 'A');
202         if (c2 >= 'A' && c2 <= 'Z')
203             c2 = c2 + ('a' - 'A');
204         if (c1 != c2)
205             return c1 - c2;
206         --n;
207     }
208     if (!n)
209         return 0;
210     return *s1 - *s2;
211 }
212
213 /*
214  * Local variables:
215  * c-basic-offset: 4
216  * c-file-style: "Stroustrup"
217  * indent-tabs-mode: nil
218  * End:
219  * vim: shiftwidth=4 tabstop=8 expandtab
220  */
221