HtmlTextStream has now pt::Stream as a based class and uses pt::WTextStream as a buffer

master
Tomasz Sowa 2 years ago
parent ba6159964b
commit c5c02d7f44

@ -696,7 +696,7 @@ void App::ClearAfterRequest()
try
{
// simple operations which should not throw an exception
json_out_stream.Clear();
json_out_stream.clear();
templates.ClearAfterRequest();
cur.request->Clear();
cur.session->ClearAfterRequest();
@ -767,7 +767,7 @@ void App::SaveSessionsIfNeeded()
const std::wstring * App::CreateFrameAnswer()
pt::WTextStream * App::CreateFrameAnswer()
{
Request & req = *cur.request;
auto i = req.out_streams.streams_map.begin();
@ -777,8 +777,7 @@ const std::wstring * App::CreateFrameAnswer()
{
if( (frame && i->first == *frame) || (!frame && i->first == L"content") )
{
return &i->second->Str();
break;
return &i->second->get_buffer();
}
}
@ -786,10 +785,10 @@ const std::wstring * App::CreateFrameAnswer()
}
void App::CreateJSONAnswer()
pt::WTextStream * App::CreateJSONAnswer()
{
Request & req = *cur.request;
json_out_stream.Clear();
json_out_stream.clear();
if( !req.return_info_only )
{
@ -806,9 +805,9 @@ void App::CreateJSONAnswer()
}
json_out_stream << L"\"";
JSONescape(json_out_stream, i->first);
JSONescape(i->first, json_out_stream);
json_out_stream << L"\": \"";
JSONescape(json_out_stream, i->second->Str());
JSONescapeStream(i->second->get_buffer(), json_out_stream);
json_out_stream << L"\"";
is_first = false;
}
@ -825,6 +824,8 @@ void App::CreateJSONAnswer()
log << " (Request::info only)";
log << logend;
return &json_out_stream;
}
@ -1736,9 +1737,16 @@ void App::FilterContent()
if( filter_main_stream )
{
// !! IMPROVE ME may some kind of html_filtered.reserve() here? (optimization)
TemplatesFunctions::html_filter.Filter(req.out_main_stream.Str(), html_filtered);
req.out_main_stream.Str(std::move(html_filtered)); // !! IMPROVE ME we do not have Str(&&) method
std::wstring tmp_out_main_stream;
req.out_main_stream.to_str(tmp_out_main_stream, false);
TemplatesFunctions::html_filter.Filter(tmp_out_main_stream, html_filtered); // IMPROVEME let Filter take pt::WTextStream
/*
* it would be better to just return a pointer from this method
*
*/
req.out_main_stream.clear();
req.out_main_stream.PutText(html_filtered);
log << log3 << "App: html in the main stream has been filtered" << logend;
}
@ -1747,9 +1755,14 @@ void App::FilterContent()
for(auto i = req.out_streams.streams_map.begin() ; i != req.out_streams.streams_map.end() ; ++i)
{
HtmlTextStream & stream = *i->second;
// !! IMPROVE ME may some kind of html_filtered.reserve() here? (optimization)
TemplatesFunctions::html_filter.Filter(stream.Str(), html_filtered);
stream.Str(std::move(html_filtered));
std::wstring tmp_stream;
stream.to_str(tmp_stream, false);
TemplatesFunctions::html_filter.Filter(tmp_stream, html_filtered);
stream.clear();
stream.PutText(html_filtered);
}
log << log3 << "App: html in json out streams have been filtered" << logend;
@ -1878,22 +1891,25 @@ return header;
void App::SendTextAnswer()
{
const std::wstring * source = nullptr;
const pt::WTextStream * source = nullptr;
bool compressing = false;
int compress_encoding = 0;
size_t output_size = 0;
Header header = GetHTTPStatusCode();
if( CanSendContent() )
{
/*
* FIXME frames are not filtered (when is_htmx_request is true)
*
* FilterContent() should be combined with CreateJSONAnswer() and CreateFrameAnswer() somehow
*/
FilterContent();
if( cur.request->return_json )
{
CreateJSONAnswer();
source = &json_out_stream.Str(); // json_out_stream was prepared by CreateJSONAnswer()
source = CreateJSONAnswer();
}
else
if( cur.request->is_htmx_request )
@ -1908,12 +1924,12 @@ size_t output_size = 0;
}
else
{
source = &cur.request->out_main_stream.Str();
source = &cur.request->out_main_stream.get_buffer();
}
SelectCompression(source->length(), compressing, compress_encoding);
SelectCompression(source->size(), compressing, compress_encoding);
pt::wide_to_utf8(*source, output_8bit);
pt::wide_stream_to_utf8(*source, output_8bit);
// !! IMPROVE ME add to log the binary stream as well
if( config.log_server_answer )

@ -147,7 +147,7 @@ private:
pthread_t signal_thread;
std::string socket_to_send_on_exit;
std::string send_data_buf;
TextStream<std::wstring> json_out_stream;
pt::WTextStream json_out_stream;
std::string aheader_name, aheader_value;
std::wstring html_filtered;
std::string output_8bit;
@ -156,7 +156,7 @@ private:
std::wstring http_header_name;
std::wstring http_header_value;
std::string http_header_8bit;
std::wstring empty_response;
pt::WTextStream empty_response;
WinixModelConnector model_connector; // main thread model connector, each thread has its own connector
morm::JSONConnector json_connector;
@ -200,8 +200,8 @@ private:
void SaveSessionsIfNeeded(); // !! IMPROVE ME wywalic do menagera sesji??
void LogAccess();
void SendData(const BinaryPage & page, FCGX_Stream * out);
const std::wstring * CreateFrameAnswer();
void CreateJSONAnswer();
pt::WTextStream * CreateFrameAnswer();
pt::WTextStream * CreateJSONAnswer();
void ReadRequest();
void SendTextAnswer();

@ -1450,6 +1450,17 @@ void RemovePostFileTmp(PostFileTab & post_file_tab)
}
void JSONescapeStream(const pt::WTextStream & in, pt::WTextStream & out)
{
pt::WTextStream::const_iterator i = in.begin();
for( ; i != in.end() ; ++i)
{
JSONescape(*i, out);
}
}
bool wide_to_utf8(const wchar_t * wide_string, char * utf8, size_t utf8_size)
{
bool res = pt::wide_to_utf8(wide_string, utf8, utf8_size);

@ -946,20 +946,11 @@ void SetMinMax(IntType & val, IntType min_val, IntType max_val)
}
template<class Stream, class StringType>
void JSONescape(Stream & out, const StringType & str)
template<class Stream>
void JSONescape(wchar_t c, Stream & out)
{
// !! IMPROVE ME (optimizing)
// it is better to not write one by one character
// but use write method insted
for(size_t i=0 ; i<str.size() ; ++i)
switch( c )
{
switch(str[i])
{
case 0: out << '\\'; out << '0'; break;
case '\r': out << '\\'; out << 'r'; break;
case '\n': out << '\\'; out << 'n'; break;
@ -970,12 +961,31 @@ void JSONescape(Stream & out, const StringType & str)
//case '/': out << '\\'; out << '/'; break; // slash doesn't have to be escaped
case '"': out << '\\'; out << '\"'; break;
default:
out << str[i];
out << c;
}
}
template<class StringType, class Stream>
void JSONescape(const StringType & str, Stream & out)
{
for(size_t i=0 ; i < str.size() ; ++i)
{
if constexpr(sizeof(char) == sizeof(typename StringType::value_type))
{
JSONescape(static_cast<wchar_t>(static_cast<unsigned char>(str[i])), out);
}
else
{
JSONescape(str[i], out);
}
}
}
void JSONescapeStream(const pt::WTextStream & in, pt::WTextStream & out);
/*
* converting from a wide string to an UTF-8 string

@ -75,7 +75,7 @@ void Request::ClearOutputStreams()
{
size_t len = 0;
out_main_stream.Clear();
out_main_stream.clear();
if( config )
len = config->ezc_out_streams_size;

@ -526,7 +526,7 @@ void Rm::CreateJSON(bool status)
using TemplatesFunctions::R;
pt::WTextStream buf;
JSONescape(buf, cur->request->item.url);
JSONescape(cur->request->item.url, buf);
auto & out = cur->request->out_main_stream;
out << R("{\"files\": [{\"") << R(buf) << R("\": ");

@ -633,7 +633,7 @@ void Item::execute(EzcEnv & env)
else
ezc_generator.Generate(item_run_content);
ItemContent::print_content(env.out, item_run_content.Str(), item_content.content_raw_type, config->html_filter);
ItemContent::print_content(env.out, item_run_content.get_buffer(), item_content.content_raw_type, config->html_filter);
}
}
else

@ -389,6 +389,48 @@ bool ItemContent::CanContentBeHtmlFiltered()
void ItemContent::print_content(HtmlTextStream & out, const pt::WTextStream & content, ItemContent::ContentType content_type, bool is_html_filter_on)
{
using TemplatesFunctions::R;
if( is_html_filter_on && !ItemContent::CanContentBeHtmlFiltered(content_type) )
out << R("<nofilter>");
if( content_type == ItemContent::ct_text )
{
out << content;
}
else
if( content_type == ItemContent::ct_formatted_text )
{
std::wstring tmp_string;
content.to_str(tmp_string);
TemplatesFunctions::HtmlEscapeFormTxt(out, tmp_string);
}
else
if( content_type == ItemContent::ct_bbcode )
{
static std::wstring out_temp;
out_temp.clear();
out_temp.reserve(content.size()*2);
BBCODEParser bbcode_parser; // IMPROVE ME move me to a better place
std::wstring tmp_string;
content.to_str(tmp_string);
bbcode_parser.Filter(tmp_string.c_str(), out_temp);
out << R(out_temp);
}
else
{
// ct_html, ct_other
out.get_buffer().operator<<(content); // tricky way of putting content without escaping
}
if( is_html_filter_on && !ItemContent::CanContentBeHtmlFiltered(content_type) )
out << R("</nofilter>");
}
void ItemContent::print_content(HtmlTextStream & out, const std::wstring & content, ItemContent::ContentType content_type, bool is_html_filter_on)
{

@ -254,6 +254,7 @@ public:
bool do_migration(int & current_table_version);
static bool CanContentBeHtmlFiltered(ItemContent::ContentType ct);
static void print_content(HtmlTextStream & out, const pt::WTextStream & content, ItemContent::ContentType content_type, bool is_html_filter_on);
static void print_content(HtmlTextStream & out, const std::wstring & content, ItemContent::ContentType content_type, bool is_html_filter_on);
bool CanContentBeHtmlFiltered();

@ -353,12 +353,12 @@ void thread_sort_tab_run(Info & i)
{
Ezc::Pattern * p = pattern_cacher.GetPattern(*thread_info.item_sort_tab[item_sort_index]);
item_run_content.Clear();
item_run_content.clear();
InitGenerator(ezc_generator, cur->request->models);
ezc_generator.SetPattern(*p);
ezc_generator.Generate(item_run_content);
ItemContent::print_content(i.out, item_run_content.Str(), thread_info.item_sort_tab[item_sort_index]->item_content.content_raw_type, config->html_filter);
ItemContent::print_content(i.out, item_run_content.get_buffer(), thread_info.item_sort_tab[item_sort_index]->item_content.content_raw_type, config->html_filter);
}
else
{

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2008-2014, Tomasz Sowa
* Copyright (c) 2008-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -41,38 +41,37 @@ namespace Winix
namespace TemplatesFunctions
{
// not thread safe
static std::string qencode_tmp;
void fil_urlencode(Info & i)
{
UrlEncode(i.in.Str(), i.out);
UrlEncode(i.in.get_buffer(), i.out.get_buffer(), false);
}
void fil_qencode(Info & i)
{
QEncode(i.in.Str(), qencode_tmp);
std::wstring tmp_str;
std::string qencode_tmp;
i.in.to_str(tmp_str);
QEncode(tmp_str, qencode_tmp);
i.out << R(qencode_tmp);
}
void fil_capitalize(Info & i)
{
const std::wstring & str = i.in.Str();
for(size_t a=0 ; a<str.size() ; ++a)
i.out << R(locale.ToCapital(str[a]));
for(size_t a=0 ; a < i.in.size() ; ++a)
i.out << R(locale.ToCapital(i.in.get_wchar(a)));
}
void fil_tosmall(Info & i)
{
const std::wstring & str = i.in.Str();
for(size_t a=0 ; a<str.size() ; ++a)
i.out << R(locale.ToSmall(str[a]));
for(size_t a=0 ; a < i.in.size() ; ++a)
i.out << R(locale.ToSmall(i.in.get_wchar(a)));
}
@ -80,23 +79,24 @@ void fil_tosmall(Info & i)
void fil_firstup(Info & i)
{
bool was_dot = true;
const std::wstring & str = i.in.Str();
for(size_t a=0 ; a<str.size() ; ++a)
for(size_t a=0 ; a < i.in.size() ; ++a)
{
wchar_t c = i.in.get_wchar(a);
if( was_dot )
{
if( str[a]!=' ' && str[a]!='\t' && str[a]!=13 && str[a]!=10 && str[a]!=160 )
if( c!=' ' && c!='\t' && c!=13 && c!=10 && c!=160 )
was_dot = false;
i.out << R(locale.ToCapital(str[a]));
i.out << R(locale.ToCapital(c));
}
else
{
i.out << R(str[a]);
i.out << R(c);
}
if( str[a] == '.' )
if( c == '.' )
was_dot = true;
}
}
@ -106,29 +106,32 @@ void fil_firstup(Info & i)
void fil_first_wordup(Info & i)
{
bool was_white = true;
const std::wstring & str = i.in.Str();
for(size_t a=0 ; a<str.size() ; ++a)
for(size_t a=0 ; a < i.in.size() ; ++a)
{
wchar_t c = i.in.get_wchar(a);
if( was_white )
{
i.out << R(locale.ToCapital(str[a]));
i.out << R(locale.ToCapital(c));
}
else
{
i.out << R(str[a]);
i.out << R(c);
}
was_white = (str[a]==' ' || str[a]=='\t' || str[a]==13 || str[a]==10 || str[a]==160);
was_white = (c==' ' || c=='\t' || c==13 || c==10 || c==160);
}
}
bool fil_csv_has_colon_or_quote(const std::wstring & str)
bool fil_csv_has_colon_or_quote(const pt::WTextStream & str)
{
for(size_t i=0 ; i<str.size() ; ++i)
{
if( str[i] == ',' || str[i] == '"' )
wchar_t c = str.get_wchar(i);
if( c == ',' || c == '"' )
return true;
}
@ -139,25 +142,25 @@ return false;
void fil_csv_escape(Info & i)
{
const std::wstring & str = i.in.Str();
if( fil_csv_has_colon_or_quote(str) )
if( fil_csv_has_colon_or_quote(i.in.get_buffer()) )
{
i.out << R("\"");
for(size_t a=0 ; a<str.size() ; ++a)
for(size_t a=0 ; a < i.in.size() ; ++a)
{
if( str[a] == '"' )
wchar_t c = i.in.get_wchar(a);
if( c == '"' )
i.out << R("\"\"");
else
i.out << R(str[a]);
i.out << R(c);
}
i.out << R("\"");
}
else
{
i.out << R(str);
i.out << i.in;
}
}
@ -165,14 +168,14 @@ void fil_csv_escape(Info & i)
void fil_new_line_to_br(Info & i)
{
const std::wstring & str = i.in.Str();
for(size_t a=0 ; a<str.size() ; ++a)
for(size_t a=0 ; a < i.in.size() ; ++a)
{
if( str[a] == '\n' )
wchar_t c = i.in.get_wchar(a);
if( c == '\n' )
i.out << R("<br>\n");
else
i.out << R(str[a]);
i.out << R(c);
}
}
@ -184,17 +187,17 @@ void fil_new_line_to_br(Info & i)
*/
void fil_html_quote(Info & i)
{
const std::wstring & str = i.in.Str();
for(size_t a=0 ; a<str.size() ; ++a)
for(size_t a=0 ; a < i.in.size() ; ++a)
{
if( str[a] == '\"' )
wchar_t c = i.in.get_wchar(a);
if( c == '\"' )
i.out << R("&quot;");
else
if( str[a] == '\'' )
if( c == '\'' )
i.out << R("&#39;");
else
i.out << R(str[a]);
i.out << R(c);
}
}
@ -205,17 +208,17 @@ void fil_html_quote(Info & i)
*/
void fil_html_newline(Info & i)
{
const std::wstring & str = i.in.Str();
for(size_t a=0 ; a<str.size() ; ++a)
for(size_t a=0 ; a < i.in.size() ; ++a)
{
if( str[a] == 10 )
wchar_t c = i.in.get_wchar(a);
if( c == 10 )
i.out << R("&#10;");
else
if( str[a] == 13 )
if( c == 13 )
i.out << R("&#13;");
else
i.out << R(str[a]);
i.out << R(c);
}
}

@ -45,100 +45,209 @@ HtmlTextStream::HtmlTextStream()
}
void HtmlTextStream::Clear()
bool HtmlTextStream::is_char_stream() const
{
escape = true;
TextStream<std::wstring>::Clear();
return false;
}
bool HtmlTextStream::is_wchar_stream() const
{
return true;
}
void HtmlTextStream::clear()
{
Clear();
escape = true;
buffer.clear();
}
bool HtmlTextStream::empty() const
{
return buffer.empty();
}
size_t HtmlTextStream::size() const
{
return buffer.size();
}
void HtmlTextStream::to_str(std::wstring & str)
void HtmlTextStream::reserve(size_t len)
{
str = TextStream<std::wstring>::Str();
buffer.reserve(len);
}
size_t HtmlTextStream::capacity() const
{
return buffer.capacity();
}
HtmlTextStream::iterator HtmlTextStream::begin()
{
return buffer.begin();
}
HtmlTextStream::iterator HtmlTextStream::end()
{
return buffer.end();
}
HtmlTextStream::const_iterator HtmlTextStream::begin() const
{
return buffer.begin();
}
HtmlTextStream::const_iterator HtmlTextStream::end() const
{
return buffer.end();
}
void HtmlTextStream::to_str(std::string & str, bool clear_string) const
{
buffer.to_str(str, clear_string);
}
void HtmlTextStream::to_str(std::wstring & str, bool clear_string) const
{
buffer.to_str(str, clear_string);
}
std::string HtmlTextStream::to_str() const
{
return buffer.to_str();
}
std::wstring HtmlTextStream::to_wstr() const
{
return buffer.to_wstr();
}
char HtmlTextStream::get_char(size_t index) const
{
return buffer.get_char(index);
}
wchar_t HtmlTextStream::get_wchar(size_t index) const
{
return buffer.get_wchar(index);
}
const pt::WTextStream & HtmlTextStream::get_buffer() const
{
return buffer;
}
pt::WTextStream & HtmlTextStream::get_buffer()
{
return buffer;
}
/*
without escaping
*/
HtmlTextStream & HtmlTextStream::PutChar(char c)
HtmlTextStream & HtmlTextStream::PutText(const char * str)
{
TextStream<std::wstring>::operator<<(c);
buffer.operator<<(str);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(wchar_t c)
HtmlTextStream & HtmlTextStream::PutText(const std::string & str)
{
TextStream<std::wstring>::operator<<(c);
buffer.operator<<(str);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const char * str)
HtmlTextStream & HtmlTextStream::PutText(const wchar_t * str)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(str);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const char * str, size_t len)
HtmlTextStream & HtmlTextStream::PutText(const std::wstring & str)
{
TextStream<std::wstring>::Write(str, len);
buffer.operator<<(str);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const std::string * str)
HtmlTextStream & HtmlTextStream::PutText(const char * str, size_t len)
{
TextStream<std::wstring>::operator<<(str);
buffer.write(str, len);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const std::string & str)
HtmlTextStream & HtmlTextStream::PutText(const wchar_t * str, size_t len)
{
TextStream<std::wstring>::operator<<(str);
buffer.write(str, len);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const wchar_t * str)
HtmlTextStream & HtmlTextStream::PutChar(char c)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const std::wstring * str)
HtmlTextStream & HtmlTextStream::PutChar(unsigned char c)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const std::wstring & str)
HtmlTextStream & HtmlTextStream::PutChar(wchar_t c)
{
buffer.operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(bool val)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(val);
return *this;
}
@ -149,252 +258,253 @@ return *this;
HtmlTextStream & HtmlTextStream::operator<<(const RawText<const char*> & raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(const RawText<const wchar_t*> & raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<std::string> raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<const std::string*> raw)
HtmlTextStream & HtmlTextStream::operator<<(const RawText<const wchar_t*> & raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<const std::wstring*> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<std::wstring> raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<std::string> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<char> raw)
{
return PutText(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<std::wstring> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned char> raw)
{
return PutText(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<wchar_t> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<char> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<bool> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<wchar_t> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<short> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<int> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<long> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned int> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<long long> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned long> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned short> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<double> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned int> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<void*> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned long> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Space> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned long long> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Date> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<float> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const HtmlTextStream & stream)
HtmlTextStream & HtmlTextStream::operator<<(RawText<double> raw)
{
TextStream<std::wstring>::operator<<(stream.Str());
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::Write(const char * buf, size_t len)
HtmlTextStream & HtmlTextStream::operator<<(RawText<long double> raw)
{
TextStream<std::wstring>::Write(buf, len);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::Write(const wchar_t * buf, size_t len)
HtmlTextStream & HtmlTextStream::operator<<(RawText<void*> raw)
{
TextStream<std::wstring>::Write(buf, len);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::write(const char * buf, size_t len)
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Stream> raw)
{
TextStream<std::wstring>::write(buf, len);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::write(const wchar_t * buf, size_t len)
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Space> raw)
{
TextStream<std::wstring>::write(buf, len);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Date> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const HtmlTextStream & stream)
{
buffer.operator<<(stream.buffer);
return *this;
}
/*
with escaping
*/
void HtmlTextStream::Escape(bool escape_characters)
HtmlTextStream & HtmlTextStream::write(const char * buf, size_t len)
{
escape = escape_characters;
buffer.write(buf, len);
return *this;
}
HtmlTextStream & HtmlTextStream::ETextPutChar(char c)
HtmlTextStream & HtmlTextStream::write(const wchar_t * buf, size_t len)
{
return ETextPutChar(static_cast<wchar_t>(c));
buffer.write(buf, len);
return *this;
}
HtmlTextStream & HtmlTextStream::ETextPutChar(wchar_t c)
{
if( c == '<' )
buffer += L"&lt;";
else
if( c == '>' )
buffer += L"&gt;";
else
if( c == '&' )
buffer += L"&amp;";
else
if( c == '\"' )
buffer += L"&quot;";
else
if( c == '\'' )
buffer += L"&#39;"; // (it is "&apos;" but IE8 has a problem with &apos;)
else
if( c == 10 )
buffer += L"&#10;";
else
if( c == 13 )
buffer += L"&#13;";
else
if( c != 0 ) // !! CHECK ME may it should be changed to something like '&#0';
buffer += c;
return *this;
}
/*
with escaping
*/
HtmlTextStream & HtmlTextStream::EPutText(const char * str)
{
pt::utf8_to_wide(str, tmp_string);
int res;
bool correct;
size_t utf8_len;
for(size_t i=0 ; i<tmp_string.size() ; ++i)
ETextPutChar(tmp_string[i]);
// CHECKME
while( (utf8_len = pt::utf8_to_int(str, res, correct)) > 0 )
{
if( !correct )
res = 0xFFFD; // U+FFFD "replacement character"
tmp_string.clear();
ETextPutChar(static_cast<wchar_t>(res));
str += utf8_len;
}
return *this;
}
HtmlTextStream & HtmlTextStream::EPutText(const char * str, size_t len)
HtmlTextStream & HtmlTextStream::EPutText(const std::string & str)
{
pt::utf8_to_wide(str, len, tmp_string);
return EPutText(str.c_str(), str.size());
}
for(size_t i=0 ; i<tmp_string.size() ; ++i)
ETextPutChar(tmp_string[i]);
tmp_string.clear();
HtmlTextStream & HtmlTextStream::EPutText(const wchar_t * str)
{
for( ; *str ; ++str )
ETextPutChar(*str);
return *this;
}
HtmlTextStream & HtmlTextStream::EPutText(const std::string * str)
HtmlTextStream & HtmlTextStream::EPutText(const std::wstring & str)
{
return EPutText(*str);
return EPutText(str.c_str(), str.size());
}
HtmlTextStream & HtmlTextStream::EPutText(const std::string & str)
HtmlTextStream & HtmlTextStream::EPutText(const char * str, size_t len)
{
pt::utf8_to_wide(str, tmp_string);
for(size_t i=0 ; i<tmp_string.size() ; ++i)
ETextPutChar(tmp_string[i]);
int res;
bool correct;
size_t utf8_len;
tmp_string.clear();
return *this;
}
// CHECKME
while( (utf8_len = pt::utf8_to_int(str, len, res, correct)) > 0 )
{
if( !correct )
res = 0xFFFD; // U+FFFD "replacement character"
ETextPutChar(static_cast<wchar_t>(res));
HtmlTextStream & HtmlTextStream::EPutText(const wchar_t * str)
{
for( ; *str ; ++str )
ETextPutChar(*str);
len -= utf8_len;
str += utf8_len;
}
return *this;
}
@ -409,32 +519,61 @@ return *this;
}
HtmlTextStream & HtmlTextStream::EPutText(const std::wstring * str)
HtmlTextStream & HtmlTextStream::ETextPutChar(char c)
{
return EPutText(str->c_str(), str->size());
return ETextPutChar(static_cast<unsigned char>(c));
}
HtmlTextStream & HtmlTextStream::EPutText(const std::wstring & str)
HtmlTextStream & HtmlTextStream::ETextPutChar(unsigned char c)
{
return EPutText(str.c_str(), str.size());
return ETextPutChar(static_cast<wchar_t>(c));
}
HtmlTextStream & HtmlTextStream::operator<<(const char * str)
HtmlTextStream & HtmlTextStream::ETextPutChar(wchar_t c)
{
if( escape )
EPutText(str);
if( c == '<' )
buffer << L"&lt;";
else
PutText(str);
if( c == '>' )
buffer << L"&gt;";
else
if( c == '&' )
buffer << L"&amp;";
else
if( c == '\"' )
buffer << L"&quot;";
else
if( c == '\'' )
buffer << L"&#39;"; // (it is "&apos;" but IE8 has a problem with &apos;) (&apos; is valid in HTML5, but not HTML4)
else
if( c == 10 )
buffer << L"&#10;";
else
if( c == 13 )
buffer << L"&#13;";
else
if( c == 0 )
buffer << L"&#0;";
else
buffer << c;
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const std::string * str)
void HtmlTextStream::Escape(bool escape_characters)
{
escape = escape_characters;
}
HtmlTextStream & HtmlTextStream::operator<<(const char * str)
{
if( escape )
EPutText(str);
@ -456,8 +595,6 @@ return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const wchar_t * str)
{
if( escape )
@ -469,7 +606,7 @@ return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const std::wstring * str)
HtmlTextStream & HtmlTextStream::operator<<(const std::wstring & str)
{
if( escape )
EPutText(str);
@ -480,20 +617,18 @@ return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const std::wstring & str)
HtmlTextStream & HtmlTextStream::operator<<(char v)
{
if( escape )
EPutText(str);
ETextPutChar(v);
else
PutText(str);
PutChar(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(char v)
HtmlTextStream & HtmlTextStream::operator<<(unsigned char v)
{
if( escape )
ETextPutChar(v);
@ -515,14 +650,31 @@ return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(int v)
HtmlTextStream & HtmlTextStream::operator<<(bool v)
{
/*
* int, long and others don't have to be escaped
* (they consist of digits only: '0' - '9' and other characters which
* don't have to be escaped)
* bool, short, int, long, long long, float, double and long double don't have to be escaped
* they consist of digits only: '0' - '9' (and with an exponent in the case of float/double/long double)
* and don't have to be escaped
*
*/
TextStream<std::wstring>::operator<<(v);
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(short v)
{
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(int v)
{
buffer.operator<<(v);
return *this;
}
@ -530,7 +682,23 @@ return *this;
HtmlTextStream & HtmlTextStream::operator<<(long v)
{
TextStream<std::wstring>::operator<<(v);
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(long long v)
{
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(unsigned short v)
{
buffer.operator<<(v);
return *this;
}
@ -538,7 +706,7 @@ return *this;
HtmlTextStream & HtmlTextStream::operator<<(unsigned int v)
{
TextStream<std::wstring>::operator<<(v)