From f9828584ad6d0f66eb85b2e6d8bca5a29d569dcb Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 27 Jun 2005 22:03:59 +0000 Subject: [PATCH] Case insensitive CQL relations/indexes/operations. --- doc/introduction.xml | 4 ++-- include/yaz/cql.h | 16 +++++++++++++++- src/cql.y | 10 +++++----- src/cqltransform.c | 20 ++++++++++---------- src/cqlutil.c | 42 +++++++++++++++++++++++++++++++++++++++--- 5 files changed, 71 insertions(+), 21 deletions(-) diff --git a/doc/introduction.xml b/doc/introduction.xml index cf19427..836a43c 100644 --- a/doc/introduction.xml +++ b/doc/introduction.xml @@ -1,11 +1,11 @@ - + Introduction &yaz; is a C/C++ library for information retrieval applications using the Z39.50/SRW/SRU protocols for information retrieval. - + Properties of &yaz;: diff --git a/include/yaz/cql.h b/include/yaz/cql.h index 3a6215a..d745ad0 100644 --- a/include/yaz/cql.h +++ b/include/yaz/cql.h @@ -1,4 +1,4 @@ -/* $Id: cql.h,v 1.12 2005-06-25 15:46:02 adam Exp $ +/* $Id: cql.h,v 1.13 2005-06-27 22:03:59 adam Exp $ Copyright (C) 1995-2005, Index Data ApS Index Data Aps @@ -281,6 +281,20 @@ const char *cql_strerror(int code); YAZ_EXPORT const char *cql_uri(); +/** + * Compares two CQL strings (for relations, operators, etc) + * (unfortunately defined as case-insensitive unlike XML etc) + */ +YAZ_EXPORT +int cql_strcmp(const char *s1, const char *s2); + +/** + * Compares two CQL strings at most n bytes + * (unfortunately defined as case-insensitive unlike XML etc) + */ +YAZ_EXPORT +int cql_strncmp(const char *s1, const char *s2, size_t n); + YAZ_END_CDECL #endif diff --git a/src/cql.y b/src/cql.y index 1086549..b5077d3 100644 --- a/src/cql.y +++ b/src/cql.y @@ -1,4 +1,4 @@ -/* $Id: cql.y,v 1.8 2004-11-01 20:13:02 adam Exp $ +/* $Id: cql.y,v 1.9 2005-06-27 22:03:59 adam Exp $ Copyright (C) 2002-2004 Index Data Aps @@ -299,13 +299,13 @@ int yylex(YYSTYPE *lval, void *vp) #endif if (c != 0) cp->ungetbyte(c, cp->client_data); - if (!strcmp(lval->buf, "and")) + if (!cql_strcmp(lval->buf, "and")) return AND; - if (!strcmp(lval->buf, "or")) + if (!cql_strcmp(lval->buf, "or")) return OR; - if (!strcmp(lval->buf, "not")) + if (!cql_strcmp(lval->buf, "not")) return NOT; - if (!strncmp(lval->buf, "prox", 4)) + if (!cql_strcmp(lval->buf, "prox")) return PROX; } return TERM; diff --git a/src/cqltransform.c b/src/cqltransform.c index 559b2d4..70c45e7 100644 --- a/src/cqltransform.c +++ b/src/cqltransform.c @@ -1,4 +1,4 @@ -/* $Id: cqltransform.c,v 1.15 2005-06-25 15:46:03 adam Exp $ +/* $Id: cqltransform.c,v 1.16 2005-06-27 22:03:59 adam Exp $ Copyright (C) 1995-2005, Index Data ApS Index Data Aps @@ -127,7 +127,7 @@ static const char *cql_lookup_property(cql_transform_t ct, for (e = ct->entry; e; e = e->next) { - if (!strcmp(e->pattern, pattern)) + if (!cql_strcmp(e->pattern, pattern)) return e->value; } return 0; @@ -383,7 +383,7 @@ void cql_transform_r(cql_transform_t ct, if (ns) { if (!strcmp(ns, cql_uri()) - && cn->u.st.index && !strcmp(cn->u.st.index, "resultSet")) + && cn->u.st.index && !cql_strcmp(cn->u.st.index, "resultSet")) { (*pr)("@set \"", client_data); (*pr)(cn->u.st.term, client_data); @@ -402,13 +402,13 @@ void cql_transform_r(cql_transform_t ct, ct->addinfo = 0; } } - if (cn->u.st.relation && !strcmp(cn->u.st.relation, "=")) + if (cn->u.st.relation && !cql_strcmp(cn->u.st.relation, "=")) cql_pr_attr(ct, "relation", "eq", "scr", pr, client_data, 19); - else if (cn->u.st.relation && !strcmp(cn->u.st.relation, "<=")) + else if (cn->u.st.relation && !cql_strcmp(cn->u.st.relation, "<=")) cql_pr_attr(ct, "relation", "le", "scr", pr, client_data, 19); - else if (cn->u.st.relation && !strcmp(cn->u.st.relation, ">=")) + else if (cn->u.st.relation && !cql_strcmp(cn->u.st.relation, ">=")) cql_pr_attr(ct, "relation", "ge", "scr", pr, client_data, 19); else @@ -425,11 +425,11 @@ void cql_transform_r(cql_transform_t ct, } cql_pr_attr(ct, "structure", cn->u.st.relation, 0, pr, client_data, 24); - if (cn->u.st.relation && !strcmp(cn->u.st.relation, "all")) + if (cn->u.st.relation && !cql_strcmp(cn->u.st.relation, "all")) { emit_wordlist(ct, cn, pr, client_data, "and"); } - else if (cn->u.st.relation && !strcmp(cn->u.st.relation, "any")) + else if (cn->u.st.relation && !cql_strcmp(cn->u.st.relation, "any")) { emit_wordlist(ct, cn, pr, client_data, "or"); } @@ -464,9 +464,9 @@ int cql_transform(cql_transform_t ct, for (e = ct->entry; e ; e = e->next) { - if (!memcmp(e->pattern, "set.", 4)) + if (!cql_strncmp(e->pattern, "set.", 4)) cql_apply_prefix(nmem, cn, e->pattern+4, e->value); - else if (!strcmp(e->pattern, "set")) + else if (!cql_strcmp(e->pattern, "set")) cql_apply_prefix(nmem, cn, 0, e->value); } cql_transform_r (ct, cn, pr, client_data); diff --git a/src/cqlutil.c b/src/cqlutil.c index 9fa3ae6..68daa0e 100644 --- a/src/cqlutil.c +++ b/src/cqlutil.c @@ -1,4 +1,4 @@ -/* $Id: cqlutil.c,v 1.8 2005-06-25 15:46:04 adam Exp $ +/* $Id: cqlutil.c,v 1.9 2005-06-27 22:03:59 adam Exp $ Copyright (C) 1995-2005, Index Data ApS Index Data Aps @@ -100,7 +100,7 @@ struct cql_node *cql_apply_prefix(NMEM nmem, 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))) + !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); @@ -116,7 +116,7 @@ struct cql_node *cql_apply_prefix(NMEM nmem, 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))) + !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); @@ -147,6 +147,42 @@ 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 -- 1.7.10.4