YAZ  5.23.1
cqlutil.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12 
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include <yaz/cql.h>
17 
18 void cql_fputs(const char *buf, void *client_data)
19 {
20  FILE *f = (FILE *) client_data;
21  fputs(buf, f);
22 }
23 
24 struct cql_node *cql_node_dup(NMEM nmem, struct cql_node *cp)
25 {
26  struct cql_node *cn = 0;
27 
28  if (!cp)
29  return 0;
30  switch (cp->which)
31  {
32  case CQL_NODE_ST:
33  cn = cql_node_mk_sc(nmem, cp->u.st.index,
34  cp->u.st.relation,
35  cp->u.st.term);
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;
41  break;
42  case CQL_NODE_BOOL:
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);
46  break;
47  case CQL_NODE_SORT:
48  cn = cql_node_mk_sort(nmem, cp->u.sort.index, cp->u.sort.modifiers);
49  cn->u.sort.next = cql_node_dup(nmem, cp->u.sort.next);
50  cn->u.sort.search = cql_node_dup(nmem, cp->u.sort.search);
51  }
52  return cn;
53 }
54 
56  const char *index,
57  const char *relation,
58  const char *term)
59 {
60  struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
61  p->which = CQL_NODE_ST;
62  p->u.st.index = 0;
63  if (index)
64  p->u.st.index = nmem_strdup(nmem, index);
65  p->u.st.index_uri = 0;
66  p->u.st.term = 0;
67  if (term)
68  p->u.st.term = nmem_strdup(nmem, term);
69  p->u.st.relation = 0;
70  if (relation)
71  p->u.st.relation = nmem_strdup(nmem, relation);
72  p->u.st.relation_uri = 0;
73  p->u.st.modifiers = 0;
74  p->u.st.extra_terms = 0;
75  return p;
76 }
77 
78 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
79 {
80  struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
81  p->which = CQL_NODE_BOOL;
82  p->u.boolean.value = 0;
83  if (op)
84  p->u.boolean.value = nmem_strdup(nmem, op);
85  p->u.boolean.left = 0;
86  p->u.boolean.right = 0;
87  p->u.boolean.modifiers = 0;
88  return p;
89 }
90 
91 struct cql_node *cql_node_mk_sort(NMEM nmem, const char *index,
92  struct cql_node *modifiers)
93 {
94  struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
95  p->which = CQL_NODE_SORT;
96  p->u.sort.index = 0;
97  if (index)
98  p->u.sort.index = nmem_strdup(nmem, index);
99  p->u.sort.modifiers = modifiers;
100  p->u.sort.next = 0;
101  p->u.sort.search = 0;
102  return p;
103 }
104 
105 const char *cql_uri(void)
106 {
107  return "info:srw/cql-context-set/1/cql-v1.2";
108 }
109 
111  struct cql_node *n, const char *prefix,
112  const char *uri)
113 {
114  if (n->which == CQL_NODE_ST)
115  {
116  if (!n->u.st.index_uri && n->u.st.index)
117  { /* not yet resolved.. */
118  const char *cp = strchr(n->u.st.index, '.');
119  if (prefix && cp &&
120  strlen(prefix) == (size_t) (cp - n->u.st.index) &&
121  !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
122  {
123  char *nval = nmem_strdup(nmem, cp+1);
124  n->u.st.index_uri = nmem_strdup(nmem, uri);
125  n->u.st.index = nval;
126  }
127  else if (!prefix && !cp)
128  {
129  n->u.st.index_uri = nmem_strdup(nmem, uri);
130  }
131  }
132  if (!n->u.st.relation_uri && n->u.st.relation)
133  {
134  const char *cp = strchr(n->u.st.relation, '.');
135  if (prefix && cp &&
136  strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
137  !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
138  {
139  char *nval = nmem_strdup(nmem, cp+1);
140  n->u.st.relation_uri = nmem_strdup(nmem, uri);
141  n->u.st.relation = nval;
142  }
143  }
144  struct cql_node *mod;
145  for (mod = n->u.st.modifiers; mod; mod = mod->u.st.modifiers)
146  {
147  if (!mod->u.st.index_uri && mod->u.st.index)
148  {
149  const char *cp = strchr(mod->u.st.index, '.');
150  if (prefix && cp &&
151  strlen(prefix) == (size_t) (cp - mod->u.st.index) &&
152  !cql_strncmp(mod->u.st.index, prefix, strlen(prefix)))
153  {
154  char *nval = nmem_strdup(nmem, cp+1);
155  mod->u.st.index_uri = nmem_strdup(nmem, uri);
156  mod->u.st.index = nval;
157  }
158  }
159  }
160  }
161  else if (n->which == CQL_NODE_BOOL)
162  {
163  cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
164  cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
165  }
166  else if (n->which == CQL_NODE_SORT)
167  {
168  cql_apply_prefix(nmem, n->u.sort.search, prefix, uri);
169  }
170  return n;
171 }
172 
173 void cql_node_destroy(struct cql_node *cn)
174 {
175  if (!cn)
176  return;
177  switch (cn->which)
178  {
179  case CQL_NODE_ST:
180  cql_node_destroy(cn->u.st.modifiers);
181  break;
182  case CQL_NODE_BOOL:
183  cql_node_destroy(cn->u.boolean.left);
184  cql_node_destroy(cn->u.boolean.right);
185  cql_node_destroy(cn->u.boolean.modifiers);
186  break;
187  case CQL_NODE_SORT:
188  cql_node_destroy(cn->u.sort.search);
189  cql_node_destroy(cn->u.sort.next);
190  cql_node_destroy(cn->u.sort.modifiers);
191  }
192 }
193 
194 int cql_strcmp(const char *s1, const char *s2)
195 {
196  while (*s1 && *s2)
197  {
198  int c1 = *s1++;
199  int c2 = *s2++;
200  if (c1 >= 'A' && c1 <= 'Z')
201  c1 = c1 + ('a' - 'A');
202  if (c2 >= 'A' && c2 <= 'Z')
203  c2 = c2 + ('a' - 'A');
204  if (c1 != c2)
205  return c1 - c2;
206  }
207  return *s1 - *s2;
208 }
209 
210 int cql_strncmp(const char *s1, const char *s2, size_t n)
211 {
212  while (*s1 && *s2 && n)
213  {
214  int c1 = *s1++;
215  int c2 = *s2++;
216  if (c1 >= 'A' && c1 <= 'Z')
217  c1 = c1 + ('a' - 'A');
218  if (c2 >= 'A' && c2 <= 'Z')
219  c2 = c2 + ('a' - 'A');
220  if (c1 != c2)
221  return c1 - c2;
222  --n;
223  }
224  if (!n)
225  return 0;
226  return *s1 - *s2;
227 }
228 
229 /*
230  * Local variables:
231  * c-basic-offset: 4
232  * c-file-style: "Stroustrup"
233  * indent-tabs-mode: nil
234  * End:
235  * vim: shiftwidth=4 tabstop=8 expandtab
236  */
237 
#define CQL_NODE_SORT
Node type: sortby single spec.
Definition: cql.h:115
struct cql_node::@13::@14 st
struct cql_node * cql_node_dup(NMEM nmem, struct cql_node *cp)
Definition: cqlutil.c:24
struct cql_node * modifiers
Definition: cql.h:136
void * nmem_malloc(NMEM n, size_t size)
allocates memory block on NMEM handle
Definition: nmem.c:145
char * term
Definition: cql.h:130
union cql_node::@13 u
Header with public definitions about CQL.
#define CQL_NODE_ST
Node type: search term.
Definition: cql.h:111
const char * cql_uri(void)
returns the standard CQL context set URI.
Definition: cqlutil.c:105
char * nmem_strdup(NMEM mem, const char *src)
allocates string on NMEM handle (similar strdup)
Definition: nmemsdup.c:18
int cql_strncmp(const char *s1, const char *s2, size_t n)
compares two CQL strings (ala strncmp)
Definition: cqlutil.c:210
struct cql_node * cql_node_mk_boolean(NMEM nmem, const char *op)
creates a boolean node.
Definition: cqlutil.c:78
int which
Definition: cql.h:121
void cql_node_destroy(struct cql_node *cn)
destroys a node and its children.
Definition: cqlutil.c:173
struct cql_node * cql_apply_prefix(NMEM nmem, struct cql_node *n, const char *prefix, const char *uri)
applies a prefix+uri to "unresolved" index and relation URIs. "unresolved" URIs are those nodes where...
Definition: cqlutil.c:110
char * index
Definition: cql.h:126
char * buf
Definition: cql.h:173
void cql_fputs(const char *buf, void *client_data)
stream handle for file (used by cql_to_xml_stdio)
Definition: cqlutil.c:18
char * relation
Definition: cql.h:132
struct cql_node::@13::@16 sort
int cql_strcmp(const char *s1, const char *s2)
compares two CQL strings (ala strcmp)
Definition: cqlutil.c:194
#define CQL_NODE_BOOL
Node type: boolean.
Definition: cql.h:113
struct cql_node * cql_node_mk_sc(NMEM nmem, const char *index, const char *relation, const char *term)
creates a search clause node (st).
Definition: cqlutil.c:55
struct cql_node * cql_node_mk_sort(NMEM nmem, const char *index, struct cql_node *modifiers)
creates a sort single spec node.
Definition: cqlutil.c:91
struct cql_node::@13::@15 boolean
CQL parse tree (node)
Definition: cql.h:119