Merge branch 'master' into normal_cql
[yaz-moved-to-github.git] / src / cql2ccl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file cql2ccl.c
7  * \brief Implements CQL to CCL conversion.
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16
17 #include <yaz/cql.h>
18
19 static int cql_to_ccl_r(struct cql_node *cn,
20                         void (*pr)(const char *buf, void *client_data),
21                         void *client_data);
22
23 static void pr_term(const char **cpp, int stop_at_space,
24                     void (*pr)(const char *buf, void *client_data),
25                     void *client_data)
26 {
27     const char *cp;
28     int quote_mode = 0;
29     for (cp = *cpp; *cp; cp++)
30     {
31         char x[4];
32
33         if (*cp == '\\' && cp[1])
34         {
35             if (!quote_mode)
36             {
37                 pr("\"", client_data);
38                 quote_mode = 1;
39             }
40             cp++;
41             if (*cp == '\"' || *cp == '\\')
42                 pr("\\", client_data);
43             x[0] = *cp;
44             x[1] = '\0';
45             pr(x, client_data);
46         }
47         else if (*cp == '*')
48         {
49             if (quote_mode)
50             {
51                 pr("\"", client_data);
52                 quote_mode = 0;
53             }
54             pr("?", client_data);
55         }
56         else if (*cp == '?')
57         {
58             if (quote_mode)
59             {
60                 pr("\"", client_data);
61                 quote_mode = 0;
62             }
63             pr("#", client_data);
64         }
65         else if (*cp == ' ' && stop_at_space)
66             break;
67         else
68         {
69             if (!quote_mode)
70             {
71                 pr("\"", client_data);
72                 quote_mode = 1;
73             }
74             x[0] = *cp;
75             x[1] = '\0';
76             pr(x, client_data);
77         }
78     }
79     if (quote_mode)
80         pr("\"", client_data);
81     if (cp == *cpp)
82         pr("\"\"", client_data);
83     *cpp = cp;
84 }
85
86 static int node(struct cql_node *cn,
87                 void (*pr)(const char *buf, void *client_data),
88                 void *client_data)
89 {
90     const char *ccl_field = 0;
91     const char *split_op = 0;
92     const char *ccl_rel = 0;
93     const char *rel = cn->u.st.relation;
94     const char *cp;
95
96     if (cn->u.st.index && strcmp(cn->u.st.index,
97                                  "cql.serverChoice"))
98         ccl_field = cn->u.st.index;
99
100     if (!rel)
101         ;
102     else if (!strcmp(rel, "<") || !strcmp(rel, "<=")
103              || !strcmp(rel, ">") || !strcmp(rel, ">=")
104              || !strcmp(rel, "<>") || !strcmp(rel, "="))
105         ccl_rel = rel;
106     else if (!strcmp(rel, "all"))
107     {
108         ccl_rel = "=";
109         split_op = "and";
110     }
111     else if (!strcmp(rel, "any"))
112     {
113         ccl_rel = "=";
114         split_op = "or";
115     }
116     else if (!strcmp(rel, "==") || !strcmp(rel, "adj"))
117     {
118         ccl_rel = "=";
119     }
120     else
121     {
122         /* unsupported relation */
123         return -1;
124     }
125     cp = cn->u.st.term;
126     while (1)
127     {
128         if (ccl_field && ccl_rel)
129         {
130             pr(ccl_field, client_data);
131             pr(ccl_rel, client_data);
132             if (!split_op)
133                 ccl_rel = 0;
134         }
135         pr_term(&cp, split_op ? 1 : 0, pr, client_data);
136         while (*cp == ' ')
137             cp++;
138         if (*cp == '\0')
139             break;
140         pr(" ", client_data);
141         if (split_op)
142         {
143             pr(split_op, client_data);
144             pr(" ", client_data);
145         }
146     }
147     return 0;
148 }
149
150
151 static int bool(struct cql_node *cn,
152                 void (*pr)(const char *buf, void *client_data),
153                 void *client_data)
154 {
155     char *value = cn->u.boolean.value;
156     int r;
157
158     pr("(", client_data);
159     r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
160     if (r)
161         return r;
162
163     pr(") ", client_data);
164
165     if (strcmp(value, "prox"))
166     {   /* not proximity. assuming boolean */
167         pr(value, client_data);
168     }
169     else
170     {
171         struct cql_node *n = cn->u.boolean.modifiers;
172         int ordered = 0;
173         int distance = 1;
174         for (; n ; n = n->u.st.modifiers)
175             if (n->which == CQL_NODE_ST)
176             {
177                 if (!strcmp(n->u.st.index, "unit"))
178                 {
179                     if (!strcmp(n->u.st.term, "word"))
180                         ;
181                     else
182                         return -1;
183                 }
184                 else if (!strcmp(n->u.st.index, "distance"))
185                 {
186                     if (!strcmp(n->u.st.relation, "<="))
187                         distance = atoi(n->u.st.term);
188                     else if (!strcmp(n->u.st.relation, "<"))
189                             distance = atoi(n->u.st.term) - 1;
190                     else
191                         return -1;
192                 }
193                 else if (!strcmp(n->u.st.index, "unordered"))
194                 {
195                     ordered = 0;
196                 }
197                 else if (!strcmp(n->u.st.index, "ordered"))
198                 {
199                     ordered = 1;
200                 }
201                 else
202                     return -1;
203             }
204         pr(ordered ? "!" : "%", client_data);
205         if (distance != 1)
206         {
207             char x[40];
208             sprintf(x, "%d", distance);
209             pr(x, client_data);
210         }
211     }
212     pr(" (", client_data);
213
214     r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
215     pr(")", client_data);
216     return r;
217 }
218
219 static int cql_to_ccl_r(struct cql_node *cn,
220                         void (*pr)(const char *buf, void *client_data),
221                         void *client_data)
222 {
223     if (!cn)
224         return -1;
225
226     switch (cn->which)
227     {
228     case CQL_NODE_ST:
229         return node(cn, pr, client_data);
230     case CQL_NODE_BOOL:
231         return bool(cn, pr, client_data);
232     case CQL_NODE_SORT:
233         return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
234     }
235     return -1;
236 }
237
238 int cql_to_ccl(struct cql_node *cn,
239                void (*pr)(const char *buf, void *client_data),
240                void *client_data)
241 {
242     return cql_to_ccl_r(cn, pr, client_data);
243 }
244
245 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
246 {
247     cql_to_ccl(cn, cql_fputs, f);
248 }
249
250 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
251 {
252     struct cql_buf_write_info info;
253     int r;
254     info.off = 0;
255     info.max = max;
256     info.buf = out;
257     r = cql_to_ccl(cn, cql_buf_write_handler, &info);
258     if (info.off >= 0)
259         info.buf[info.off] = '\0';
260     else
261         return -2; /* buffer overflow */
262     return r;
263 }
264
265 /*
266  * Local variables:
267  * c-basic-offset: 4
268  * c-file-style: "Stroustrup"
269  * indent-tabs-mode: nil
270  * End:
271  * vim: shiftwidth=4 tabstop=8 expandtab
272  */
273