1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CQL to XCQL conversion.
19 static int cql_to_ccl_r(struct cql_node *cn,
20 void (*pr)(const char *buf, void *client_data),
23 static void pr_term(struct cql_node *cn,
24 void (*pr)(const char *buf, void *client_data),
29 if (! *cn->u.st.term) /* empty term special case */
30 pr("\"\"", client_data);
35 for (cp = cn->u.st.term; *cp; cp++)
39 if (*cp == '\\' && cp[1])
51 pr("\"", client_data);
60 pr("\"", client_data);
69 pr("\"", client_data);
78 pr("\"", client_data);
80 if (cn->u.st.extra_terms)
82 cn = cn->u.st.extra_terms;
86 static int node(struct cql_node *cn,
87 void (*pr)(const char *buf, void *client_data),
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;
95 if (cn->u.st.index && strcmp(cn->u.st.index,
97 ccl_field = cn->u.st.index;
101 else if (!strcmp(rel, "<") || !strcmp(rel, "<=")
102 || !strcmp(rel, ">") || !strcmp(rel, ">=")
103 || !strcmp(rel, "<>") || !strcmp(rel, "="))
105 else if (!strcmp(rel, "all"))
110 else if (!strcmp(rel, "any"))
115 else if (!strcmp(rel, "==") || !strcmp(rel, "adj"))
121 /* unsupported relation */
126 if (ccl_field && ccl_rel)
128 pr(ccl_field, client_data);
129 pr(ccl_rel, client_data);
131 pr_term(cn, pr, client_data);
135 const char *cp = cn->u.st.term;
141 if (ccl_field && ccl_rel)
143 pr(ccl_field, client_data);
144 pr(ccl_rel, client_data);
146 while (*cp && *cp != ' ')
161 pr(" ", client_data);
162 pr(split_op, client_data);
163 pr(" ", client_data);
170 static int bool(struct cql_node *cn,
171 void (*pr)(const char *buf, void *client_data),
174 char *value = cn->u.boolean.value;
177 pr("(", client_data);
178 r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
182 pr(" ", client_data);
184 if (strcmp(value, "prox"))
185 { /* not proximity. assuming boolean */
186 pr(value, client_data);
190 struct cql_node *n = cn->u.boolean.modifiers;
193 for (; n ; n = n->u.st.modifiers)
194 if (n->which == CQL_NODE_ST)
196 if (!strcmp(n->u.st.index, "unit"))
198 if (!strcmp(n->u.st.term, "word"))
203 else if (!strcmp(n->u.st.index, "distance"))
205 if (!strcmp(n->u.st.relation, "<="))
206 distance = atoi(n->u.st.term);
207 else if (!strcmp(n->u.st.relation, "<"))
208 distance = atoi(n->u.st.term) - 1;
212 else if (!strcmp(n->u.st.index, "unordered"))
216 else if (!strcmp(n->u.st.index, "ordered"))
223 pr(ordered ? "!" : "%", client_data);
227 sprintf(x, "%d", distance);
231 pr(" ", client_data);
233 r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
234 pr(")", client_data);
238 static int cql_to_ccl_r(struct cql_node *cn,
239 void (*pr)(const char *buf, void *client_data),
248 return node(cn, pr, client_data);
250 return bool(cn, pr, client_data);
252 return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
257 int cql_to_ccl(struct cql_node *cn,
258 void (*pr)(const char *buf, void *client_data),
261 return cql_to_ccl_r(cn, pr, client_data);
264 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
266 cql_to_ccl(cn, cql_fputs, f);
269 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
271 struct cql_buf_write_info info;
276 r = cql_to_ccl(cn, cql_buf_write_handler, &info);
278 info.buf[info.off] = '\0';
280 return -2; /* buffer overflow */
287 * c-file-style: "Stroustrup"
288 * indent-tabs-mode: nil
290 * vim: shiftwidth=4 tabstop=8 expandtab