ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

223 lines
6.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #define CPPCMS_SOURCE
  20. #include "filters.h"
  21. #include "base64.h"
  22. #include "util.h"
  23. #include <iostream>
  24. namespace cppcms { namespace filters {
  25. streamable::streamable()
  26. {
  27. set(0,0,0,0);
  28. }
  29. streamable::streamable(streamable const &other)
  30. {
  31. set(other.ptr_,other.to_stream_,other.to_string_,other.type_);
  32. }
  33. streamable::~streamable()
  34. {
  35. }
  36. streamable const &streamable::operator=(streamable const &other)
  37. {
  38. if(&other!=this)
  39. set(other.ptr_,other.to_stream_,other.to_string_,other.type_);
  40. return *this;
  41. }
  42. namespace {
  43. void ch_to_stream(std::ostream &out,void const *p)
  44. {
  45. out<<reinterpret_cast<char const *>(p);
  46. }
  47. std::string ch_to_string(std::ios &ios,void const *p)
  48. {
  49. return reinterpret_cast<char const *>(p);
  50. }
  51. std::string s_to_string(std::ios &ios,void const *p)
  52. {
  53. return *reinterpret_cast<std::string const *>(p);
  54. }
  55. }
  56. streamable::streamable(char const *ptr)
  57. {
  58. set(ptr,ch_to_stream,ch_to_string,&typeid(char const *));
  59. }
  60. template<>
  61. streamable::streamable(std::string const &str)
  62. {
  63. set(&str,to_stream<std::string>,s_to_string,&typeid(std::string));
  64. }
  65. std::string streamable::get(std::ios &ios) const
  66. {
  67. return to_string_(ios,ptr_);
  68. }
  69. void streamable::operator()(std::ostream &out) const
  70. {
  71. to_stream_(out,ptr_);
  72. }
  73. void streamable::set(void const *ptr,to_stream_type tse,to_string_type tst,std::type_info const *type)
  74. {
  75. ptr_=ptr;
  76. to_stream_=tse;
  77. to_string_=tst;
  78. type_=type;
  79. }
  80. std::type_info const &streamable::type() const
  81. {
  82. return *type_;
  83. }
  84. ///////////////////////////////////
  85. struct to_upper::data {};
  86. to_upper::to_upper() {}
  87. to_upper::~to_upper() {}
  88. to_upper::to_upper(to_upper const &other) : obj_(other.obj_) {}
  89. to_upper::to_upper(streamable const &obj) : obj_(obj) {}
  90. to_upper const &to_upper::operator=(to_upper const &other){ obj_ = other.obj_; return *this; }
  91. void to_upper::operator()(std::ostream &out) const
  92. {
  93. std::string tmp =obj_.get(out) ;
  94. std::locale loc = out.getloc();
  95. out << ::cppcms::locale::to_upper( tmp,loc);
  96. }
  97. struct to_lower::data {};
  98. to_lower::to_lower() {}
  99. to_lower::~to_lower() {}
  100. to_lower::to_lower(to_lower const &other) : obj_(other.obj_) {}
  101. to_lower::to_lower(streamable const &obj) : obj_(obj) {}
  102. to_lower const &to_lower::operator=(to_lower const &other){ obj_ = other.obj_; return *this; }
  103. void to_lower::operator()(std::ostream &out) const
  104. {
  105. out << locale::to_lower(obj_.get(out),out.getloc());
  106. }
  107. #ifndef CPPCMS_USE_STD_LOCALES
  108. struct to_title::data {};
  109. to_title::to_title() {}
  110. to_title::~to_title() {}
  111. to_title::to_title(to_title const &other) : obj_(other.obj_) {}
  112. to_title::to_title(streamable const &obj) : obj_(obj) {}
  113. to_title const &to_title::operator=(to_title const &other){ obj_ = other.obj_; return *this; }
  114. void to_title::operator()(std::ostream &out) const
  115. {
  116. out << locale::to_title(obj_.get(out),out.getloc());
  117. }
  118. #endif
  119. struct escape::data {};
  120. escape::escape() {}
  121. escape::~escape() {}
  122. escape::escape(escape const &other) : obj_(other.obj_) {}
  123. escape::escape(streamable const &obj) : obj_(obj) {}
  124. escape const &escape::operator=(escape const &other){ obj_ = other.obj_; return *this; }
  125. void escape::operator()(std::ostream &out) const
  126. {
  127. out << util::escape(obj_.get(out));
  128. }
  129. struct urlencode::data {};
  130. urlencode::urlencode() {}
  131. urlencode::~urlencode() {}
  132. urlencode::urlencode(urlencode const &other) : obj_(other.obj_) {}
  133. urlencode::urlencode(streamable const &obj) : obj_(obj) {}
  134. urlencode const &urlencode::operator=(urlencode const &other){ obj_ = other.obj_; return *this; }
  135. void urlencode::operator()(std::ostream &out) const
  136. {
  137. out << util::urlencode(obj_.get(out));
  138. }
  139. struct raw::data {};
  140. raw::raw() {}
  141. raw::~raw() {}
  142. raw::raw(raw const &other) : obj_(other.obj_) {}
  143. raw::raw(streamable const &obj) : obj_(obj) {}
  144. raw const &raw::operator=(raw const &other){ obj_ = other.obj_; return *this; }
  145. void raw::operator()(std::ostream &out) const
  146. {
  147. out << obj_;;
  148. }
  149. struct base64_urlencode::data {};
  150. base64_urlencode::base64_urlencode() {}
  151. base64_urlencode::~base64_urlencode() {}
  152. base64_urlencode::base64_urlencode(base64_urlencode const &other) : obj_(other.obj_) {}
  153. base64_urlencode::base64_urlencode(streamable const &obj) : obj_(obj) {}
  154. base64_urlencode const &base64_urlencode::operator=(base64_urlencode const &other){ obj_ = other.obj_; return *this; }
  155. void base64_urlencode::operator()(std::ostream &os) const
  156. {
  157. std::string const s=obj_.get(os);
  158. using namespace cppcms::b64url;
  159. unsigned char const *begin=reinterpret_cast<unsigned char const *>(s.c_str());
  160. unsigned char const *end=begin+s.size();
  161. std::vector<unsigned char> out(encoded_size(s.size())+1);
  162. encode(begin,end,&out.front());
  163. out.back()=0;
  164. char const *buf=reinterpret_cast<char const *>(out.front());
  165. os<<buf;
  166. }
  167. struct date::data {};
  168. struct time::data {};
  169. struct datetime::data {};
  170. date::date() : time_(0) {}
  171. datetime::datetime() : time_(0){}
  172. time::time() : time_(0) {}
  173. date::~date() {}
  174. datetime::~datetime() {}
  175. time::~time() {}
  176. date::date(date const &other) : time_(other.time_) {}
  177. time::time(time const &other) : time_(other.time_) {}
  178. datetime::datetime(datetime const &other) : time_(other.time_) {}
  179. date const &date::operator=(date const &other) { time_=other.time_; return *this; }
  180. time const &time::operator=(time const &other) { time_=other.time_; return *this; }
  181. datetime const &datetime::operator=(datetime const &other) { time_=other.time_; return *this; }
  182. date::date(double t) : time_(t) {}
  183. time::time(double t) : time_(t) {}
  184. datetime::datetime(double t) : time_(t) {}
  185. void date::operator()(std::ostream &out) const
  186. {
  187. out << format("{1,date}") % time_;
  188. }
  189. void time::operator()(std::ostream &out) const
  190. {
  191. out << format("{1,time}") % time_;
  192. }
  193. void datetime::operator()(std::ostream &out) const
  194. {
  195. out << format("{1,datetime}") % time_;
  196. }
  197. }} // cppcms::filters