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