The static function bool(), which is part of cql_to_ccl_r(), now makes
[yaz-moved-to-github.git] / src / cql2ccl.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 cql2ccl.c
7  * \brief Implements CQL to XCQL 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(struct cql_node *cn,
24                     void (*pr)(const char *buf, void *client_data),
25                     void *client_data)
26 {
27     while (cn)
28     {
29         const char *cp;
30         cp = cn->u.st.term;
31         while (*cp)
32         {
33             char x[2];
34             if (*cp == '*')
35                 x[0] = '?';
36             else
37                 x[0] = *cp;
38             x[1] = 0;
39             pr(x, client_data);
40             cp++;
41         }
42         if (cn->u.st.extra_terms)
43             pr(" ", client_data);
44         cn = cn->u.st.extra_terms;
45     }
46 }
47
48 static int node(struct cql_node *cn, 
49                 void (*pr)(const char *buf, void *client_data),
50                 void *client_data)
51 {
52     const char *ccl_field = 0;
53     const char *split_op = 0;
54     const char *ccl_rel = 0;
55     const char *rel = cn->u.st.relation;
56
57     if (cn->u.st.index && strcmp(cn->u.st.index,
58                                  "cql.serverChoice"))
59         ccl_field = cn->u.st.index;
60
61     if (!rel)
62         ;
63     else if (!strcmp(rel, "<") || !strcmp(rel, "<=")
64              || !strcmp(rel, ">") || !strcmp(rel, ">=")
65              || !strcmp(rel, "<>") || !strcmp(rel, "="))
66         ccl_rel = rel;
67     else if (!strcmp(rel, "all"))
68     {
69         ccl_rel = "=";
70         split_op = "and";
71     }
72     else if (!strcmp(rel, "any"))
73     {
74         ccl_rel = "=";
75         split_op = "or";
76     }
77     else if (!strcmp(rel, "==") || !strcmp(rel, "adj"))
78     {
79         ccl_rel = "=";
80     }
81     else
82     {
83         /* unsupported relation */
84         return -1;
85     }
86     if (!split_op)
87     {
88         if (ccl_field && ccl_rel)
89         {
90             pr(ccl_field, client_data);
91             pr(ccl_rel, client_data);
92         }
93         pr_term(cn, pr, client_data);
94     }
95     else
96     {
97         const char *cp = cn->u.st.term;
98         
99         while (1)
100         {
101             if (*cp == '\0')
102                 break;
103             if (ccl_field && ccl_rel)
104             {
105                 pr(ccl_field, client_data);
106                 pr(ccl_rel, client_data);
107             }
108             while (*cp && *cp != ' ')
109             {
110                 char x[2];
111                 if (*cp == '*')
112                     x[0] = '?';
113                 else
114                     x[0] = *cp;
115                 x[1] = '\0';
116                 pr(x, client_data);
117                 cp++;
118             }
119             while (*cp == ' ')
120                 cp++;
121             if (*cp == '\0')
122                 break;
123             pr(" ", client_data);
124             pr(split_op, client_data);
125             pr(" ", client_data);            
126         }
127     }
128     return 0;
129 }
130
131
132 static int bool(struct cql_node *cn, 
133                 void (*pr)(const char *buf, void *client_data),
134                 void *client_data)
135 {
136     char *value = cn->u.boolean.value;
137     int r;
138
139     /* Rather lame initial attempt at interpreting proximity */
140     if (!strcmp(value, "prox")) value = "!";
141
142     pr("(", client_data);
143     r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
144     if (r)
145         return r;
146     
147     pr(" ", client_data);
148     pr(value, client_data);
149     pr(" ", client_data);
150
151     r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
152     pr(")", client_data);
153     return r;
154 }
155
156 static int cql_to_ccl_r(struct cql_node *cn, 
157                         void (*pr)(const char *buf, void *client_data),
158                         void *client_data)
159 {
160     if (!cn)
161         return -1;
162
163     switch (cn->which)
164     {
165     case CQL_NODE_ST:
166         return node(cn, pr, client_data);
167     case CQL_NODE_BOOL:
168         return bool(cn, pr, client_data);
169     case CQL_NODE_SORT:
170         return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
171     }
172     return -1;
173 }
174
175 int cql_to_ccl(struct cql_node *cn, 
176                void (*pr)(const char *buf, void *client_data),
177                void *client_data)
178 {
179     return cql_to_ccl_r(cn, pr, client_data);
180 }
181
182 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
183 {
184     cql_to_ccl(cn, cql_fputs, f);
185 }
186
187 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
188 {
189     struct cql_buf_write_info info;
190     int r;
191     info.off = 0;
192     info.max = max;
193     info.buf = out;
194     r = cql_to_ccl(cn, cql_buf_write_handler, &info);
195     if (info.off >= 0)
196         info.buf[info.off] = '\0';
197     else
198         return -2; /* buffer overflow */
199     return r;
200 }
201
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * c-file-style: "Stroustrup"
206  * indent-tabs-mode: nil
207  * End:
208  * vim: shiftwidth=4 tabstop=8 expandtab
209  */
210