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 for (j = 0; j < attributes->num_attributes; j++)
26 Z_AttributeElement *ae = attributes->attributes[j];
27 if (*ae->attributeType == 1) /* use attribute */
29 if (ae->which == Z_AttributeValue_complex)
31 Z_ComplexAttribute *ca = ae->value.complex;
33 for (i = 0; i < ca->num_list; i++)
35 Z_StringOrNumeric *son = ca->list[i];
36 if (son->which == Z_StringOrNumeric_string)
45 static int rpn2cql_attr(cql_transform_t ct,
46 Z_AttributeList *attributes, WRBUF w)
48 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
49 const char *index = cql_lookup_reverse(ct, "index.", attributes);
50 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
52 /* if transform (properties) do not match, we'll just use a USE
53 string attribute (bug #2978) */
55 index = lookup_index_from_string_attr(attributes);
59 cql_transform_set_error(ct,
60 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
63 /* for serverChoice we omit index+relation+structure */
64 if (strcmp(index, "cql.serverChoice"))
69 if (!strcmp(relation, "exact"))
71 else if (!strcmp(relation, "eq"))
73 else if (!strcmp(relation, "le"))
75 else if (!strcmp(relation, "ge"))
77 wrbuf_puts(w, relation);
84 if (strcmp(structure, "*"))
87 wrbuf_puts(w, structure);
95 static int rpn2cql_simple(cql_transform_t ct,
96 void (*pr)(const char *buf, void *client_data),
98 Z_Operand *q, WRBUF w)
101 if (q->which != Z_Operand_APT)
104 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
108 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
109 Z_Term *term = apt->term;
110 const char *sterm = 0;
114 ret = rpn2cql_attr(ct, apt->attributes, w);
119 lterm = term->u.general->len;
120 sterm = (const char *) term->u.general->buf;
123 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
125 case Z_Term_characterString:
126 sterm = term->u.characterString;
127 lterm = strlen(sterm);
131 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
138 for (i = 0 ; i < lterm; i++)
143 wrbuf_write(w, sterm, lterm);
148 pr(wrbuf_cstr(w), client_data);
153 static int rpn2cql_structure(cql_transform_t ct,
154 void (*pr)(const char *buf, void *client_data),
156 Z_RPNStructure *q, int nested,
159 if (q->which == Z_RPNStructure_simple)
160 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
163 Z_Operator *op = q->u.complex->roperator;
167 pr("(", client_data);
169 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
175 pr(" and ", client_data);
178 pr(" or ", client_data);
180 case Z_Operator_and_not:
181 pr(" not ", client_data);
183 case Z_Operator_prox:
184 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
187 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
189 pr(")", client_data);
194 int cql_transform_rpn2cql_stream(cql_transform_t ct,
195 void (*pr)(const char *buf, void *client_data),
200 WRBUF w = wrbuf_alloc();
201 cql_transform_set_error(ct, 0, 0);
202 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
208 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
212 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
218 * c-file-style: "Stroustrup"
219 * indent-tabs-mode: nil
221 * vim: shiftwidth=4 tabstop=8 expandtab