X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fcql2ccl.c;h=54b16e733c56c970cfdbe676080e4611d0176d1f;hb=fda1ca39e89e4c7f3479c0a9d2a1e38acfdb46bb;hp=5f2011b4fee925d499ee8eab1d5c2d97b8a32185;hpb=2cf1b669768e41118f6937dd0afab37c8ed586a8;p=yaz-moved-to-github.git diff --git a/src/cql2ccl.c b/src/cql2ccl.c index 5f2011b..54b16e7 100644 --- a/src/cql2ccl.c +++ b/src/cql2ccl.c @@ -3,7 +3,7 @@ * See the file LICENSE for details. */ /** - * \file xcqlutil.c + * \file cql2ccl.c * \brief Implements CQL to XCQL conversion. */ #if HAVE_CONFIG_H @@ -21,14 +21,71 @@ static int cql_to_ccl_r(struct cql_node *cn, void *client_data); static void pr_term(struct cql_node *cn, - void (*pr)(const char *buf, void *client_data), - void *client_data) + void (*pr)(const char *buf, void *client_data), + void *client_data) { while (cn) { - pr("\"", client_data); - pr(cn->u.st.term, client_data); - pr("\"", client_data); + if (! *cn->u.st.term) /* empty term special case */ + pr("\"\"", client_data); + else + { + const char *cp; + int quote_mode = 0; + for (cp = cn->u.st.term; *cp; cp++) + { + char x[4]; + + if (*cp == '\\' && cp[1]) + { + if (!quote_mode) + { + pr("\"", client_data); + quote_mode = 1; + } + cp++; + if (*cp == '\"' || *cp == '\\') + pr("\\\"", client_data); + else + { + x[0] = *cp; + x[1] = '\0'; + pr(x, client_data); + } + } + else if (*cp == '*') + { + if (quote_mode) + { + pr("\"", client_data); + quote_mode = 0; + } + pr("?", client_data); + } + else if (*cp == '?') + { + if (quote_mode) + { + pr("\"", client_data); + quote_mode = 0; + } + pr("#", client_data); + } + else + { + if (!quote_mode) + { + pr("\"", client_data); + quote_mode = 1; + } + x[0] = *cp; + x[1] = '\0'; + pr(x, client_data); + } + } + if (quote_mode) + pr("\"", client_data); + } if (cn->u.st.extra_terms) pr(" ", client_data); cn = cn->u.st.extra_terms; @@ -98,7 +155,10 @@ static int node(struct cql_node *cn, while (*cp && *cp != ' ') { char x[2]; - x[0] = *cp; + if (*cp == '*') + x[0] = '?'; + else + x[0] = *cp; x[1] = '\0'; pr(x, client_data); cp++; @@ -111,7 +171,6 @@ static int node(struct cql_node *cn, pr(split_op, client_data); pr(" ", client_data); } - return -1; } return 0; } @@ -121,6 +180,7 @@ static int bool(struct cql_node *cn, void (*pr)(const char *buf, void *client_data), void *client_data) { + char *value = cn->u.boolean.value; int r; pr("(", client_data); @@ -129,7 +189,54 @@ static int bool(struct cql_node *cn, return r; pr(" ", client_data); - pr(cn->u.boolean.value, client_data); + + if (strcmp(value, "prox")) + { /* not proximity. assuming boolean */ + pr(value, client_data); + } + else + { + struct cql_node *n = cn->u.boolean.modifiers; + int ordered = 0; + int distance = 1; + for (; n ; n = n->u.st.modifiers) + if (n->which == CQL_NODE_ST) + { + if (!strcmp(n->u.st.index, "unit")) + { + if (!strcmp(n->u.st.term, "word")) + ; + else + return -1; + } + else if (!strcmp(n->u.st.index, "distance")) + { + if (!strcmp(n->u.st.relation, "<=")) + distance = atoi(n->u.st.term); + else if (!strcmp(n->u.st.relation, "<")) + distance = atoi(n->u.st.term) - 1; + else + return -1; + } + else if (!strcmp(n->u.st.index, "unordered")) + { + ordered = 0; + } + else if (!strcmp(n->u.st.index, "ordered")) + { + ordered = 1; + } + else + return -1; + } + pr(ordered ? "!" : "%", client_data); + if (distance != 1) + { + char x[40]; + sprintf(x, "%d", distance); + pr(x, client_data); + } + } pr(" ", client_data); r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data); @@ -168,7 +275,20 @@ void cql_to_ccl_stdio(struct cql_node *cn, FILE *f) cql_to_ccl(cn, cql_fputs, f); } - +int cql_to_ccl_buf(struct cql_node *cn, char *out, int max) +{ + struct cql_buf_write_info info; + int r; + info.off = 0; + info.max = max; + info.buf = out; + r = cql_to_ccl(cn, cql_buf_write_handler, &info); + if (info.off >= 0) + info.buf[info.off] = '\0'; + else + return -2; /* buffer overflow */ + return r; +} /* * Local variables: