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),
138 pr("(", client_data);
139 r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
143 pr(" ", client_data);
144 pr(cn->u.boolean.value, client_data);
145 pr(" ", client_data);
147 r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
148 pr(")", client_data);
152 static int cql_to_ccl_r(struct cql_node *cn,
153 void (*pr)(const char *buf, void *client_data),
162 return node(cn, pr, client_data);
164 return bool(cn, pr, client_data);
166 return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
171 int cql_to_ccl(struct cql_node *cn,
172 void (*pr)(const char *buf, void *client_data),
175 return cql_to_ccl_r(cn, pr, client_data);
178 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
180 cql_to_ccl(cn, cql_fputs, f);
183 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
185 struct cql_buf_write_info info;
190 r = cql_to_ccl(cn, cql_buf_write_handler, &info);
192 info.buf[info.off] = '\0';
194 return -2; /* buffer overflow */
201 * c-file-style: "Stroustrup"
202 * indent-tabs-mode: nil
204 * vim: shiftwidth=4 tabstop=8 expandtab