computing backend record syntax and elementset name/schema
[metaproxy-moved-to-github.git] / src / util.hpp
1 /* $Id: util.hpp,v 1.22 2006-10-04 14:04:00 marc Exp $
2    Copyright (c) 2005-2006, 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::list<std::string> &vhosts);
78
79         int get_or_remove_vhost_otherinfo(
80             Z_OtherInformation **otherInformation,
81             bool remove_flag,
82             std::list<std::string> &vhosts);
83
84         void get_init_diagnostics(Z_InitResponse *res,
85                                   int &error_code, std::string &addinfo);
86
87         void get_default_diag(Z_DefaultDiagFormat *r,
88                               int &error_code, std::string &addinfo);
89
90         void piggyback(int smallSetUpperBound,
91                        int largeSetLowerBound,
92                        int mediumSetPresentNumber,
93                        int result_set_size,
94                        int &number_to_present);
95     };
96
97     class odr : public boost::noncopyable {
98     public:
99         odr(int type);
100         odr();
101         ~odr();
102         operator ODR() const;
103         Z_APDU *create_close(const Z_APDU *in_apdu, 
104                              int reason, const char *addinfo);
105         Z_APDU *create_initResponse(const Z_APDU *in_apdu, 
106                                     int error, const char *addinfo);
107         Z_APDU *create_searchResponse(const Z_APDU *in_apdu,
108                                       int error, const char *addinfo);
109         Z_APDU *create_presentResponse(const Z_APDU *in_apdu,
110                                        int error, const char *addinfo);
111         Z_APDU *create_scanResponse(const Z_APDU *in_apdu,
112                                     int error, const char *addinfo);
113         Z_APDU *create_APDU(int type, const Z_APDU *in_apdu);
114
115         Z_GDU *create_HTTP_Response(metaproxy_1::Session &session,
116                                     Z_HTTP_Request *req, int code);
117     private:
118         ODR m_odr;
119     };
120
121     class PlainFile {
122         class Rep;
123         boost::scoped_ptr<Rep> m_p;
124     public:
125         PlainFile();
126         ~PlainFile();
127         bool open(const std::string &fname);
128         bool getline(std::vector<std::string> &args);
129     };
130 }
131 #endif
132 /*
133  * Local variables:
134  * c-basic-offset: 4
135  * indent-tabs-mode: nil
136  * c-file-style: "stroustrup"
137  * End:
138  * vim: shiftwidth=4 tabstop=8 expandtab
139  */