1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements RPN to CQL conversion
15 #include <yaz/rpn2cql.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/diagbib1.h>
18 #include <yaz/z-core.h>
19 #include <yaz/wrbuf.h>
21 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
24 int server_choice = 1;
25 for (j = 0; j < attributes->num_attributes; j++)
27 Z_AttributeElement *ae = attributes->attributes[j];
28 if (*ae->attributeType == 1) /* use attribute */
30 if (ae->which == Z_AttributeValue_complex)
32 Z_ComplexAttribute *ca = ae->value.complex;
34 for (i = 0; i < ca->num_list; i++)
36 Z_StringOrNumeric *son = ca->list[i];
37 if (son->which == Z_StringOrNumeric_string)
41 server_choice = 0; /* not serverChoice because we have use attr */
45 return "cql.serverChoice";
49 static int rpn2cql_attr(cql_transform_t ct,
50 Z_AttributeList *attributes, WRBUF w)
52 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
53 const char *index = cql_lookup_reverse(ct, "index.", attributes);
54 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
56 /* if transform (properties) do not match, we'll just use a USE
57 string attribute (bug #2978) */
59 index = lookup_index_from_string_attr(attributes);
63 cql_transform_set_error(ct,
64 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
67 /* for serverChoice we omit index+relation+structure */
68 if (strcmp(index, "cql.serverChoice"))
73 if (!strcmp(relation, "exact"))
75 else if (!strcmp(relation, "eq"))
77 else if (!strcmp(relation, "le"))
79 else if (!strcmp(relation, "ge"))
81 wrbuf_puts(w, relation);
88 if (strcmp(structure, "*"))
91 wrbuf_puts(w, structure);
99 static int rpn2cql_simple(cql_transform_t ct,
100 void (*pr)(const char *buf, void *client_data),
102 Z_Operand *q, WRBUF w)
105 if (q->which != Z_Operand_APT)
108 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
112 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
113 Z_Term *term = apt->term;
114 const char *sterm = 0;
118 ret = rpn2cql_attr(ct, apt->attributes, w);
123 lterm = term->u.general->len;
124 sterm = (const char *) term->u.general->buf;
127 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
129 case Z_Term_characterString:
130 sterm = term->u.characterString;
131 lterm = strlen(sterm);
135 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
142 for (i = 0 ; i < lterm; i++)
147 wrbuf_write(w, sterm, lterm);
152 pr(wrbuf_cstr(w), client_data);
157 static int rpn2cql_structure(cql_transform_t ct,
158 void (*pr)(const char *buf, void *client_data),
160 Z_RPNStructure *q, int nested,
163 if (q->which == Z_RPNStructure_simple)
164 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
167 Z_Operator *op = q->u.complex->roperator;
171 pr("(", client_data);
173 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
179 pr(" and ", client_data);
182 pr(" or ", client_data);
184 case Z_Operator_and_not:
185 pr(" not ", client_data);
187 case Z_Operator_prox:
188 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
191 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
193 pr(")", client_data);
198 int cql_transform_rpn2cql_stream(cql_transform_t ct,
199 void (*pr)(const char *buf, void *client_data),
204 WRBUF w = wrbuf_alloc();
205 cql_transform_set_error(ct, 0, 0);
206 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
212 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
216 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
222 * c-file-style: "Stroustrup"
223 * indent-tabs-mode: nil
225 * vim: shiftwidth=4 tabstop=8 expandtab