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 pr("(", client_data);
140 r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
144 pr(" ", client_data);
146 if (strcmp(value, "prox"))
147 { /* not proximity. assuming boolean */
148 pr(value, client_data);
152 struct cql_node *n = cn->u.boolean.modifiers;
155 for (; n ; n = n->u.st.modifiers)
156 if (n->which == CQL_NODE_ST)
158 if (!strcmp(n->u.st.index, "unit"))
160 if (!strcmp(n->u.st.term, "word"))
165 else if (!strcmp(n->u.st.index, "distance"))
167 if (!strcmp(n->u.st.relation, "<="))
168 distance = atoi(n->u.st.term);
169 else if (!strcmp(n->u.st.relation, "<"))
170 distance = atoi(n->u.st.term) - 1;
174 else if (!strcmp(n->u.st.index, "unordered"))
178 else if (!strcmp(n->u.st.index, "ordered"))
185 pr(ordered ? "!" : "%", client_data);
189 sprintf(x, "%d", distance);
193 pr(" ", client_data);
195 r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
196 pr(")", client_data);
200 static int cql_to_ccl_r(struct cql_node *cn,
201 void (*pr)(const char *buf, void *client_data),
210 return node(cn, pr, client_data);
212 return bool(cn, pr, client_data);
214 return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
219 int cql_to_ccl(struct cql_node *cn,
220 void (*pr)(const char *buf, void *client_data),
223 return cql_to_ccl_r(cn, pr, client_data);
226 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
228 cql_to_ccl(cn, cql_fputs, f);
231 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
233 struct cql_buf_write_info info;
238 r = cql_to_ccl(cn, cql_buf_write_handler, &info);
240 info.buf[info.off] = '\0';
242 return -2; /* buffer overflow */
249 * c-file-style: "Stroustrup"
250 * indent-tabs-mode: nil
252 * vim: shiftwidth=4 tabstop=8 expandtab