Fixed bug #830: pkg-config support. YAZ installs yaz.pc for Debian
[yaz-moved-to-github.git] / src / cqlutil.c
1 /* $Id: cqlutil.c,v 1.11 2007-01-03 08:42:15 adam Exp $
2    Copyright (C) 1995-2007, Index Data ApS
3    Index Data Aps
4
5 This file is part of the YAZ toolkit.
6
7 See the file LICENSE for details.
8 */
9
10 /**
11  * \file cqlutil.c
12  * \brief Implements CQL tree node utilities.
13  */
14
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include <yaz/cql.h>
19
20 void cql_fputs(const char *buf, void *client_data)
21 {
22     FILE *f = (FILE *) client_data;
23     fputs(buf, f);
24 }
25
26 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp)
27 {
28     struct cql_node *cn = 0;
29
30     if (!cp)
31         return 0;
32     switch (cp->which)
33     {
34     case CQL_NODE_ST:
35         cn = cql_node_mk_sc(nmem, cp->u.st.index,
36                             cp->u.st.relation,
37                             cp->u.st.term);
38         cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers);
39         cn->u.st.index_uri = cp->u.st.index_uri ? 
40             nmem_strdup(nmem, cp->u.st.index_uri) : 0;
41         cn->u.st.relation_uri = cp->u.st.relation_uri ?
42             nmem_strdup(nmem, cp->u.st.relation_uri) : 0;
43         break;
44     case CQL_NODE_BOOL:
45         cn = cql_node_mk_boolean(nmem, cp->u.boolean.value);
46         cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left);
47         cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right);
48     }
49     return cn;
50 }
51
52 struct cql_node *cql_node_mk_sc(NMEM nmem,
53                                 const char *index,
54                                 const char *relation,
55                                 const char *term)
56 {
57     struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
58     p->which = CQL_NODE_ST;
59     p->u.st.index = 0;
60     if (index)
61         p->u.st.index = nmem_strdup(nmem, index);
62     p->u.st.index_uri = 0;
63     p->u.st.term = 0;
64     if (term)
65         p->u.st.term = nmem_strdup(nmem, term);
66     p->u.st.relation = 0;
67     if (relation)
68         p->u.st.relation = nmem_strdup(nmem, relation);
69     p->u.st.relation_uri = 0;
70     p->u.st.modifiers = 0;
71     return p;
72 }
73
74 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
75 {
76     struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
77     p->which = CQL_NODE_BOOL;
78     p->u.boolean.value = 0;
79     if (op)
80         p->u.boolean.value = nmem_strdup(nmem, op);
81     p->u.boolean.left = 0;
82     p->u.boolean.right = 0;
83     p->u.boolean.modifiers = 0;
84     return p;
85 }
86
87 const char *cql_uri(void)
88 {
89     return "info:srw/cql-context-set/1/cql-v1.1";
90 }
91
92 struct cql_node *cql_apply_prefix(NMEM nmem,
93                                   struct cql_node *n, const char *prefix,
94                                   const char *uri)
95 {
96     if (n->which == CQL_NODE_ST)
97     {
98         if (!n->u.st.index_uri && n->u.st.index)
99         {   /* not yet resolved.. */
100             const char *cp = strchr(n->u.st.index, '.');
101             if (prefix && cp && 
102                 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
103                 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
104             {
105                 char *nval = nmem_strdup(nmem, cp+1);
106                 n->u.st.index_uri = nmem_strdup(nmem, uri);
107                 n->u.st.index = nval;
108             }
109             else if (!prefix && !cp)
110             {
111                 n->u.st.index_uri = nmem_strdup(nmem, uri);
112             }
113         }
114         if (!n->u.st.relation_uri && n->u.st.relation)
115         {
116             const char *cp = strchr(n->u.st.relation, '.');
117             if (prefix && cp &&
118                 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
119                 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
120             {
121                 char *nval = nmem_strdup(nmem, cp+1);
122                 n->u.st.relation_uri = nmem_strdup(nmem, uri);
123                 n->u.st.relation = nval;
124             }
125         }
126     }
127     else if (n->which == CQL_NODE_BOOL)
128     {
129         cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
130         cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
131     }
132     return n;
133 }
134
135 void cql_node_destroy(struct cql_node *cn)
136 {
137     if (!cn)
138         return;
139     switch (cn->which)
140     {
141     case CQL_NODE_ST:
142         cql_node_destroy(cn->u.st.modifiers);
143         break;
144     case CQL_NODE_BOOL:
145         cql_node_destroy(cn->u.boolean.left);
146         cql_node_destroy(cn->u.boolean.right);
147         cql_node_destroy(cn->u.boolean.modifiers);
148     }
149 }
150
151 int cql_strcmp(const char *s1, const char *s2)
152 {
153     while (*s1 && *s2)
154     {
155         int c1 = *s1++;
156         int c2 = *s2++;
157         if (c1 >= 'A' && c1 <= 'Z')
158             c1 = c1 + ('a' - 'A');
159         if (c2 >= 'A' && c2 <= 'Z')
160             c2 = c2 + ('a' - 'A');
161         if (c1 != c2)
162             return c1 - c2;
163     }
164     return *s1 - *s2;
165 }
166
167 int cql_strncmp(const char *s1, const char *s2, size_t n)
168 {
169     while (*s1 && *s2 && n)
170     {
171         int c1 = *s1++;
172         int c2 = *s2++;
173         if (c1 >= 'A' && c1 <= 'Z')
174             c1 = c1 + ('a' - 'A');
175         if (c2 >= 'A' && c2 <= 'Z')
176             c2 = c2 + ('a' - 'A');
177         if (c1 != c2)
178             return c1 - c2;
179         --n;
180     }
181     if (!n)
182         return 0;
183     return *s1 - *s2;
184 }
185
186 /*
187  * Local variables:
188  * c-basic-offset: 4
189  * indent-tabs-mode: nil
190  * End:
191  * vim: shiftwidth=4 tabstop=8 expandtab
192  */
193