Avoid printf %.*s for arbitrary strings
[yaz-moved-to-github.git] / src / rpn2cql.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief Implements RPN to CQL conversion
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <yaz/rpn2cql.h>
17 #include <yaz/xmalloc.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz/z-core.h>
20 #include <yaz/wrbuf.h>
21
22 static void wrbuf_vputs(const char *buf, void *client_data)
23 {
24     wrbuf_write((WRBUF) client_data, buf, strlen(buf));
25 }
26
27 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
28 {
29     int j;
30     int server_choice = 1;
31     for (j = 0; j < attributes->num_attributes; j++)
32     {
33         Z_AttributeElement *ae = attributes->attributes[j];
34         if (*ae->attributeType == 1) /* use attribute */
35         {
36             if (ae->which == Z_AttributeValue_complex)
37             {
38                 Z_ComplexAttribute *ca = ae->value.complex;
39                 int i;
40                 for (i = 0; i < ca->num_list; i++)
41                 {
42                     Z_StringOrNumeric *son = ca->list[i];
43                     if (son->which == Z_StringOrNumeric_string)
44                         return son->u.string;
45                 }
46             }
47             server_choice = 0; /* not serverChoice because we have use attr */
48         }
49     }
50     if (server_choice)
51         return "cql.serverChoice";
52     return 0;
53 }
54
55 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
56 {
57     int j;
58     for (j = 0; j < attributes->num_attributes; j++)
59     {
60         Z_AttributeElement *ae = attributes->attributes[j];
61         if (*ae->attributeType == 2) /* relation attribute */
62         {
63             if (ae->which == Z_AttributeValue_numeric)
64             {
65                 /* Only support for numeric relation */
66                 Odr_int *relation = ae->value.numeric;
67                 /* map this numeric to representation in CQL */
68                 switch (*relation)
69                 {
70                     /* Unsure on whether this is the relation attribute constants? */
71                 case Z_ProximityOperator_Prox_lessThan: 
72                     return "<";
73                 case Z_ProximityOperator_Prox_lessThanOrEqual: 
74                     return "<="; 
75                 case Z_ProximityOperator_Prox_equal: 
76                     return "="; 
77                 case Z_ProximityOperator_Prox_greaterThanOrEqual: 
78                     return ">="; 
79                 case Z_ProximityOperator_Prox_greaterThan: 
80                     return ">"; 
81                 case Z_ProximityOperator_Prox_notEqual: 
82                     return "<>"; 
83                 case 100: 
84                     /* phonetic is not supported in CQL */
85                     return 0; 
86                 case 101: 
87                     /* stem is not supported in CQL */
88                     return 0; 
89                 case 102: 
90                     /* relevance is supported in CQL, but not implemented yet */
91                     return 0; 
92                 default:
93                     /* Invalid relation */
94                     return 0;
95                 }
96             }
97             else {
98                 /*  Can we have a complex relation value?
99                     Should we implement something?
100                 */
101             }
102         }
103     }
104     return "=";
105 }
106
107 static int rpn2cql_attr(cql_transform_t ct,
108                         Z_AttributeList *attributes, WRBUF w)
109 {
110     const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
111     const char *index = cql_lookup_reverse(ct, "index.", attributes);
112     const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
113
114     /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
115     if (!index)
116         index = lookup_index_from_string_attr(attributes);
117
118     /* Attempt to fix bug #2978: Look for a relation attribute */
119     if (!relation) 
120         relation = lookup_relation_index_from_attr(attributes);
121
122     if (!index)
123     {
124         cql_transform_set_error(ct,
125                                 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
126         return -1;
127     }
128     /* for serverChoice we omit index+relation+structure */
129     if (strcmp(index, "cql.serverChoice"))
130     {
131         wrbuf_puts(w, index);
132         if (relation)
133         {
134             if (!strcmp(relation, "exact"))
135                 relation = "==";
136             else if (!strcmp(relation, "eq"))
137                 relation = "=";
138             else if (!strcmp(relation, "le"))
139                 relation = "<=";
140             else if (!strcmp(relation, "ge"))
141                 relation = ">=";
142             /* Missing mapping of not equal, phonetic, stem and relevance */
143             wrbuf_puts(w, relation);
144         }
145         else
146             wrbuf_puts(w, "=");
147
148         if (structure)
149         {
150             if (strcmp(structure, "*"))
151             {
152                 wrbuf_puts(w, "/");
153                 wrbuf_puts(w, structure);
154                 wrbuf_puts(w, " ");
155             }
156         }
157     }
158     return 0;
159 }
160
161 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
162 static int checkForTruncation(int flag, Z_AttributeList *attributes)
163 {
164     int j;
165     for (j = 0; j < attributes->num_attributes; j++)
166     {
167         Z_AttributeElement *ae = attributes->attributes[j];
168         if (*ae->attributeType == 5) /* truncation attribute */
169         {
170             if (ae->which == Z_AttributeValue_numeric)
171             {
172                 Odr_int truncation = *(ae->value.numeric);
173                 /* This logic only works for Left, right and both. eg. 1,2,3 */
174                 if (truncation <= 3)
175                     return ((int) truncation & flag);
176             }
177             /* Complex: Shouldn't happen */
178         }
179     }
180     /* No truncation or unsupported */
181     return 0;
182 };
183
184 static int checkForLeftTruncation(Z_AttributeList *attributes) {
185         return checkForTruncation(2, attributes);
186 }
187
188 static int checkForRightTruncation(Z_AttributeList *attributes) {
189         return checkForTruncation(1, attributes);
190 };
191
192 static int rpn2cql_simple(cql_transform_t ct,
193                           void (*pr)(const char *buf, void *client_data),
194                           void *client_data,
195                           Z_Operand *q, WRBUF w)
196 {
197     int ret = 0;
198     if (q->which != Z_Operand_APT)
199     {
200         ret = -1;
201         cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
202     }
203     else
204     {
205         Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
206         Z_Term *term = apt->term;
207         const char *sterm = 0;
208         size_t lterm = 0;
209
210         wrbuf_rewind(w);
211         ret = rpn2cql_attr(ct, apt->attributes, w);
212
213         switch(term->which)
214         {
215         case Z_Term_general:
216             lterm = term->u.general->len;
217             sterm = (const char *) term->u.general->buf;
218             break;
219         case Z_Term_numeric:
220             wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
221             break;
222         case Z_Term_characterString:
223             sterm = term->u.characterString;
224             lterm = strlen(sterm);
225             break;
226         default:
227             ret = -1;
228             cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
229         }
230
231         if (term)
232         {
233             size_t i;
234             int must_quote = 0;
235             for (i = 0 ; i < lterm; i++)
236                 if (sterm[i] == ' ')
237                     must_quote = 1;
238             if (must_quote)
239                 wrbuf_puts(w, "\"");
240             /* Bug 2878: Check and add Truncation */
241                         if (checkForLeftTruncation(apt->attributes))
242                 wrbuf_puts(w, "*");
243             wrbuf_write(w, sterm, lterm);
244             /* Bug 2878: Check and add Truncation */
245                         if (checkForRightTruncation(apt->attributes))
246                 wrbuf_puts(w, "*");
247             if (must_quote)
248                 wrbuf_puts(w, "\"");
249         }
250         if (ret == 0)
251             pr(wrbuf_cstr(w), client_data);
252     }
253     return ret;
254 }
255
256
257 static int rpn2cql_structure(cql_transform_t ct,
258                              void (*pr)(const char *buf, void *client_data),
259                              void *client_data,
260                              Z_RPNStructure *q, int nested,
261                              WRBUF w)
262 {
263     if (q->which == Z_RPNStructure_simple)
264         return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
265     else
266     {
267         Z_Operator *op = q->u.complex->roperator;
268         int r;
269
270         if (nested)
271             pr("(", client_data);
272
273         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
274         if (r)
275             return r;
276         switch(op->which)
277         {
278         case  Z_Operator_and:
279             pr(" and ", client_data);
280             break;
281         case  Z_Operator_or:
282             pr(" or ", client_data);
283             break;
284         case  Z_Operator_and_not:
285             pr(" not ", client_data);
286             break;
287         case  Z_Operator_prox:
288             cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
289             return -1;
290         }
291         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
292         if (nested)
293             pr(")", client_data);
294         return r;
295     }
296 }
297
298 int cql_transform_rpn2cql_stream(cql_transform_t ct,
299                                  void (*pr)(const char *buf, void *client_data),
300                                  void *client_data,
301                                  Z_RPNQuery *q)
302 {
303     int r;
304     WRBUF w = wrbuf_alloc();
305     cql_transform_set_error(ct, 0, 0);
306     r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
307     wrbuf_destroy(w);
308     return r;
309 }
310
311
312 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
313                                 WRBUF w,
314                                 Z_RPNQuery *q)
315 {
316     return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
317 }
318
319 /*
320  * Local variables:
321  * c-basic-offset: 4
322  * c-file-style: "Stroustrup"
323  * indent-tabs-mode: nil
324  * End:
325  * vim: shiftwidth=4 tabstop=8 expandtab
326  */
327