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),
42 if (cn->u.st.extra_terms)
44 cn = cn->u.st.extra_terms;
48 static int node(struct cql_node *cn,
49 void (*pr)(const char *buf, void *client_data),
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;
57 if (cn->u.st.index && strcmp(cn->u.st.index,
59 ccl_field = cn->u.st.index;
63 else if (!strcmp(rel, "<") || !strcmp(rel, "<=")
64 || !strcmp(rel, ">") || !strcmp(rel, ">=")
65 || !strcmp(rel, "<>") || !strcmp(rel, "="))
67 else if (!strcmp(rel, "all"))
72 else if (!strcmp(rel, "any"))
77 else if (!strcmp(rel, "==") || !strcmp(rel, "adj"))
83 /* unsupported relation */
88 if (ccl_field && ccl_rel)
90 pr(ccl_field, client_data);
91 pr(ccl_rel, client_data);
93 pr_term(cn, pr, client_data);
97 const char *cp = cn->u.st.term;
103 if (ccl_field && ccl_rel)
105 pr(ccl_field, client_data);
106 pr(ccl_rel, client_data);
108 while (*cp && *cp != ' ')
123 pr(" ", client_data);
124 pr(split_op, client_data);
125 pr(" ", client_data);
132 static int bool(struct cql_node *cn,
133 void (*pr)(const char *buf, void *client_data),
136 char *value = cn->u.boolean.value;
139 /* Rather lame initial attempt at interpreting proximity */
140 if (!strcmp(value, "prox")) value = "!";
142 pr("(", client_data);
143 r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
147 pr(" ", client_data);
148 pr(value, client_data);
149 pr(" ", client_data);
151 r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
152 pr(")", client_data);
156 static int cql_to_ccl_r(struct cql_node *cn,
157 void (*pr)(const char *buf, void *client_data),
166 return node(cn, pr, client_data);
168 return bool(cn, pr, client_data);
170 return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
175 int cql_to_ccl(struct cql_node *cn,
176 void (*pr)(const char *buf, void *client_data),
179 return cql_to_ccl_r(cn, pr, client_data);
182 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
184 cql_to_ccl(cn, cql_fputs, f);
187 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
189 struct cql_buf_write_info info;
194 r = cql_to_ccl(cn, cql_buf_write_handler, &info);
196 info.buf[info.off] = '\0';
198 return -2; /* buffer overflow */
205 * c-file-style: "Stroustrup"
206 * indent-tabs-mode: nil
208 * vim: shiftwidth=4 tabstop=8 expandtab