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 CCL 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])
43 pr("\"", client_data);
47 if (*cp == '\"' || *cp == '\\')
48 pr("\\", client_data);
57 pr("\"", client_data);
66 pr("\"", client_data);
75 pr("\"", client_data);
84 pr("\"", client_data);
86 if (cn->u.st.extra_terms)
88 cn = cn->u.st.extra_terms;
92 static int node(struct cql_node *cn,
93 void (*pr)(const char *buf, void *client_data),
96 const char *ccl_field = 0;
97 const char *split_op = 0;
98 const char *ccl_rel = 0;
99 const char *rel = cn->u.st.relation;
101 if (cn->u.st.index && strcmp(cn->u.st.index,
103 ccl_field = cn->u.st.index;
107 else if (!strcmp(rel, "<") || !strcmp(rel, "<=")
108 || !strcmp(rel, ">") || !strcmp(rel, ">=")
109 || !strcmp(rel, "<>") || !strcmp(rel, "="))
111 else if (!strcmp(rel, "all"))
116 else if (!strcmp(rel, "any"))
121 else if (!strcmp(rel, "==") || !strcmp(rel, "adj"))
127 /* unsupported relation */
132 if (ccl_field && ccl_rel)
134 pr(ccl_field, client_data);
135 pr(ccl_rel, client_data);
137 pr_term(cn, pr, client_data);
141 const char *cp = cn->u.st.term;
147 if (ccl_field && ccl_rel)
149 pr(ccl_field, client_data);
150 pr(ccl_rel, client_data);
152 while (*cp && *cp != ' ')
167 pr(" ", client_data);
168 pr(split_op, client_data);
169 pr(" ", client_data);
176 static int bool(struct cql_node *cn,
177 void (*pr)(const char *buf, void *client_data),
180 char *value = cn->u.boolean.value;
183 pr("(", client_data);
184 r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
188 pr(" ", client_data);
190 if (strcmp(value, "prox"))
191 { /* not proximity. assuming boolean */
192 pr(value, client_data);
196 struct cql_node *n = cn->u.boolean.modifiers;
199 for (; n ; n = n->u.st.modifiers)
200 if (n->which == CQL_NODE_ST)
202 if (!strcmp(n->u.st.index, "unit"))
204 if (!strcmp(n->u.st.term, "word"))
209 else if (!strcmp(n->u.st.index, "distance"))
211 if (!strcmp(n->u.st.relation, "<="))
212 distance = atoi(n->u.st.term);
213 else if (!strcmp(n->u.st.relation, "<"))
214 distance = atoi(n->u.st.term) - 1;
218 else if (!strcmp(n->u.st.index, "unordered"))
222 else if (!strcmp(n->u.st.index, "ordered"))
229 pr(ordered ? "!" : "%", client_data);
233 sprintf(x, "%d", distance);
237 pr(" ", client_data);
239 r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
240 pr(")", client_data);
244 static int cql_to_ccl_r(struct cql_node *cn,
245 void (*pr)(const char *buf, void *client_data),
254 return node(cn, pr, client_data);
256 return bool(cn, pr, client_data);
258 return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
263 int cql_to_ccl(struct cql_node *cn,
264 void (*pr)(const char *buf, void *client_data),
267 return cql_to_ccl_r(cn, pr, client_data);
270 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
272 cql_to_ccl(cn, cql_fputs, f);
275 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
277 struct cql_buf_write_info info;
282 r = cql_to_ccl(cn, cql_buf_write_handler, &info);
284 info.buf[info.off] = '\0';
286 return -2; /* buffer overflow */
293 * c-file-style: "Stroustrup"
294 * indent-tabs-mode: nil
296 * vim: shiftwidth=4 tabstop=8 expandtab