NCBI C++ ToolKit
cgi_serial.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CGI___CGI_SERIAL__HPP
2 #define CGI___CGI_SERIAL__HPP
3 
4 /* $Id: cgi_serial.hpp 77769 2017-05-05 14:01:02Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Maxim Didenko
30  *
31  */
32 
33 /// @file cont_serial.hpp
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbistre.hpp>
37 #include <corelib/ncbistr.hpp>
38 #include <cgi/ncbicgi.hpp>
39 
40 #include <vector>
41 
43 
44 ///////////////////////////////////////////////////////
45 ///
46 /// template<typename TElem> CContElemConverter
47 ///
48 /// The helper class for storing/retrieving a call to/from a string
49 ///
50 template<typename TElem>
52 {
53 public:
54  static TElem FromString(const string& str) { return TElem(str); }
55  static string ToString (const TElem& elem) { return string(elem); }
56 };
57 
58 
59 ///////////////////////////////////////////////////////
60 ///
61 /// template<> CContElemConverter<string>
62 ///
63 /// The specialization of CContElemConverter for the string class.
64 ///
65 template<>
67 {
68 public:
69  static const string& FromString(const string& str) { return str; }
70  static const string& ToString (const string& str) { return str; }
71 };
72 
73 ///////////////////////////////////////////////////////
74 ///
75 /// template<> CContElemConverter<CCgiEntry>
76 ///
77 /// The specialization of CContElemConverter for the CCgiEntry class.
78 ///
79 template<>
81 {
82 public:
83  static CCgiEntry FromString(const string& str);
84  static string ToString (const CCgiEntry& elem);
85 };
86 
87 
88 ///////////////////////////////////////////////////////
89 ///
90 /// COStreamHelper
91 ///
92 /// The helper class for storing a data into to a stream
93 /// in the form
94 /// data_size data
95 ///
97 {
98 public:
100  ~COStreamHelper() { try { flush(); } catch (...) {} }
101 
102  operator CNcbiOstream&() { return x_GetStrm(); }
103 
104  template<typename T>
106  {
107  x_GetStrm() << t;
108  return *this;
109  }
110 
111  void flush(bool write_empty_data = false)
112  {
113  if (m_str.get() != NULL) {
114  unique_ptr<CNcbiOstrstream> strm(m_str.release());
115  string s = CNcbiOstrstreamToString(*strm);
116  // Historically counted, but did not output, a final \0.
117  m_Ostream << (s.size() + 1) << ' ' << s;
118  } else if (write_empty_data)
119  m_Ostream << 1 << ' ';
120  }
121 
122 private:
124  if (m_str.get() == NULL) {
125  m_str.reset(new CNcbiOstrstream);
126  }
127  return *m_str;
128  }
130  unique_ptr<CNcbiOstrstream> m_str;
131 };
132 
133 
134 ///////////////////////////////////////////////////////
135 ///
136 /// Read a string from a stream. The string is following
137 /// by its size
138 ///
140 {
141  string str;
142  if (!is.good() || is.eof())
143  return str;
144 
145  size_t size;
146  is >> size;
147  if (!is.good() || is.eof())
148  return str;
149  if (size > 0) {
151  is.read(buf.get(), size);
152  size_t count = (size_t)is.gcount();
153  if (count > 0)
154  str.append(buf.get()+1, count-1);
155  }
156  return str;
157 }
158 
159 ///////////////////////////////////////////////////////
160 ///
161 /// Write a map to a stream
162 ///
163 template<typename TMap>
164 CNcbiOstream& WriteMap(CNcbiOstream& os, const TMap& cont)
165 {
166  typedef CContElemConverter<typename TMap::key_type> TKeyConverter;
167  typedef CContElemConverter<typename TMap::mapped_type> TValueConverter;
168 
169  COStreamHelper ostr(os);
170  ITERATE(typename TMap, it, cont) {
171  if (it != cont.begin())
172  ostr << '&';
173  ostr << NStr::URLEncode(TKeyConverter::ToString(it->first)) << '='
175  }
176  ostr.flush(true);
177  return os;
178 }
179 
180 
181 ///////////////////////////////////////////////////////
182 ///
183 /// Read a map from a stream
184 ///
185 template<typename TMap>
187 {
188  typedef CContElemConverter<typename TMap::key_type> TKeyConverter;
189  typedef CContElemConverter<typename TMap::mapped_type> TValueConverter;
190 
191  string input = ReadStringFromStream(is);
192 
193  vector<CTempString> pairs;
194  NStr::Split(input, "&", pairs,
196 
197  cont.clear();
198  ITERATE(vector<CTempString>, it, pairs) {
201  NStr::SplitInTwo(*it, "=", key, value);
202  cont.insert(typename TMap::value_type(
203  TKeyConverter::FromString(NStr::URLDecode(key)),
204  TValueConverter::FromString(NStr::URLDecode(value)))
205  );
206  }
207 
208  return is;
209 }
210 
211 ///////////////////////////////////////////////////////
212 ///
213 /// Write a container to a stream
214 ///
215 template<typename TCont>
216 CNcbiOstream& WriteContainer(CNcbiOstream& os, const TCont& cont)
217 {
218  typedef CContElemConverter<typename TCont::value_type> TValueConverter;
219  COStreamHelper ostr(os);
220  ITERATE(typename TCont, it, cont) {
221  if (it != cont.begin())
222  ostr << '&';
224  }
225  ostr.flush(true);
226  return os;
227 }
228 
229 ///////////////////////////////////////////////////////
230 ///
231 /// Read a container from a stream
232 ///
233 template<typename TCont>
235 {
236  typedef CContElemConverter<typename TCont::value_type> TValueConverter;
237 
238  string input = ReadStringFromStream(is);
239 
240  vector<CTempString> strings;
241  NStr::Split(input, "&", strings,
243 
244  cont.clear();
245  ITERATE(vector<CTempString>, it, strings) {
246  cont.push_back(TValueConverter::FromString(NStr::URLDecode(*it)));
247  }
248 
249  return is;
250 }
251 
252 
253 ///////////////////////////////////////////////////////
254 ///
255 /// Write cgi cookeis to a stream
256 ///
258 extern CNcbiOstream&
259 WriteCgiCookies(CNcbiOstream& os, const CCgiCookies& cont);
260 
261 ///////////////////////////////////////////////////////
262 ///
263 /// Read cgi cookeis from a stream
264 ///
266 extern CNcbiIstream&
268 
269 ///////////////////////////////////////////////////////
270 ///
271 /// Write an environment to a stream
272 ///
274 extern CNcbiOstream&
276 
277 ///////////////////////////////////////////////////////
278 ///
279 /// Write an environment from a stream
280 ///
282 extern CNcbiIstream&
284 
286 
287 #endif /* CGI___CGI_SERIAL__HPP */
CNcbiIstream & ReadMap(CNcbiIstream &is, TMap &cont)
Read a map from a stream.
Definition: cgi_serial.hpp:186
CNcbiIstream & ReadEnvironment(CNcbiIstream &is, CNcbiEnvironment &cont)
Write an environment from a stream.
Definition: cgi_serial.cpp:111
CNcbiIstream & ReadCgiCookies(CNcbiIstream &is, CCgiCookies &cont)
Read cgi cookeis from a stream.
Definition: cgi_serial.cpp:87
CNcbiOstream & WriteContainer(CNcbiOstream &os, const TCont &cont)
Write a container to a stream.
Definition: cgi_serial.hpp:216
CNcbiOstream & WriteEnvironment(CNcbiOstream &os, const CNcbiEnvironment &cont)
Write an environment to a stream.
Definition: cgi_serial.cpp:98
CNcbiOstream & WriteMap(CNcbiOstream &os, const TMap &cont)
Write a map to a stream.
Definition: cgi_serial.hpp:164
CNcbiOstream & WriteCgiCookies(CNcbiOstream &os, const CCgiCookies &cont)
Write cgi cookeis to a stream.
Definition: cgi_serial.cpp:79
string ReadStringFromStream(CNcbiIstream &is)
Read a string from a stream.
Definition: cgi_serial.hpp:139
CNcbiIstream & ReadContainer(CNcbiIstream &is, TCont &cont)
Read a container from a stream.
Definition: cgi_serial.hpp:234
AutoPtr –.
Definition: ncbimisc.hpp:401
CCgiCookies::
Definition: ncbicgi.hpp:219
static const string & FromString(const string &str)
Definition: cgi_serial.hpp:69
static const string & ToString(const string &str)
Definition: cgi_serial.hpp:70
template<typename TElem> CContElemConverter
Definition: cgi_serial.hpp:52
static TElem FromString(const string &str)
Definition: cgi_serial.hpp:54
static string ToString(const TElem &elem)
Definition: cgi_serial.hpp:55
CNcbiEnvironment –.
Definition: ncbienv.hpp:110
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
COStreamHelper.
Definition: cgi_serial.hpp:97
void flush(bool write_empty_data=false)
Definition: cgi_serial.hpp:111
COStreamHelper & operator<<(const T &t)
Definition: cgi_serial.hpp:105
CNcbiOstream & x_GetStrm()
Definition: cgi_serial.hpp:123
COStreamHelper(CNcbiOstream &os)
Definition: cgi_serial.hpp:99
unique_ptr< CNcbiOstrstream > m_str
Definition: cgi_serial.hpp:130
CNcbiOstream & m_Ostream
Definition: cgi_serial.hpp:129
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
Include a standard set of the NCBI C++ Toolkit most basic headers.
The NCBI C++ standard methods for dealing with std::string.
#define T(s)
Definition: common.h:230
static const char * str(char *buf, int n)
Definition: stats.c:84
static const char *const strings[]
Definition: utf8.c:21
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
static string URLDecode(const CTempString str, EUrlDecode flag=eUrlDec_All)
URL-decode string.
Definition: ncbistr.cpp:6214
static bool SplitInTwo(const CTempString str, const CTempString delim, string &str1, string &str2, TSplitFlags flags=0)
Split a string into two pieces using the specified delimiters.
Definition: ncbistr.cpp:3554
static string URLEncode(const CTempString str, EUrlEncode flag=eUrlEnc_SkipMarkChars)
URL-encode string.
Definition: ncbistr.cpp:6062
@ fSplit_Truncate
Definition: ncbistr.hpp:2501
@ fSplit_MergeDelimiters
Merge adjacent delimiters.
Definition: ncbistr.hpp:2498
#define NCBI_XCGI_EXPORT
Definition: ncbi_export.h:1097
char * buf
static int input()
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
const struct ncbi::grid::netcache::search::fields::SIZE size
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
EIPRangeType t
Definition: ncbi_localip.c:101
#define nullptr
Definition: ncbimisc.hpp:45
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
string ToString(const wxRect &rc)
Definition: wx_utils.cpp:773
Modified on Tue Apr 23 07:40:24 2024 by modify_doxy.py rev. 669887