Year 2007.
[metaproxy-moved-to-github.git] / src / util.hpp
1 /* $Id: util.hpp,v 1.24 2007-01-25 14:05:54 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #ifndef YP2_UTIL_HPP
8 #define YP2_UTIL_HPP
9
10 #include "package.hpp"
11
12 #include <yaz/z-core.h>
13
14 #include <boost/utility.hpp>
15 #include <boost/scoped_ptr.hpp>
16
17 #include <list>
18 #include <vector>
19 #include <sstream>
20 #include <string>
21
22
23 namespace metaproxy_1 {
24     namespace util  {
25
26         
27         template<typename T> 
28         std::string to_string(const T& t)
29         {
30             std::ostringstream o;
31             if(o << t)
32                 return o.str();    
33             return std::string();
34         };
35
36         const char * 
37         record_composition_to_esn(Z_RecordComposition *comp);
38
39         std::string http_header_value(const Z_HTTP_Header* header, 
40                                                const std::string name);
41
42         std::string http_headers_debug(const Z_HTTP_Request &http_req);        
43
44         void http_response(metaproxy_1::Package &package, 
45                            const std::string &content, 
46                            int http_code = 200);
47
48
49         int memcmp2(const void *buf1, int len1, const void *buf2, int len2);
50
51         std::string database_name_normalize(const std::string &s);
52
53         bool pqf(ODR odr, Z_APDU *apdu, const std::string &q);
54
55         std::string zQueryToString(Z_Query *query);
56
57         Z_ReferenceId **get_referenceId(const Z_APDU *apdu);
58
59         void transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst);
60
61         Z_APDU *create_APDU(ODR odr, int type, const Z_APDU *in_apdu);
62
63         bool set_databases_from_zurl(ODR odr, std::string zurl,
64                                      int *db_num, char ***db_strings);
65
66         void split_zurl(std::string zurl, std::string &host,
67                         std::list<std::string> &db);
68         
69         void get_vhost_otherinfo(Z_OtherInformation *otherInformation,
70                                  std::list<std::string> &vhosts);
71         
72         int remove_vhost_otherinfo(Z_OtherInformation **otherInformation,
73                                    std::list<std::string> &vhosts);
74
75         void set_vhost_otherinfo(Z_OtherInformation **otherInformation, 
76                                  ODR odr,
77                                  const std::string vhost, 
78                                  const int cat = 1 );
79
80         void set_vhost_otherinfo(Z_OtherInformation **otherInformation,
81                                  ODR odr,
82                                  const std::list<std::string> &vhosts);
83
84         int get_or_remove_vhost_otherinfo(
85             Z_OtherInformation **otherInformation,
86             bool remove_flag,
87             std::list<std::string> &vhosts);
88
89         void get_init_diagnostics(Z_InitResponse *res,
90                                   int &error_code, std::string &addinfo);
91
92         void get_default_diag(Z_DefaultDiagFormat *r,
93                               int &error_code, std::string &addinfo);
94
95         void piggyback(int smallSetUpperBound,
96                        int largeSetLowerBound,
97                        int mediumSetPresentNumber,
98                        int result_set_size,
99                        int &number_to_present);
100     };
101
102     class odr : public boost::noncopyable {
103     public:
104         odr(int type);
105         odr();
106         ~odr();
107         operator ODR() const;
108         Z_APDU *create_close(const Z_APDU *in_apdu, 
109                              int reason, const char *addinfo);
110         Z_APDU *create_initResponse(const Z_APDU *in_apdu, 
111                                     int error, const char *addinfo);
112         Z_APDU *create_searchResponse(const Z_APDU *in_apdu,
113                                       int error, const char *addinfo);
114         Z_APDU *create_presentResponse(const Z_APDU *in_apdu,
115                                        int error, const char *addinfo);
116         Z_APDU *create_scanResponse(const Z_APDU *in_apdu,
117                                     int error, const char *addinfo);
118         Z_APDU *create_APDU(int type, const Z_APDU *in_apdu);
119
120         Z_GDU *create_HTTP_Response(metaproxy_1::Session &session,
121                                     Z_HTTP_Request *req, int code);
122     private:
123         ODR m_odr;
124     };
125
126     class PlainFile {
127         class Rep;
128         boost::scoped_ptr<Rep> m_p;
129     public:
130         PlainFile();
131         ~PlainFile();
132         bool open(const std::string &fname);
133         bool getline(std::vector<std::string> &args);
134     };
135 }
136 #endif
137 /*
138  * Local variables:
139  * c-basic-offset: 4
140  * indent-tabs-mode: nil
141  * c-file-style: "stroustrup"
142  * End:
143  * vim: shiftwidth=4 tabstop=8 expandtab
144  */