Menu

[r767]: / framework / branches / refactoring / base_view.cpp  Maximize  Restore  History

Download this file

233 lines (186 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#define CPPCMS_SOURCE
#include "base_view.h"
#include "util.h"
#include "locale_gettext.h"
#include "locale_environment.h"
#include "base64.h"
#include "locale_info.h"
#include "utf_iterator.h"
#include "cppcms_error.h"
#include <vector>
#include <boost/format.hpp>
namespace cppcms {
struct base_view::data {
std::ostream *out;
locale::gettext::tr const *tr;
};
base_view::base_view(std::ostream &out) :
d(new data)
{
d->out=&out;
set_domain("");
}
void base_view::set_domain(std::string domain)
{
if(std::has_facet<cppcms::locale::gettext>(out().getloc())) {
if(!domain.empty())
d->tr = &std::use_facet<cppcms::locale::gettext>(out().getloc()).dictionary(domain.c_str());
else
d->tr = &std::use_facet<cppcms::locale::gettext>(out().getloc()).dictionary();
}
else {
static const locale::gettext::tr t;
d->tr=&t;
}
}
std::ostream &base_view::out()
{
return *d->out;
}
char const *base_view::gettext(char const *s)
{
return d->tr->gettext(s);
}
char const *base_view::ngettext(char const *s,char const *s1,int m)
{
return d->tr->ngettext(s,s1,m);
}
base_view::~base_view()
{
}
void base_view::render()
{
}
void base_view::date(std::ostream &s,std::tm const &t)
{
if(s.getloc().name()!="C")
strftime("%x")(s,t);
else
strftime("%Y-%m-%d")(s,t);
}
void base_view::time(std::ostream &s,std::tm const &t)
{
if(s.getloc().name()!="C")
strftime("%X")(s,t);
else
strftime("%H:%M:%S")(s,t);
}
base_view::strftime::strftime(std::string f) : format_(f)
{
}
base_view::strftime::~strftime()
{
}
void base_view::strftime::operator()(std::ostream &out,std::tm const &time) const
{
char const *begin=format_.data();
char const *end=begin+format_.size();
std::use_facet<std::time_put<char> >(out.getloc()).put(out,out,' ',&time,begin,end);
}
void base_view::urlencode(std::ostream &out,std::string const &s)
{
out << util::urlencode(s);
}
void base_view::base64_encode(std::ostream &os,std::string const &s)
{
using namespace cppcms::b64url;
unsigned char const *begin=reinterpret_cast<unsigned char const *>(s.c_str());
unsigned char const *end=begin+s.size();
std::vector<unsigned char> out(encoded_size(s.size())+1);
encode(begin,end,&out.front());
out.back()=0;
char const *buf=reinterpret_cast<char const *>(out.front());
os<<buf;
}
namespace {
void to_something( std::ostream &out,
std::string const &s,
char (std::ctype<char>::*narop)(char) const,
wchar_t (std::ctype<wchar_t>::*wideop)(wchar_t) const)
{
using namespace std;
std::locale l=out.getloc();
if(!has_facet<locale::info>(l)
|| !use_facet<locale::info>(l).is_utf8()
|| !has_facet<ctype<wchar_t> >(l))
{
ctype<char> const &converter=use_facet<ctype<char> >(l);
for(unsigned i=0;i<s.size();i++)
out.put((converter.*narop)(s[i]));
return;
}
ctype<wchar_t> const &converter=use_facet<ctype<wchar_t> >(l);
std::string::const_iterator p=s.begin(),e=s.end();
uint32_t code_point;
while(p!=e && (code_point=cppcms::utf8::next(p,e,false,true))!=utf::illegal) {
if(sizeof(wchar_t) == 4 || code_point <=0xFFFF)
code_point=(converter.*wideop)(code_point);
utf8::seq s=utf8::encode(code_point);
unsigned i=0;
do {
out.put(s.c[i]);
i++;
}while(i<sizeof(s.c) && s.c[i]!=0);
}
if(p!=e) {
out.write(s.c_str() + (p-s.begin()), (e-p));
}
}
} // anonymous
void base_view::to_lower(std::ostream &out,std::string const &s)
{
to_something(out,s,&std::ctype<char>::tolower,&std::ctype<wchar_t>::tolower);
}
void base_view::to_upper(std::ostream &out,std::string const &s)
{
to_something(out,s,&std::ctype<char>::toupper,&std::ctype<wchar_t>::toupper);
}
base_view::doublef::~doublef(){}
base_view::intf::~intf(){}
base_view::doublef::doublef(std::string f) : format_(f) {}
base_view::intf::intf(std::string f) : format_(f) {}
void base_view::intf::operator()(std::ostream &out,int x) const
{
boost::format f(format_);
f.exceptions(0);
out<<f % x;
}
void base_view::doublef::operator()(std::ostream &out,double x) const
{
boost::format f(format_);
f.exceptions(0);
out<<f % x;
}
namespace details {
struct views_storage::data {};
views_storage &views_storage::instance() {
static views_storage this_instance;
return this_instance;
};
void views_storage::add_view(std::string t,std::string v,view_factory_type f)
{
storage[t][v]=f;
}
void views_storage::remove_views(std::string t)
{
storage.erase(t);
}
std::auto_ptr<base_view> views_storage::fetch_view(std::string t,std::string v,std::ostream &s,base_content *c)
{
templates_type::iterator p=storage.find(t);
if(p==storage.end())
throw cppcms_error("Can't find skin "+t);
template_views_type::iterator p2=p->second.find(v);
if(p2==p->second.end())
throw cppcms_error("Can't find template "+v);
view_factory_type &f=p2->second;
return f(s,c);
}
views_storage::views_storage()
{
}
views_storage::~views_storage()
{
}
} // details
}// cppcms
MongoDB Logo MongoDB