X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fcqlutil.c;h=c20eb7f89f416cad8c49e95cb25cf803f0a81dc8;hp=511fa9f7c41adc80b9356135f66843dd69271533;hb=0f073baf2183a54f2a5691e697fde68c9a5f50c5;hpb=4c176312acdc3444c9afc820f76a393e64668e52 diff --git a/src/cqlutil.c b/src/cqlutil.c index 511fa9f..c20eb7f 100644 --- a/src/cqlutil.c +++ b/src/cqlutil.c @@ -1,16 +1,14 @@ -/* $Id: cqlutil.c,v 1.7 2005-01-15 19:47:13 adam Exp $ - Copyright (C) 1995-2005, Index Data ApS - Index Data Aps - -This file is part of the YAZ toolkit. - -See the file LICENSE for details. -*/ - +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 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 @@ -36,10 +34,10 @@ struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp) cp->u.st.relation, cp->u.st.term); 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; + 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(nmem, cp->u.boolean.value); @@ -50,7 +48,7 @@ struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp) } struct cql_node *cql_node_mk_sc(NMEM nmem, - const char *index, + const char *index, const char *relation, const char *term) { @@ -68,6 +66,7 @@ struct cql_node *cql_node_mk_sc(NMEM nmem, p->u.st.relation = nmem_strdup(nmem, relation); p->u.st.relation_uri = 0; p->u.st.modifiers = 0; + p->u.st.extra_terms = 0; return p; } @@ -84,50 +83,50 @@ struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op) return p; } -const char *cql_uri() +const char *cql_uri(void) { - return "info:srw/cql-context-set/1/cql-v1.1"; + 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 *n, const char *prefix, + const char *uri) { if (n->which == CQL_NODE_ST) { - 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) && - !memcmp(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) && - !memcmp(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; - } - } + 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) { - cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri); - cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri); + cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri); + cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri); } return n; } @@ -147,3 +146,48 @@ void cql_node_destroy(struct cql_node *cn) cql_node_destroy(cn->u.boolean.modifiers); } } + +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 + */ +