Bump copyright year
[yazpp-moved-to-github.git] / src / yaz-cql2rpn.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) 1998-2010 Index Data and Mike Taylor
3  * See the file LICENSE for details.
4  */
5
6 #include <yaz/log.h>
7 #include <yaz/pquery.h>
8 #include <yazpp/cql2rpn.h>
9
10 using namespace yazpp_1;
11
12 Yaz_cql2rpn::Yaz_cql2rpn()
13 {
14     m_transform = 0;
15 }
16
17 Yaz_cql2rpn::~Yaz_cql2rpn()
18 {
19     if (m_transform)
20         cql_transform_close(m_transform);
21 }
22
23 void Yaz_cql2rpn::set_pqf_file(const char *fname)
24 {
25     if (!m_transform)
26         m_transform = cql_transform_open_fname(fname);
27 }
28
29
30 bool Yaz_cql2rpn::parse_spec_file(const char *fname, int *error)
31 {
32     *error = 0;
33     cql_transform_close(m_transform);
34     m_transform = cql_transform_open_fname(fname);
35     return m_transform ? true : false;
36 }
37
38 int Yaz_cql2rpn::query_transform(const char *cql_query, 
39                                  Z_RPNQuery **rpnquery, ODR o,
40                                  char **addinfop)
41 {
42     const char *addinfo = 0;
43     if (!m_transform)
44         return -3;
45     CQL_parser cp = cql_parser_create();
46
47     int r = cql_parser_string(cp, cql_query);
48     if (r)
49     {
50         r = 10;
51     }
52     else
53     {
54         char rpn_buf[10240];
55         r = cql_transform_buf(m_transform, cql_parser_result(cp), 
56                               rpn_buf, sizeof(rpn_buf)-1);
57         if (!r)
58         {
59             YAZ_PQF_Parser pp = yaz_pqf_create();
60
61             *rpnquery = yaz_pqf_parse(pp, o, rpn_buf);
62             if (!*rpnquery)
63             {
64                 size_t off;
65                 const char *pqf_msg;
66                 yaz_pqf_error(pp, &pqf_msg, &off);
67                 r = -1;
68             }
69             yaz_pqf_destroy(pp);
70         }
71         else
72         {
73             r = cql_transform_error(m_transform, &addinfo);
74         }
75     }   
76     cql_parser_destroy(cp);
77     if (addinfo)
78         *addinfop = odr_strdup(o, addinfo);
79     else
80         *addinfop = 0;
81     return r;
82 }
83 /*
84  * Local variables:
85  * c-basic-offset: 4
86  * c-file-style: "Stroustrup"
87  * indent-tabs-mode: nil
88  * End:
89  * vim: shiftwidth=4 tabstop=8 expandtab
90  */
91