1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CQL tree node utilities.
18 void cql_fputs(const char *buf, void *client_data)
20 FILE *f = (FILE *) client_data;
24 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp)
26 struct cql_node *cn = 0;
33 cn = cql_node_mk_sc(nmem, cp->u.st.index,
36 cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers);
37 cn->u.st.index_uri = cp->u.st.index_uri ?
38 nmem_strdup(nmem, cp->u.st.index_uri) : 0;
39 cn->u.st.relation_uri = cp->u.st.relation_uri ?
40 nmem_strdup(nmem, cp->u.st.relation_uri) : 0;
43 cn = cql_node_mk_boolean(nmem, cp->u.boolean.value);
44 cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left);
45 cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right);
50 struct cql_node *cql_node_mk_sc(NMEM nmem,
55 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
56 p->which = CQL_NODE_ST;
59 p->u.st.index = nmem_strdup(nmem, index);
60 p->u.st.index_uri = 0;
63 p->u.st.term = nmem_strdup(nmem, term);
66 p->u.st.relation = nmem_strdup(nmem, relation);
67 p->u.st.relation_uri = 0;
68 p->u.st.modifiers = 0;
69 p->u.st.extra_terms = 0;
73 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
75 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
76 p->which = CQL_NODE_BOOL;
77 p->u.boolean.value = 0;
79 p->u.boolean.value = nmem_strdup(nmem, op);
80 p->u.boolean.left = 0;
81 p->u.boolean.right = 0;
82 p->u.boolean.modifiers = 0;
86 const char *cql_uri(void)
88 return "info:srw/cql-context-set/1/cql-v1.2";
91 struct cql_node *cql_apply_prefix(NMEM nmem,
92 struct cql_node *n, const char *prefix,
95 if (n->which == CQL_NODE_ST)
97 if (!n->u.st.index_uri && n->u.st.index)
98 { /* not yet resolved.. */
99 const char *cp = strchr(n->u.st.index, '.');
101 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
102 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
104 char *nval = nmem_strdup(nmem, cp+1);
105 n->u.st.index_uri = nmem_strdup(nmem, uri);
106 n->u.st.index = nval;
108 else if (!prefix && !cp)
110 n->u.st.index_uri = nmem_strdup(nmem, uri);
113 if (!n->u.st.relation_uri && n->u.st.relation)
115 const char *cp = strchr(n->u.st.relation, '.');
117 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
118 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
120 char *nval = nmem_strdup(nmem, cp+1);
121 n->u.st.relation_uri = nmem_strdup(nmem, uri);
122 n->u.st.relation = nval;
126 else if (n->which == CQL_NODE_BOOL)
128 cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
129 cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
134 void cql_node_destroy(struct cql_node *cn)
141 cql_node_destroy(cn->u.st.modifiers);
144 cql_node_destroy(cn->u.boolean.left);
145 cql_node_destroy(cn->u.boolean.right);
146 cql_node_destroy(cn->u.boolean.modifiers);
150 int cql_strcmp(const char *s1, const char *s2)
156 if (c1 >= 'A' && c1 <= 'Z')
157 c1 = c1 + ('a' - 'A');
158 if (c2 >= 'A' && c2 <= 'Z')
159 c2 = c2 + ('a' - 'A');
166 int cql_strncmp(const char *s1, const char *s2, size_t n)
168 while (*s1 && *s2 && n)
172 if (c1 >= 'A' && c1 <= 'Z')
173 c1 = c1 + ('a' - 'A');
174 if (c2 >= 'A' && c2 <= 'Z')
175 c2 = c2 + ('a' - 'A');
188 * c-file-style: "Stroustrup"
189 * indent-tabs-mode: nil
191 * vim: shiftwidth=4 tabstop=8 expandtab