X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fcqlutil.c;h=c0c256f03b0baff5a3325725e4ad930c7d982483;hp=1b842b1c165abc4248357cfca8ce7b5f9e208c33;hb=dd37dbb261ac22f4d12bae1ec70236f51763051f;hpb=42f18e58ed67f77727cde23bcee44dbd32ed242a diff --git a/src/cqlutil.c b/src/cqlutil.c index 1b842b1..c0c256f 100644 --- a/src/cqlutil.c +++ b/src/cqlutil.c @@ -1,11 +1,14 @@ -/* $Id: cqlutil.c,v 1.2 2004-03-10 16:34:29 adam Exp $ - Copyright (C) 2002-2004 - Index Data Aps - -This file is part of the YAZ toolkit. - -See the file LICENSE. -*/ +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data + * See the file LICENSE for details. + */ +/** + * \file cqlutil.c + * \brief Implements CQL tree node utilities. + */ +#if HAVE_CONFIG_H +#include +#endif #include #include @@ -18,7 +21,7 @@ void cql_fputs(const char *buf, void *client_data) fputs(buf, f); } -struct cql_node *cql_node_dup (struct cql_node *cp) +struct cql_node *cql_node_dup(NMEM nmem, struct cql_node *cp) { struct cql_node *cn = 0; @@ -27,72 +30,126 @@ struct cql_node *cql_node_dup (struct cql_node *cp) switch (cp->which) { case CQL_NODE_ST: - cn = cql_node_mk_sc(cp->u.st.index, + cn = cql_node_mk_sc(nmem, cp->u.st.index, cp->u.st.relation, cp->u.st.term); - cn->u.st.modifiers = cql_node_dup(cp->u.st.modifiers); - cn->u.st.prefixes = cql_node_dup(cp->u.st.prefixes); + cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers); + cn->u.st.index_uri = cp->u.st.index_uri ? + nmem_strdup(nmem, cp->u.st.index_uri) : 0; + cn->u.st.relation_uri = cp->u.st.relation_uri ? + nmem_strdup(nmem, cp->u.st.relation_uri) : 0; break; case CQL_NODE_BOOL: - cn = cql_node_mk_boolean(cp->u.boolean.value); - cn->u.boolean.left = cql_node_dup(cp->u.boolean.left); - cn->u.boolean.right = cql_node_dup(cp->u.boolean.right); - cn->u.boolean.prefixes = cql_node_dup(cp->u.boolean.prefixes); + cn = cql_node_mk_boolean(nmem, cp->u.boolean.value); + cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left); + cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right); + break; + case CQL_NODE_SORT: + cn = cql_node_mk_sort(nmem, cp->u.sort.index, cp->u.sort.modifiers); + cn->u.sort.next = cql_node_dup(nmem, cp->u.sort.next); + cn->u.sort.search = cql_node_dup(nmem, cp->u.sort.search); } return cn; } -struct cql_node *cql_node_mk_sc(const char *index, +struct cql_node *cql_node_mk_sc(NMEM nmem, + const char *index, const char *relation, const char *term) { - struct cql_node *p = (struct cql_node *) malloc(sizeof(*p)); + struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p)); p->which = CQL_NODE_ST; p->u.st.index = 0; if (index) - p->u.st.index = strdup(index); + p->u.st.index = nmem_strdup(nmem, index); + p->u.st.index_uri = 0; p->u.st.term = 0; if (term) - p->u.st.term = strdup(term); + p->u.st.term = nmem_strdup(nmem, term); p->u.st.relation = 0; if (relation) - p->u.st.relation = strdup(relation); + p->u.st.relation = nmem_strdup(nmem, relation); + p->u.st.relation_uri = 0; p->u.st.modifiers = 0; - p->u.st.prefixes = 0; + p->u.st.extra_terms = 0; return p; } -struct cql_node *cql_node_mk_boolean(const char *op) +struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op) { - struct cql_node *p = (struct cql_node *) malloc(sizeof(*p)); + struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p)); p->which = CQL_NODE_BOOL; p->u.boolean.value = 0; if (op) - p->u.boolean.value = strdup(op); + p->u.boolean.value = nmem_strdup(nmem, op); p->u.boolean.left = 0; p->u.boolean.right = 0; p->u.boolean.modifiers = 0; - p->u.boolean.prefixes = 0; return p; } -struct cql_node *cql_node_prefix(struct cql_node *n, const char *prefix, - const char *uri) +struct cql_node *cql_node_mk_sort(NMEM nmem, const char *index, + struct cql_node *modifiers) +{ + struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p)); + p->which = CQL_NODE_SORT; + p->u.sort.index = 0; + if (index) + p->u.sort.index = nmem_strdup(nmem, index); + p->u.sort.modifiers = modifiers; + p->u.sort.next = 0; + p->u.sort.search = 0; + return p; +} + +const char *cql_uri(void) +{ + return "info:srw/cql-context-set/1/cql-v1.2"; +} + +struct cql_node *cql_apply_prefix(NMEM nmem, + struct cql_node *n, const char *prefix, + const char *uri) { - struct cql_node **cpp = 0; if (n->which == CQL_NODE_ST) { - cpp = &n->u.st.prefixes; + if (!n->u.st.index_uri && n->u.st.index) + { /* not yet resolved.. */ + const char *cp = strchr(n->u.st.index, '.'); + if (prefix && cp && + strlen(prefix) == (size_t) (cp - n->u.st.index) && + !cql_strncmp(n->u.st.index, prefix, strlen(prefix))) + { + char *nval = nmem_strdup(nmem, cp+1); + n->u.st.index_uri = nmem_strdup(nmem, uri); + n->u.st.index = nval; + } + else if (!prefix && !cp) + { + n->u.st.index_uri = nmem_strdup(nmem, uri); + } + } + if (!n->u.st.relation_uri && n->u.st.relation) + { + const char *cp = strchr(n->u.st.relation, '.'); + if (prefix && cp && + strlen(prefix) == (size_t)(cp - n->u.st.relation) && + !cql_strncmp(n->u.st.relation, prefix, strlen(prefix))) + { + char *nval = nmem_strdup(nmem, cp+1); + n->u.st.relation_uri = nmem_strdup(nmem, uri); + n->u.st.relation = nval; + } + } } else if (n->which == CQL_NODE_BOOL) { - cpp = &n->u.boolean.prefixes; + cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri); + cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri); } - if (cpp) + else if (n->which == CQL_NODE_SORT) { - struct cql_node *cp = cql_node_mk_sc(prefix, "=", uri); - cp->u.st.modifiers = *cpp; - *cpp = cp; + cql_apply_prefix(nmem, n->u.sort.search, prefix, uri); } return n; } @@ -104,19 +161,61 @@ void cql_node_destroy(struct cql_node *cn) switch (cn->which) { case CQL_NODE_ST: - free (cn->u.st.index); - free (cn->u.st.relation); - free (cn->u.st.term); cql_node_destroy(cn->u.st.modifiers); - cql_node_destroy(cn->u.st.prefixes); break; case CQL_NODE_BOOL: - free (cn->u.boolean.value); cql_node_destroy(cn->u.boolean.left); cql_node_destroy(cn->u.boolean.right); - cql_node_destroy(cn->u.boolean.prefixes); cql_node_destroy(cn->u.boolean.modifiers); + break; + case CQL_NODE_SORT: + cql_node_destroy(cn->u.sort.search); + cql_node_destroy(cn->u.sort.next); + cql_node_destroy(cn->u.sort.modifiers); } - free (cn); } +int cql_strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s2) + { + int c1 = *s1++; + int c2 = *s2++; + if (c1 >= 'A' && c1 <= 'Z') + c1 = c1 + ('a' - 'A'); + if (c2 >= 'A' && c2 <= 'Z') + c2 = c2 + ('a' - 'A'); + if (c1 != c2) + return c1 - c2; + } + return *s1 - *s2; +} + +int cql_strncmp(const char *s1, const char *s2, size_t n) +{ + while (*s1 && *s2 && n) + { + int c1 = *s1++; + int c2 = *s2++; + if (c1 >= 'A' && c1 <= 'Z') + c1 = c1 + ('a' - 'A'); + if (c2 >= 'A' && c2 <= 'Z') + c2 = c2 + ('a' - 'A'); + if (c1 != c2) + return c1 - c2; + --n; + } + if (!n) + return 0; + return *s1 - *s2; +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +