GPL v2.
[metaproxy-moved-to-github.git] / src / util.hpp
1 /* $Id: util.hpp,v 1.25 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #ifndef YP2_UTIL_HPP
23 #define YP2_UTIL_HPP
24
25 #include "package.hpp"
26
27 #include <yaz/z-core.h>
28
29 #include <boost/utility.hpp>
30 #include <boost/scoped_ptr.hpp>
31
32 #include <list>
33 #include <vector>
34 #include <sstream>
35 #include <string>
36
37
38 namespace metaproxy_1 {
39     namespace util  {
40
41         
42         template<typename T> 
43         std::string to_string(const T& t)
44         {
45             std::ostringstream o;
46             if(o << t)
47                 return o.str();    
48             return std::string();
49         };
50
51         const char * 
52         record_composition_to_esn(Z_RecordComposition *comp);
53
54         std::string http_header_value(const Z_HTTP_Header* header, 
55                                                const std::string name);
56
57         std::string http_headers_debug(const Z_HTTP_Request &http_req);        
58
59         void http_response(metaproxy_1::Package &package, 
60                            const std::string &content, 
61                            int http_code = 200);
62
63
64         int memcmp2(const void *buf1, int len1, const void *buf2, int len2);
65
66         std::string database_name_normalize(const std::string &s);
67
68         bool pqf(ODR odr, Z_APDU *apdu, const std::string &q);
69
70         std::string zQueryToString(Z_Query *query);
71
72         Z_ReferenceId **get_referenceId(const Z_APDU *apdu);
73
74         void transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst);
75
76         Z_APDU *create_APDU(ODR odr, int type, const Z_APDU *in_apdu);
77
78         bool set_databases_from_zurl(ODR odr, std::string zurl,
79                                      int *db_num, char ***db_strings);
80
81         void split_zurl(std::string zurl, std::string &host,
82                         std::list<std::string> &db);
83         
84         void get_vhost_otherinfo(Z_OtherInformation *otherInformation,
85                                  std::list<std::string> &vhosts);
86         
87         int remove_vhost_otherinfo(Z_OtherInformation **otherInformation,
88                                    std::list<std::string> &vhosts);
89
90         void set_vhost_otherinfo(Z_OtherInformation **otherInformation, 
91                                  ODR odr,
92                                  const std::string vhost, 
93                                  const int cat = 1 );
94
95         void set_vhost_otherinfo(Z_OtherInformation **otherInformation,
96                                  ODR odr,
97                                  const std::list<std::string> &vhosts);
98
99         int get_or_remove_vhost_otherinfo(
100             Z_OtherInformation **otherInformation,
101             bool remove_flag,
102             std::list<std::string> &vhosts);
103
104         void get_init_diagnostics(Z_InitResponse *res,
105                                   int &error_code, std::string &addinfo);
106
107         void get_default_diag(Z_DefaultDiagFormat *r,
108                               int &error_code, std::string &addinfo);
109
110         void piggyback(int smallSetUpperBound,
111                        int largeSetLowerBound,
112                        int mediumSetPresentNumber,
113                        int result_set_size,
114                        int &number_to_present);
115     };
116
117     class odr : public boost::noncopyable {
118     public:
119         odr(int type);
120         odr();
121         ~odr();
122         operator ODR() const;
123         Z_APDU *create_close(const Z_APDU *in_apdu, 
124                              int reason, const char *addinfo);
125         Z_APDU *create_initResponse(const Z_APDU *in_apdu, 
126                                     int error, const char *addinfo);
127         Z_APDU *create_searchResponse(const Z_APDU *in_apdu,
128                                       int error, const char *addinfo);
129         Z_APDU *create_presentResponse(const Z_APDU *in_apdu,
130                                        int error, const char *addinfo);
131         Z_APDU *create_scanResponse(const Z_APDU *in_apdu,
132                                     int error, const char *addinfo);
133         Z_APDU *create_APDU(int type, const Z_APDU *in_apdu);
134
135         Z_GDU *create_HTTP_Response(metaproxy_1::Session &session,
136                                     Z_HTTP_Request *req, int code);
137     private:
138         ODR m_odr;
139     };
140
141     class PlainFile {
142         class Rep;
143         boost::scoped_ptr<Rep> m_p;
144     public:
145         PlainFile();
146         ~PlainFile();
147         bool open(const std::string &fname);
148         bool getline(std::vector<std::string> &args);
149     };
150 }
151 #endif
152 /*
153  * Local variables:
154  * c-basic-offset: 4
155  * indent-tabs-mode: nil
156  * c-file-style: "stroustrup"
157  * End:
158  * vim: shiftwidth=4 tabstop=8 expandtab
159  */