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.
 
 
 
 
 
 

201 lines
5.7 KiB

  1. #define CPPCMS_SOURCE
  2. #include "filters.h"
  3. #include "base64.h"
  4. #include "util.h"
  5. #include <iostream>
  6. namespace cppcms { namespace filters {
  7. streamable::streamable()
  8. {
  9. set(0,0,0,0);
  10. }
  11. streamable::streamable(streamable const &other)
  12. {
  13. set(other.ptr_,other.to_stream_,other.to_string_,other.type_);
  14. }
  15. streamable::~streamable()
  16. {
  17. }
  18. streamable const &streamable::operator=(streamable const &other)
  19. {
  20. if(&other!=this)
  21. set(other.ptr_,other.to_stream_,other.to_string_,other.type_);
  22. return *this;
  23. }
  24. namespace {
  25. void ch_to_stream(std::ostream &out,void const *p)
  26. {
  27. out<<reinterpret_cast<char const *>(p);
  28. }
  29. std::string ch_to_string(std::ios &ios,void const *p)
  30. {
  31. return reinterpret_cast<char const *>(p);
  32. }
  33. std::string s_to_string(std::ios &ios,void const *p)
  34. {
  35. return *reinterpret_cast<std::string const *>(p);
  36. }
  37. }
  38. streamable::streamable(char const *ptr)
  39. {
  40. set(ptr,ch_to_stream,ch_to_string,&typeid(char const *));
  41. }
  42. template<>
  43. streamable::streamable(std::string const &str)
  44. {
  45. set(&str,to_stream<std::string>,s_to_string,&typeid(std::string));
  46. }
  47. std::string streamable::get(std::ios &ios) const
  48. {
  49. return to_string_(ios,ptr_);
  50. }
  51. void streamable::operator()(std::ostream &out) const
  52. {
  53. to_stream_(out,ptr_);
  54. }
  55. void streamable::set(void const *ptr,to_stream_type tse,to_string_type tst,std::type_info const *type)
  56. {
  57. ptr_=ptr;
  58. to_stream_=tse;
  59. to_string_=tst;
  60. type_=type;
  61. }
  62. std::type_info const &streamable::type() const
  63. {
  64. return *type_;
  65. }
  66. ///////////////////////////////////
  67. struct to_upper::data {};
  68. to_upper::to_upper() {}
  69. to_upper::~to_upper() {}
  70. to_upper::to_upper(to_upper const &other) : obj_(other.obj_) {}
  71. to_upper::to_upper(streamable const &obj) : obj_(obj) {}
  72. to_upper const &to_upper::operator=(to_upper const &other){ obj_ = other.obj_; return *this; }
  73. void to_upper::operator()(std::ostream &out) const
  74. {
  75. std::string tmp =obj_.get(out) ;
  76. std::locale loc = out.getloc();
  77. out << ::cppcms::locale::to_upper( tmp,loc);
  78. }
  79. struct to_lower::data {};
  80. to_lower::to_lower() {}
  81. to_lower::~to_lower() {}
  82. to_lower::to_lower(to_lower const &other) : obj_(other.obj_) {}
  83. to_lower::to_lower(streamable const &obj) : obj_(obj) {}
  84. to_lower const &to_lower::operator=(to_lower const &other){ obj_ = other.obj_; return *this; }
  85. void to_lower::operator()(std::ostream &out) const
  86. {
  87. out << locale::to_lower(obj_.get(out),out.getloc());
  88. }
  89. struct to_title::data {};
  90. to_title::to_title() {}
  91. to_title::~to_title() {}
  92. to_title::to_title(to_title const &other) : obj_(other.obj_) {}
  93. to_title::to_title(streamable const &obj) : obj_(obj) {}
  94. to_title const &to_title::operator=(to_title const &other){ obj_ = other.obj_; return *this; }
  95. void to_title::operator()(std::ostream &out) const
  96. {
  97. out << locale::to_title(obj_.get(out),out.getloc());
  98. }
  99. struct escape::data {};
  100. escape::escape() {}
  101. escape::~escape() {}
  102. escape::escape(escape const &other) : obj_(other.obj_) {}
  103. escape::escape(streamable const &obj) : obj_(obj) {}
  104. escape const &escape::operator=(escape const &other){ obj_ = other.obj_; return *this; }
  105. void escape::operator()(std::ostream &out) const
  106. {
  107. out << util::escape(obj_.get(out));
  108. }
  109. struct urlencode::data {};
  110. urlencode::urlencode() {}
  111. urlencode::~urlencode() {}
  112. urlencode::urlencode(urlencode const &other) : obj_(other.obj_) {}
  113. urlencode::urlencode(streamable const &obj) : obj_(obj) {}
  114. urlencode const &urlencode::operator=(urlencode const &other){ obj_ = other.obj_; return *this; }
  115. void urlencode::operator()(std::ostream &out) const
  116. {
  117. out << util::urlencode(obj_.get(out));
  118. }
  119. struct raw::data {};
  120. raw::raw() {}
  121. raw::~raw() {}
  122. raw::raw(raw const &other) : obj_(other.obj_) {}
  123. raw::raw(streamable const &obj) : obj_(obj) {}
  124. raw const &raw::operator=(raw const &other){ obj_ = other.obj_; return *this; }
  125. void raw::operator()(std::ostream &out) const
  126. {
  127. out << obj_;;
  128. }
  129. struct base64_urlencode::data {};
  130. base64_urlencode::base64_urlencode() {}
  131. base64_urlencode::~base64_urlencode() {}
  132. base64_urlencode::base64_urlencode(base64_urlencode const &other) : obj_(other.obj_) {}
  133. base64_urlencode::base64_urlencode(streamable const &obj) : obj_(obj) {}
  134. base64_urlencode const &base64_urlencode::operator=(base64_urlencode const &other){ obj_ = other.obj_; return *this; }
  135. void base64_urlencode::operator()(std::ostream &os) const
  136. {
  137. std::string const s=obj_.get(os);
  138. using namespace cppcms::b64url;
  139. unsigned char const *begin=reinterpret_cast<unsigned char const *>(s.c_str());
  140. unsigned char const *end=begin+s.size();
  141. std::vector<unsigned char> out(encoded_size(s.size())+1);
  142. encode(begin,end,&out.front());
  143. out.back()=0;
  144. char const *buf=reinterpret_cast<char const *>(out.front());
  145. os<<buf;
  146. }
  147. struct date::data {};
  148. struct time::data {};
  149. struct datetime::data {};
  150. date::date() : time_(0) {}
  151. datetime::datetime() : time_(0){}
  152. time::time() : time_(0) {}
  153. date::~date() {}
  154. datetime::~datetime() {}
  155. time::~time() {}
  156. date::date(date const &other) : time_(other.time_) {}
  157. time::time(time const &other) : time_(other.time_) {}
  158. datetime::datetime(datetime const &other) : time_(other.time_) {}
  159. date const &date::operator=(date const &other) { time_=other.time_; return *this; }
  160. time const &time::operator=(time const &other) { time_=other.time_; return *this; }
  161. datetime const &datetime::operator=(datetime const &other) { time_=other.time_; return *this; }
  162. date::date(double t) : time_(t) {}
  163. time::time(double t) : time_(t) {}
  164. datetime::datetime(double t) : time_(t) {}
  165. void date::operator()(std::ostream &out) const
  166. {
  167. out << format("{1,date}") % time_;
  168. }
  169. void time::operator()(std::ostream &out) const
  170. {
  171. out << format("{1,time}") % time_;
  172. }
  173. void datetime::operator()(std::ostream &out) const
  174. {
  175. out << format("{1,datetime}") % time_;
  176. }
  177. }} // cppcms::filters