479aebf624099855904ed62234c36fac9f0cd20b
[metaproxy-moved-to-github.git] / src / util.cpp
1 /* $Id: util.cpp,v 1.3 2005-10-30 18:51:21 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include <yaz/odr.h>
10 #include <yaz/pquery.h>
11 #include "util.hpp"
12
13 bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
14     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
15     
16     Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
17     if (!rpn)
18     {
19         yaz_pqf_destroy(pqf_parser);
20         return false;
21     }
22     yaz_pqf_destroy(pqf_parser);
23     Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
24     query->which = Z_Query_type_1;
25     query->u.type_1 = rpn;
26     
27     apdu->u.searchRequest->query = query;
28     return true;
29 }
30
31 yp2::odr::odr(int type)
32 {
33     m_odr = odr_createmem(type);
34 }
35
36 yp2::odr::odr()
37 {
38     m_odr = odr_createmem(ODR_ENCODE);
39 }
40
41 yp2::odr::~odr()
42 {
43     odr_destroy(m_odr);
44 }
45
46 yp2::odr::operator ODR() const
47 {
48     return m_odr;
49 }
50
51 Z_APDU *yp2::odr::create_close(int reason, const char *addinfo)
52 {
53     Z_APDU *apdu = zget_APDU(m_odr, Z_APDU_close);
54     
55     *apdu->u.close->closeReason = reason;
56     if (addinfo)
57         apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
58     return apdu;
59 }
60
61 Z_APDU *yp2::odr::create_initResponse(int error, const char *addinfo)
62 {
63     Z_APDU *apdu = zget_APDU(m_odr, Z_APDU_initResponse);
64     if (error)
65     {
66         apdu->u.initResponse->userInformationField =
67             zget_init_diagnostics(m_odr, error, addinfo);
68         *apdu->u.initResponse->result = 0;
69     }
70     return apdu;
71 }
72
73 /*
74  * Local variables:
75  * c-basic-offset: 4
76  * indent-tabs-mode: nil
77  * c-file-style: "stroustrup"
78  * End:
79  * vim: shiftwidth=4 tabstop=8 expandtab
80  */