2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-query.cpp,v 1.17 2005-06-25 15:53:19 adam Exp $
8 #include <yaz++/z-query.h>
9 #include <yaz/pquery.h>
11 using namespace yazpp_1;
13 Yaz_Z_Query::Yaz_Z_Query()
15 odr_encode = odr_createmem (ODR_ENCODE);
16 odr_decode = odr_createmem (ODR_DECODE);
17 odr_print = odr_createmem (ODR_PRINT);
20 int Yaz_Z_Query::set_rpn (const char *rpn)
23 odr_reset (odr_encode);
24 Z_Query *query = (Z_Query*) odr_malloc (odr_encode, sizeof(*query));
25 query->which = Z_Query_type_1;
26 query->u.type_1 = p_query_rpn (odr_encode, PROTO_Z3950, rpn);
29 if (!z_Query (odr_encode, &query, 0, 0))
31 // z_Query(odr_print, &query, 0, 0);
32 m_buf = odr_getbuf (odr_encode, &m_len, 0);
36 void Yaz_Z_Query::set_Z_Query(Z_Query *z_query)
39 odr_reset (odr_encode);
40 if (!z_Query (odr_encode, &z_query, 0, 0))
42 m_buf = odr_getbuf (odr_encode, &m_len, 0);
45 Yaz_Z_Query::~Yaz_Z_Query()
47 odr_destroy (odr_encode);
48 odr_destroy (odr_decode);
49 odr_destroy (odr_print);
52 Z_Query *Yaz_Z_Query::get_Z_Query ()
57 odr_reset(odr_decode);
58 odr_setbuf(odr_decode, m_buf, m_len, 0);
59 if (!z_Query(odr_decode, &query, 0, 0))
64 void Yaz_Z_Query::print(char *str, int len)
70 odr_setbuf (odr_decode, m_buf, m_len, 0);
71 if (!z_Query(odr_decode, &query, 0, 0))
73 WRBUF wbuf = zquery2pquery(query);
76 if (wrbuf_len(wbuf) > len-1)
78 memcpy(str, wrbuf_buf(wbuf), len-1);
82 strcpy(str, wrbuf_buf(wbuf));
85 odr_reset(odr_decode);
88 int Yaz_Z_Query::match(Yaz_Z_Query *other)
90 if (m_len != other->m_len)
92 if (!m_buf || !other->m_buf)
94 if (memcmp(m_buf, other->m_buf, m_len))
99 void Yaz_Z_Query::oid2str(Odr_oid *o, WRBUF buf)
101 for (; *o >= 0; o++) {
103 sprintf(ibuf, "%d", *o);
104 wrbuf_puts(buf, ibuf);
106 wrbuf_putc(buf, '.');
110 void Yaz_Z_Query::pr_term(WRBUF wbuf, char *buf, int len)
113 wrbuf_putc(wbuf, '"');
114 for (i = 0; i<len; i++)
118 wrbuf_putc(wbuf, '\\');
119 wrbuf_putc(wbuf, ch);
121 wrbuf_puts(wbuf, "\" ");
124 int Yaz_Z_Query::rpn2pquery(Z_RPNStructure *s, WRBUF buf)
126 if (s->which == Z_RPNStructure_simple)
128 Z_Operand *o = s->u.simple;
130 if (o->which == Z_Operand_APT)
132 Z_AttributesPlusTerm *at = s->u.simple->u.attributesPlusTerm;
133 if (at->attributes) {
135 for (i = 0; i < at->attributes->num_attributes; i++) {
136 wrbuf_puts(buf, "@attr ");
137 if (at->attributes->attributes[i]->attributeSet) {
138 oid2str(at->attributes->attributes[i]->attributeSet, buf);
139 wrbuf_putc(buf, ' ');
141 wrbuf_printf(buf, "%d=", *at->attributes->attributes[i]->attributeType);
142 wrbuf_printf(buf, "%d ", *at->attributes->attributes[i]->value.numeric);
145 if (at->term->which == Z_Term_general)
147 pr_term(buf, (char*) at->term->u.general->buf,
148 at->term->u.general->len);
150 else if (at->term->which == Z_Term_characterString)
152 wrbuf_puts(buf, "@term string ");
153 pr_term(buf, at->term->u.characterString,
154 strlen(at->term->u.characterString));
158 else if (o->which == Z_Operand_resultSetId)
160 wrbuf_printf(buf, "@set %s ", o->u.resultSetId);
163 else if (s->which == Z_RPNStructure_complex)
165 Z_Complex *c = s->u.complex;
167 switch (c->roperator->which) {
168 case Z_Operator_and: wrbuf_puts(buf, "@and "); break;
169 case Z_Operator_or: wrbuf_puts(buf, "@or "); break;
170 case Z_Operator_and_not: wrbuf_puts(buf, "@not "); break;
171 case Z_Operator_prox: wrbuf_puts(buf, "@prox "); break;
172 default: wrbuf_puts(buf, "@unknown ");
174 if (!rpn2pquery(c->s1, buf))
176 if (!rpn2pquery(c->s2, buf))
182 WRBUF Yaz_Z_Query::zquery2pquery(Z_Query *q)
184 if (q->which != Z_Query_type_1 && q->which != Z_Query_type_101)
186 WRBUF buf = wrbuf_alloc();
187 if (q->u.type_1->attributeSetId) {
188 /* Output attribute set ID */
189 wrbuf_puts(buf, "@attrset ");
190 oid2str(q->u.type_1->attributeSetId, buf);
191 wrbuf_putc(buf, ' ');
193 return rpn2pquery(q->u.type_1->RPNStructure, buf) ? buf : 0;
200 * indent-tabs-mode: nil
202 * vim: shiftwidth=4 tabstop=8 expandtab