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 int rpn2cql_attr(cql_transform_t ct,
22 Z_AttributeList *attributes, WRBUF w)
24 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
25 const char *index = cql_lookup_reverse(ct, "index.", attributes);
26 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
27 if (index && strcmp(index, "index.cql.serverChoice"))
29 wrbuf_puts(w, index+6);
34 if (!strcmp(relation, "exact"))
36 else if (!strcmp(relation, "eq"))
38 else if (!strcmp(relation, "le"))
40 else if (!strcmp(relation, "ge"))
42 wrbuf_puts(w, relation);
50 if (strcmp(structure, "*"))
53 wrbuf_puts(w, structure);
61 static int rpn2cql_simple(cql_transform_t ct,
62 void (*pr)(const char *buf, void *client_data),
64 Z_Operand *q, WRBUF w)
67 if (q->which != Z_Operand_APT)
70 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
74 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
75 Z_Term *term = apt->term;
76 const char *sterm = 0;
80 ret = rpn2cql_attr(ct, apt->attributes, w);
85 lterm = term->u.general->len;
86 sterm = (const char *) term->u.general->buf;
89 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
91 case Z_Term_characterString:
92 sterm = term->u.characterString;
93 lterm = strlen(sterm);
97 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
104 for (i = 0 ; i < lterm; i++)
109 wrbuf_write(w, sterm, lterm);
114 pr(wrbuf_cstr(w), client_data);
119 static int rpn2cql_structure(cql_transform_t ct,
120 void (*pr)(const char *buf, void *client_data),
122 Z_RPNStructure *q, int nested,
125 if (q->which == Z_RPNStructure_simple)
126 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
129 Z_Operator *op = q->u.complex->roperator;
133 pr("(", client_data);
135 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
141 pr(" and ", client_data);
144 pr(" or ", client_data);
146 case Z_Operator_and_not:
147 pr(" not ", client_data);
149 case Z_Operator_prox:
150 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
153 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
155 pr(")", client_data);
160 int cql_transform_rpn2cql_stream(cql_transform_t ct,
161 void (*pr)(const char *buf, void *client_data),
166 WRBUF w = wrbuf_alloc();
167 cql_transform_set_error(ct, 0, 0);
168 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
174 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
178 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
184 * c-file-style: "Stroustrup"
185 * indent-tabs-mode: nil
187 * vim: shiftwidth=4 tabstop=8 expandtab