Update for YAZ 3s new OID system
[yazpp-moved-to-github.git] / src / yaz-z-query.cpp
1 /*
2  * Copyright (c) 1998-2007, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-z-query.cpp,v 1.22 2007-04-12 15:00:33 adam Exp $
6  */
7
8 #include <yaz/querytowrbuf.h>
9 #include <yazpp/z-query.h>
10 #include <yaz/pquery.h>
11 #include <assert.h>
12
13 using namespace yazpp_1;
14
15 Yaz_Z_Query::Yaz_Z_Query()
16 {
17     odr_encode = odr_createmem(ODR_ENCODE);
18     odr_decode = odr_createmem(ODR_DECODE);
19     odr_print = odr_createmem(ODR_PRINT);
20 }
21
22
23 Yaz_Z_Query::Yaz_Z_Query(const Yaz_Z_Query &q)
24 {
25     odr_encode = odr_createmem(ODR_ENCODE);
26     odr_decode = odr_createmem(ODR_DECODE);
27     odr_print = odr_createmem(ODR_PRINT);
28
29     m_len = q.m_len;
30     m_buf = (char*) odr_malloc(odr_encode, m_len);
31     memcpy(m_buf, q.m_buf, m_len);
32 }
33
34 Yaz_Z_Query& Yaz_Z_Query::operator=(const Yaz_Z_Query &q)
35 {
36     if (this != &q)
37     {
38         odr_reset(odr_encode);
39         if (!q.m_buf)
40         {
41             m_buf = 0;
42             m_len = 0;
43         }
44         else
45         {
46             m_len = q.m_len;
47             m_buf = (char*) odr_malloc(odr_encode, m_len);
48             memcpy(m_buf, q.m_buf, m_len);
49         }
50     }
51     return *this;
52 }
53
54 Yaz_Z_Query& Yaz_Z_Query::operator=(const char *rpn)
55 {
56     set_rpn(rpn);
57     return *this;
58 }
59
60 int Yaz_Z_Query::set_rpn(const char *rpn)
61 {
62     m_buf = 0;
63     odr_reset(odr_encode);
64     Z_Query *query = (Z_Query*) odr_malloc(odr_encode, sizeof(*query));
65     query->which = Z_Query_type_1;
66     query->u.type_1 = p_query_rpn(odr_encode, rpn);
67     if (!query->u.type_1)
68         return -1;
69     if (!z_Query(odr_encode, &query, 0, 0))
70         return -1;
71     // z_Query(odr_print, &query, 0, 0);
72     m_buf = odr_getbuf(odr_encode, &m_len, 0);
73     return m_len;
74 }
75
76 void Yaz_Z_Query::set_Z_Query(Z_Query *z_query)
77 {
78     m_buf = 0;
79     odr_reset(odr_encode);
80     if (!z_Query(odr_encode, &z_query, 0, 0))
81         return;
82     m_buf = odr_getbuf(odr_encode, &m_len, 0);
83 }
84
85 Yaz_Z_Query::~Yaz_Z_Query()
86 {
87     odr_destroy(odr_encode);
88     odr_destroy(odr_decode);
89     odr_destroy(odr_print);
90 }
91
92 Z_Query *Yaz_Z_Query::get_Z_Query()
93 {
94     Z_Query *query;
95     if (!m_buf)
96         return 0;
97     odr_reset(odr_decode);
98     odr_setbuf(odr_decode, m_buf, m_len, 0);
99     if (!z_Query(odr_decode, &query, 0, 0))
100         return 0;
101     return query;
102 }
103
104 void Yaz_Z_Query::print(char *str, size_t len)
105 {
106     Z_Query *query;
107     *str = 0;
108     if (!m_buf)
109         return;
110     odr_setbuf(odr_decode, m_buf, m_len, 0);
111     if (!z_Query(odr_decode, &query, 0, 0))
112         return;
113     WRBUF wbuf = wrbuf_alloc();
114     yaz_query_to_wrbuf(wbuf, query);
115     if (wrbuf_len(wbuf) > len-1)
116     {
117         memcpy(str, wrbuf_buf(wbuf), len-1);
118         str[len-1] = '\0';
119     }
120     else
121         strcpy(str, wrbuf_cstr(wbuf));
122     wrbuf_destroy(wbuf);
123     odr_reset(odr_decode);
124 }
125
126 int Yaz_Z_Query::match(const Yaz_Z_Query *other)
127 {
128     if (m_len != other->m_len)
129         return 0;
130     if (!m_buf || !other->m_buf)
131         return 0;
132     if (memcmp(m_buf, other->m_buf, m_len))
133         return 0;
134     return 1;
135 }
136
137 /*
138  * Local variables:
139  * c-basic-offset: 4
140  * indent-tabs-mode: nil
141  * End:
142  * vim: shiftwidth=4 tabstop=8 expandtab
143  */
144