X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fcqlutil.c;h=511fa9f7c41adc80b9356135f66843dd69271533;hb=60a702f390f7e2addfdab79f2328db3ba2897c8b;hp=d915c59a7a56fa64444fa8c4fe622713aed693f7;hpb=00106dc85fcaa3b02f9a0d471ea90a594bf3a175;p=yaz-moved-to-github.git diff --git a/src/cqlutil.c b/src/cqlutil.c index d915c59..511fa9f 100644 --- a/src/cqlutil.c +++ b/src/cqlutil.c @@ -1,16 +1,20 @@ -/* $Id: cqlutil.c,v 1.3 2004-03-15 21:39:06 adam Exp $ - Copyright (C) 2002-2004 +/* $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. +See the file LICENSE for details. */ +/** + * \file cqlutil.c + * \brief Implements CQL tree node utilities. + */ + #include #include -#include #include void cql_fputs(const char *buf, void *client_data) @@ -19,7 +23,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; @@ -28,51 +32,52 @@ 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.modifiers = cql_node_dup(nmem, cp->u.st.modifiers); cn->u.st.index_uri = cp->u.st.index_uri ? - xstrdup(cp->u.st.index_uri) : 0; + nmem_strdup(nmem, cp->u.st.index_uri) : 0; cn->u.st.relation_uri = cp->u.st.relation_uri ? - xstrdup(cp->u.st.relation_uri) : 0; + 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 = 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); } 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 *) xmalloc(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 = xstrdup(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 = xstrdup(term); + p->u.st.term = nmem_strdup(nmem, term); p->u.st.relation = 0; if (relation) - p->u.st.relation = xstrdup(relation); + p->u.st.relation = nmem_strdup(nmem, relation); p->u.st.relation_uri = 0; p->u.st.modifiers = 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 *) xmalloc(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 = xstrdup(op); + p->u.boolean.value = nmem_strdup(nmem, op); p->u.boolean.left = 0; p->u.boolean.right = 0; p->u.boolean.modifiers = 0; @@ -84,7 +89,8 @@ const char *cql_uri() return "info:srw/cql-context-set/1/cql-v1.1"; } -struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix, +struct cql_node *cql_apply_prefix(NMEM nmem, + struct cql_node *n, const char *prefix, const char *uri) { if (n->which == CQL_NODE_ST) @@ -92,36 +98,36 @@ struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix, 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) == cp - 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 = xstrdup(cp+1); - n->u.st.index_uri = xstrdup(uri); - xfree (n->u.st.index); + 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 = xstrdup(uri); + 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) == cp - 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 = xstrdup(cp+1); - n->u.st.relation_uri = xstrdup(uri); - xfree (n->u.st.relation); + 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(n->u.boolean.left, prefix, uri); - cql_apply_prefix(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; } @@ -133,18 +139,11 @@ void cql_node_destroy(struct cql_node *cn) switch (cn->which) { case CQL_NODE_ST: - xfree (cn->u.st.index); - xfree (cn->u.st.relation); - xfree (cn->u.st.term); - xfree (cn->u.st.index_uri); - xfree (cn->u.st.relation_uri); cql_node_destroy(cn->u.st.modifiers); break; case CQL_NODE_BOOL: - xfree (cn->u.boolean.value); cql_node_destroy(cn->u.boolean.left); cql_node_destroy(cn->u.boolean.right); cql_node_destroy(cn->u.boolean.modifiers); } - xfree (cn); }