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