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