1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements RPN to CQL conversion
10 * Evaluation order of rules:
24 #include <yaz/rpn2cql.h>
25 #include <yaz/xmalloc.h>
26 #include <yaz/diagbib1.h>
27 #include <yaz/z-core.h>
28 #include <yaz/wrbuf.h>
30 static int rpn2cql_attr(cql_transform_t ct,
31 void (*pr)(const char *buf, void *client_data),
33 Z_AttributeList *attributes, WRBUF w)
36 for (i = 0; i < attributes->num_attributes; i++)
38 Z_AttributeElement *elem = attributes->attributes[i];
43 static int rpn2cql_simple(cql_transform_t ct,
44 void (*pr)(const char *buf, void *client_data),
46 Z_Operand *q, WRBUF w)
49 if (q->which != Z_Operand_APT)
52 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
56 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
57 Z_Term *term = apt->term;
60 ret = rpn2cql_attr(ct, pr, client_data, apt->attributes, w);
65 wrbuf_write(w, (const char *) term->u.general->buf, term->u.general->len);
68 wrbuf_printf(w, "%d", *term->u.numeric);
70 case Z_Term_characterString:
71 wrbuf_puts(w, term->u.characterString);
75 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
78 pr(wrbuf_cstr(w), client_data);
83 static int rpn2cql_structure(cql_transform_t ct,
84 void (*pr)(const char *buf, void *client_data),
86 Z_RPNStructure *q, int nested,
89 if (q->which == Z_RPNStructure_simple)
90 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
93 Z_Operator *op = q->u.complex->roperator;
99 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
105 pr(" and ", client_data);
108 pr(" or ", client_data);
110 case Z_Operator_and_not:
111 pr(" not ", client_data);
113 case Z_Operator_prox:
114 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
117 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
119 pr(")", client_data);
124 int cql_transform_rpn2cql_stream(cql_transform_t ct,
125 void (*pr)(const char *buf, void *client_data),
130 WRBUF w = wrbuf_alloc();
131 cql_transform_set_error(ct, 0, 0);
132 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
138 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
142 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
148 * indent-tabs-mode: nil
150 * vim: shiftwidth=4 tabstop=8 expandtab