-/* $Id: cqlutil.c,v 1.1 2003-10-27 12:21:30 adam Exp $
- Copyright (C) 2002-2003
+/* $Id: cqlutil.c,v 1.3 2004-03-15 21:39:06 adam Exp $
+ Copyright (C) 2002-2004
Index Data Aps
This file is part of the YAZ toolkit.
#include <stdlib.h>
#include <string.h>
+#include <yaz/xmalloc.h>
#include <yaz/cql.h>
void cql_fputs(const char *buf, void *client_data)
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);
- break;
- case CQL_NODE_MOD:
- cn = cql_node_mk_mod(cp->u.mod.name,
- cp->u.mod.value);
- cn->u.mod.next = cql_node_dup(cp->u.mod.next);
+ cn->u.st.index_uri = cp->u.st.index_uri ?
+ xstrdup(cp->u.st.index_uri) : 0;
+ cn->u.st.relation_uri = cp->u.st.relation_uri ?
+ xstrdup(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);
}
return cn;
}
const char *relation,
const char *term)
{
- struct cql_node *p = (struct cql_node *) malloc(sizeof(*p));
+ struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
p->which = CQL_NODE_ST;
p->u.st.index = 0;
if (index)
- p->u.st.index = strdup(index);
+ p->u.st.index = xstrdup(index);
+ p->u.st.index_uri = 0;
p->u.st.term = 0;
if (term)
- p->u.st.term = strdup(term);
+ p->u.st.term = xstrdup(term);
p->u.st.relation = 0;
if (relation)
- p->u.st.relation = strdup(relation);
+ p->u.st.relation = xstrdup(relation);
+ p->u.st.relation_uri = 0;
p->u.st.modifiers = 0;
- p->u.st.prefixes = 0;
- return p;
-}
-
-struct cql_node *cql_node_mk_mod(const char *name,
- const char *value)
-{
- struct cql_node *p = (struct cql_node *) malloc(sizeof(*p));
- p->which = CQL_NODE_MOD;
-
- p->u.mod.name = 0;
- if (name)
- p->u.mod.name = strdup(name);
- p->u.mod.value = 0;
- if (value)
- p->u.mod.value = strdup(value);
- p->u.mod.next = 0;
return p;
}
struct cql_node *cql_node_mk_boolean(const char *op)
{
- struct cql_node *p = (struct cql_node *) malloc(sizeof(*p));
+ struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
p->which = CQL_NODE_BOOL;
p->u.boolean.value = 0;
if (op)
- p->u.boolean.value = strdup(op);
+ p->u.boolean.value = xstrdup(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)
+const char *cql_uri()
{
- struct cql_node **cpp = 0;
- if (n->which == CQL_NODE_ST)
- {
- cpp = &n->u.st.prefixes;
- }
- else if (n->which == CQL_NODE_BOOL)
- {
- cpp = &n->u.boolean.prefixes;
- }
- if (cpp)
- {
- struct cql_node *cp = cql_node_mk_mod(prefix, uri);
- cp->u.mod.next = *cpp;
- *cpp = cp;
- }
- return n;
+ return "info:srw/cql-context-set/1/cql-v1.1";
}
-struct cql_node *cql_node_mk_proxargs(const char *relation,
- const char *distance,
- const char *unit,
- const char *ordering)
+struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix,
+ const char *uri)
{
- struct cql_node *m = 0, *m1;
-
- if (ordering && *ordering)
- m = cql_node_mk_mod("ordering", ordering);
- if (unit && *unit)
- {
- m1 = cql_node_mk_mod("unit", unit);
- m1->u.mod.next = m;
- m = m1;
- }
- if (distance && *distance)
+ if (n->which == CQL_NODE_ST)
{
- m1 = cql_node_mk_mod("distance", distance);
- m1->u.mod.next = m;
- m = m1;
+ 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 &&
+ !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);
+ n->u.st.index = nval;
+ }
+ else if (!prefix && !cp)
+ {
+ n->u.st.index_uri = xstrdup(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 &&
+ !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);
+ n->u.st.relation = nval;
+ }
+ }
}
- if (relation && *relation)
+ else if (n->which == CQL_NODE_BOOL)
{
- m1 = cql_node_mk_mod("relation", relation);
- m1->u.mod.next = m;
- m = m1;
+ cql_apply_prefix(n->u.boolean.left, prefix, uri);
+ cql_apply_prefix(n->u.boolean.right, prefix, uri);
}
- return m;
+ return n;
}
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);
+ 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);
- cql_node_destroy(cn->u.st.prefixes);
- break;
- case CQL_NODE_MOD:
- free (cn->u.mod.name);
- free (cn->u.mod.value);
- cql_node_destroy(cn->u.mod.next);
break;
case CQL_NODE_BOOL:
- free (cn->u.boolean.value);
+ 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.prefixes);
+ cql_node_destroy(cn->u.boolean.modifiers);
}
- free (cn);
+ xfree (cn);
}
-