Grid Community Toolkit  6.2.1550507116
modp_burl.h
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
3 
18 #ifndef COM_MODP_STRINGENCODERS_BURL
19 #define COM_MODP_STRINGENCODERS_BURL
20 
21 #ifdef __cplusplus
22 #define BEGIN_C extern "C" {
23 #define END_C }
24 #else
25 #define BEGIN_C
26 #define END_C
27 #endif
28 
29 BEGIN_C
30 
45 int modp_burl_encode(char* dest, const char* str, int len);
46 
59 int modp_burl_min_encode(char* dest, const char* str, int len);
60 
67 int modp_burl_min_encode_strlen(const char* src, const int len);
68 
73 #define modp_burl_encode_len(A) (3*A + 1)
74 
90 int modp_burl_encode_strlen(const char* str, const int len);
91 
102 int modp_burl_decode(char* dest, const char* str, int len);
103 
109 #define modp_burl_decode_len(A) (A + 1)
110 
111 END_C
112 
113 #ifdef __cplusplus
114 #include <cstring>
115 #include <string>
116 
117 namespace modp {
118 
119  inline std::string url_encode(const char* s, size_t len)
120  {
121  std::string x(modp_burl_encode_len(len), '\0');
122  int d = modp_burl_encode(const_cast<char*>(x.data()), s, len);
123  x.erase(d, std::string::npos);
124  return x;
125  }
126 
127  inline std::string url_encode(const char* s)
128  {
129  return url_encode(s, strlen(s));
130  }
131 
132  inline std::string url_encode(const std::string& s)
133  {
134  return url_encode(s.data(), s.size());
135  }
136 
143  inline std::string& url_encode(std::string& s)
144  {
145  std::string x(url_encode(s.data(), s.size()));
146  s.swap(x);
147  return s;
148  }
149 
156  inline std::string& url_min_encode(std::string& s)
157  {
158  std::string x(modp_burl_encode_len(s.size()), '\0');
159  int d = modp_burl_min_encode(const_cast<char*>(x.data()), s.data(), s.size());
160  x.erase(d, std::string::npos);
161  s.swap(x);
162  return s;
163  }
164 
165  inline std::string url_min_encode(const std::string& s)
166  {
167  std::string x(modp_burl_encode_len(s.size()), '\0');
168  int d = modp_burl_min_encode(const_cast<char*>(x.data()), s.data(), s.size());
169  x.erase(d, std::string::npos);
170  return x;
171  }
172 
181  inline std::string& url_decode(std::string& s)
182  {
183  int d = modp_burl_decode(const_cast<char*>(s.data()), s.data(), s.size());
184  s.erase(d, std::string::npos);
185  return s;
186  }
187 
188  inline std::string url_decode(const char* str)
189  {
190  std::string s(str);
191  url_decode(s);
192  return s;
193  }
194 
195  inline std::string url_decode(const char* str, size_t len)
196  {
197  std::string s(str, len);
198  url_decode(s);
199  return s;
200  }
201 
202  inline std::string url_decode(const std::string& s)
203  {
204  std::string x(s);
205  url_decode(x);
206  return x;
207  }
208 }
209 #endif
210 
211 #endif
int modp_burl_min_encode_strlen(const char *src, const int len)
get size of output string w/o doing actual encoding
int modp_burl_decode(char *dest, const char *str, int len)
#define modp_burl_encode_len(A)
Definition: modp_burl.h:73
int modp_burl_min_encode(char *dest, const char *str, int len)
BEGIN_C int modp_burl_encode(char *dest, const char *str, int len)
int modp_burl_encode_strlen(const char *str, const int len)