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

This commit is contained in:
2021-07-16 18:17:57 +02:00
parent ba6159964b
commit c5c02d7f44
14 changed files with 764 additions and 375 deletions

View File

@@ -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);
}
}

View File

@@ -45,67 +45,134 @@ 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();
}
void HtmlTextStream::to_str(std::wstring & str)
bool HtmlTextStream::empty() const
{
str = TextStream<std::wstring>::Str();
return buffer.empty();
}
size_t HtmlTextStream::size() const
{
return buffer.size();
}
void HtmlTextStream::reserve(size_t len)
{
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)
{
TextStream<std::wstring>::operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(wchar_t c)
{
TextStream<std::wstring>::operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const char * str)
{
TextStream<std::wstring>::operator<<(str);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const char * str, size_t len)
{
TextStream<std::wstring>::Write(str, len);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const std::string * str)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(str);
return *this;
}
@@ -113,7 +180,7 @@ return *this;
HtmlTextStream & HtmlTextStream::PutText(const std::string & str)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(str);
return *this;
}
@@ -122,15 +189,7 @@ return *this;
HtmlTextStream & HtmlTextStream::PutText(const wchar_t * str)
{
TextStream<std::wstring>::operator<<(str);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const std::wstring * str)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(str);
return *this;
}
@@ -138,12 +197,64 @@ return *this;
HtmlTextStream & HtmlTextStream::PutText(const std::wstring & str)
{
TextStream<std::wstring>::operator<<(str);
buffer.operator<<(str);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const char * str, size_t len)
{
buffer.write(str, len);
return *this;
}
HtmlTextStream & HtmlTextStream::PutText(const wchar_t * str, size_t len)
{
buffer.write(str, len);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(char c)
{
buffer.operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(unsigned char c)
{
buffer.operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(wchar_t c)
{
buffer.operator<<(c);
return *this;
}
HtmlTextStream & HtmlTextStream::PutChar(bool val)
{
buffer.operator<<(val);
return *this;
}
@@ -155,33 +266,18 @@ HtmlTextStream & HtmlTextStream::operator<<(const RawText<const char*> & raw)
}
HtmlTextStream & HtmlTextStream::operator<<(const RawText<const wchar_t*> & raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<const std::string*> raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<const std::wstring*> raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<std::string> raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(const RawText<const wchar_t*> & raw)
{
return PutText(raw.par);
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<std::wstring> raw)
{
return PutText(raw.par);
@@ -189,107 +285,158 @@ HtmlTextStream & HtmlTextStream::operator<<(RawText<std::wstring> raw)
HtmlTextStream & HtmlTextStream::operator<<(RawText<char> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned char> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<wchar_t> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<bool> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<short> raw)
{
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<long long> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned short> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned int> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned long> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<unsigned long long> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<float> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<double> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<long double> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<void*> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Stream> raw)
{
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Space> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::Date> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const HtmlTextStream & stream)
{
TextStream<std::wstring>::operator<<(stream.Str());
buffer.operator<<(stream.buffer);
return *this;
}
HtmlTextStream & HtmlTextStream::Write(const char * buf, size_t len)
{
TextStream<std::wstring>::Write(buf, len);
return *this;
}
HtmlTextStream & HtmlTextStream::Write(const wchar_t * buf, size_t len)
{
TextStream<std::wstring>::Write(buf, len);
return *this;
}
HtmlTextStream & HtmlTextStream::write(const char * buf, size_t len)
{
TextStream<std::wstring>::write(buf, len);
buffer.write(buf, len);
return *this;
}
HtmlTextStream & HtmlTextStream::write(const wchar_t * buf, size_t len)
{
TextStream<std::wstring>::write(buf, len);
buffer.write(buf, len);
return *this;
}
@@ -300,94 +447,29 @@ HtmlTextStream & HtmlTextStream::write(const wchar_t * buf, size_t len)
/*
with escaping
*/
void HtmlTextStream::Escape(bool escape_characters)
{
escape = escape_characters;
}
HtmlTextStream & HtmlTextStream::ETextPutChar(char c)
{
return ETextPutChar(static_cast<wchar_t>(c));
}
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;
}
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)
{
pt::utf8_to_wide(str, len, tmp_string);
for(size_t i=0 ; i<tmp_string.size() ; ++i)
ETextPutChar(tmp_string[i]);
tmp_string.clear();
return *this;
}
HtmlTextStream & HtmlTextStream::EPutText(const std::string * str)
{
return EPutText(*str);
}
HtmlTextStream & HtmlTextStream::EPutText(const std::string & str)
{
pt::utf8_to_wide(str, tmp_string);
for(size_t i=0 ; i<tmp_string.size() ; ++i)
ETextPutChar(tmp_string[i]);
tmp_string.clear();
return *this;
return EPutText(str.c_str(), str.size());
}
@@ -400,6 +482,34 @@ return *this;
}
HtmlTextStream & HtmlTextStream::EPutText(const std::wstring & str)
{
return EPutText(str.c_str(), str.size());
}
HtmlTextStream & HtmlTextStream::EPutText(const char * str, size_t len)
{
int res;
bool correct;
size_t utf8_len;
// 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));
len -= utf8_len;
str += utf8_len;
}
return *this;
}
HtmlTextStream & HtmlTextStream::EPutText(const wchar_t * str, size_t len)
{
for(size_t i=0 ; i<len ; ++i)
@@ -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,17 +606,6 @@ return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const std::wstring * str)
{
if( escape )
EPutText(str);
else
PutText(str);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const std::wstring & str)
{
if( escape )
@@ -491,8 +617,6 @@ return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(char v)
{
if( escape )
@@ -504,6 +628,17 @@ return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(unsigned char v)
{
if( escape )
ETextPutChar(v);
else
PutChar(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(wchar_t v)
{
if( escape )
@@ -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);
buffer.operator<<(v);
return *this;
}
@@ -546,7 +714,23 @@ return *this;
HtmlTextStream & HtmlTextStream::operator<<(unsigned long v)
{
TextStream<std::wstring>::operator<<(v);
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(unsigned long long v)
{
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(float v)
{
buffer.operator<<(v);
return *this;
}
@@ -554,7 +738,15 @@ return *this;
HtmlTextStream & HtmlTextStream::operator<<(double v)
{
TextStream<std::wstring>::operator<<(v);
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(long double v)
{
buffer.operator<<(v);
return *this;
}
@@ -562,7 +754,46 @@ return *this;
HtmlTextStream & HtmlTextStream::operator<<(const void * v)
{
TextStream<std::wstring>::operator<<(v);
buffer.operator<<(v);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(const Stream & stream)
{
if( escape )
{
if(stream.is_char_stream())
{
int res;
bool correct;
size_t len;
size_t index = 0;
size_t stream_len = stream.size();
// CHECKME
while( index < stream_len && (len = pt::utf8_to_int(stream, index, res, correct)) > 0 )
{
if( !correct )
res = 0xFFFD; // U+FFFD "replacement character"
ETextPutChar(static_cast<wchar_t>(res));
index += len;
}
}
else
if(stream.is_wchar_stream())
{
for(size_t i=0 ; i < stream.size() ; ++i)
ETextPutChar(stream.get_wchar(i));
}
}
else
{
buffer.operator<<(stream);
}
return *this;
}
@@ -573,20 +804,20 @@ HtmlTextStream & HtmlTextStream::operator<<(const pt::Space & space)
{
if( escape )
{
space.serialize_to_space_stream(*this, true);
space.serialize_to_json_stream(*this, true);
/*
tmp_stream.Clear();
// !! IMPROVE ME
// we can calculate how many memory is needed beforehand
space.Serialize(tmp_stream, true, false);
operator<<(tmp_stream.Str());
tmp_stream.Clear();
*/
pt::WTextStream tmp_stream;
space.serialize_to_json_stream(tmp_stream, true);
pt::WTextStream::iterator i = tmp_stream.begin();
for( ; i != tmp_stream.end() ; ++i)
ETextPutChar(*i);
}
else
{
TextStream<std::wstring>::operator<<(space);
// FIXME this will serialize to Space format !!!!
buffer.operator<<(space);
}
return *this;
@@ -595,28 +826,24 @@ return *this;
HtmlTextStream & HtmlTextStream::operator<<(const pt::Date & date)
{
if( escape )
{
date.Serialize(*this);
/*
tmp_stream.Clear();
// !! IMPROVE ME
// we can calculate how many memory is needed beforehand
date.Serialize(tmp_stream);
operator<<(tmp_stream.Str());
tmp_stream.Clear();
*/
}
else
{
TextStream<std::wstring>::operator<<(date);
}
// dates don't need to be escaped
buffer.operator<<(date);
return *this;
}
HtmlTextStream & HtmlTextStream::operator<<(morm::Model & model)
{
// CHECKME
pt::TextStream tmp_stream;
model.to_text(tmp_stream);
return operator<<(tmp_stream);
}
} // namespace Winix

View File

@@ -36,7 +36,9 @@
#define headerfile_winix_templates_htmltextstream
#include <ctime>
#include "core/textstream.h"
#include "textstream/textstream.h"
#include "morm.h"
namespace Winix
{
@@ -69,25 +71,54 @@ namespace Winix
> -> &gt;
& -> &nbsp;
" -> &quot;
' -> &#39; (it is "&apos;" but IE8 has a problem with &apos;)
' -> &#39; (it is "&apos;" but IE8 has a problem with &apos;) (&apos; is valid in HTML5, but not HTML4)
10 -> &#10;
13 -> &#13;
0 -> &#0;
*/
class HtmlTextStream : public TextStream<std::wstring>
class HtmlTextStream : public pt::Stream
{
public:
typedef wchar_t char_type;
typedef pt::WTextStream::buffer_type buffer_type;
typedef typename buffer_type::iterator iterator;
typedef typename buffer_type::const_iterator const_iterator;
HtmlTextStream();
bool is_char_stream() const;
bool is_wchar_stream() const;
/*
* clearing the buffer and setting 'escape' flag to true
*/
void Clear();
void clear(); // utf8 methods call clear(), in the future Clear() will be renamed to clear()
void clear();
bool empty() const;
size_t size() const;
void reserve(size_t len);
size_t capacity() const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
void to_str(std::string & str, bool clear_string = true) const;
void to_str(std::wstring & str, bool clear_string = true) const;
std::string to_str() const;
std::wstring to_wstr() const;
char get_char(size_t index) const;
wchar_t get_wchar(size_t index) const;
const pt::WTextStream & get_buffer() const;
pt::WTextStream & get_buffer();
void to_str(std::wstring & str);
/*
a helper struct to select a proper operator<<
@@ -107,18 +138,20 @@ public:
/*
without escaping
*/
HtmlTextStream & PutChar(char);
HtmlTextStream & PutChar(wchar_t);
HtmlTextStream & PutText(const char *);
HtmlTextStream & PutText(const char *, size_t len);
HtmlTextStream & PutText(const std::string *);
HtmlTextStream & PutText(const std::string &);
HtmlTextStream & PutText(const char * str);
HtmlTextStream & PutText(const std::string & str);
HtmlTextStream & PutText(const wchar_t * str);
HtmlTextStream & PutText(const wchar_t * str, size_t len);
HtmlTextStream & PutText(const std::wstring * str);
HtmlTextStream & PutText(const std::wstring & str);
HtmlTextStream & PutText(const char *, size_t len);
HtmlTextStream & PutText(const wchar_t * str, size_t len);
HtmlTextStream & PutChar(char c);
HtmlTextStream & PutChar(unsigned char c);
HtmlTextStream & PutChar(wchar_t c);
HtmlTextStream & PutChar(bool val);
/*
we need this template operator for such calling:
HtmlTextStream_object << R("some string");
@@ -131,56 +164,64 @@ public:
HtmlTextStream & operator<<(const RawText<wchar_t [str_size]> & raw) { return PutText(raw.par); }
HtmlTextStream & operator<<(const RawText<const char*> & raw);
HtmlTextStream & operator<<(const RawText<const wchar_t*> & raw);
HtmlTextStream & operator<<(RawText<const std::string*> raw);
HtmlTextStream & operator<<(RawText<const std::wstring*> raw);
HtmlTextStream & operator<<(RawText<std::string> raw);
HtmlTextStream & operator<<(const RawText<const wchar_t*> & raw);
HtmlTextStream & operator<<(RawText<std::wstring> raw);
HtmlTextStream & operator<<(RawText<char> raw);
HtmlTextStream & operator<<(RawText<unsigned char> raw);
HtmlTextStream & operator<<(RawText<wchar_t> raw);
HtmlTextStream & operator<<(RawText<bool> raw);
HtmlTextStream & operator<<(RawText<short> raw);
HtmlTextStream & operator<<(RawText<int> raw);
HtmlTextStream & operator<<(RawText<long> raw);
HtmlTextStream & operator<<(RawText<long long> raw);
HtmlTextStream & operator<<(RawText<unsigned short> raw);
HtmlTextStream & operator<<(RawText<unsigned int> raw);
HtmlTextStream & operator<<(RawText<unsigned long> raw);
HtmlTextStream & operator<<(RawText<unsigned long long> raw);
HtmlTextStream & operator<<(RawText<float> raw);
HtmlTextStream & operator<<(RawText<double> raw);
HtmlTextStream & operator<<(RawText<long double> raw);
HtmlTextStream & operator<<(RawText<void*> raw);
HtmlTextStream & operator<<(RawText<pt::Stream> raw);
HtmlTextStream & operator<<(RawText<pt::Space> raw);
HtmlTextStream & operator<<(RawText<pt::Date> raw);
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
HtmlTextStream & operator<<(RawText<pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> > raw);
// this method doesn't escape stream as there is no need for such behavior - stream should be already escaped
HtmlTextStream & operator<<(const HtmlTextStream & stream);
// 'write' don't escapes too
// with these methods you can write a zero character too
HtmlTextStream & Write(const char * buf, size_t len);
HtmlTextStream & Write(const wchar_t * buf, size_t len);
// for compatibility with standard library (Ezc uses it)
HtmlTextStream & write(const char * buf, size_t len);
HtmlTextStream & write(const wchar_t * buf, size_t len);
/*
with escaping
*/
HtmlTextStream & ETextPutChar(char c);
HtmlTextStream & ETextPutChar(wchar_t c);
HtmlTextStream & EPutText(const char * str);
HtmlTextStream & EPutText(const char * str, size_t len);
HtmlTextStream & EPutText(const std::string * str);
HtmlTextStream & EPutText(const std::string & str);
HtmlTextStream & EPutText(const wchar_t * str);
HtmlTextStream & EPutText(const wchar_t * str, size_t len);
HtmlTextStream & EPutText(const std::wstring * str);
HtmlTextStream & EPutText(const std::wstring & str);
HtmlTextStream & EPutText(const char * str, size_t len);
HtmlTextStream & EPutText(const wchar_t * str, size_t len);
HtmlTextStream & ETextPutChar(char c);
HtmlTextStream & ETextPutChar(unsigned char c);
HtmlTextStream & ETextPutChar(wchar_t c);
/*
* by default all operator<< shown below use escaping
* but you can turn it off by calling Escape(false)
@@ -188,30 +229,40 @@ public:
void Escape(bool escape_characters);
HtmlTextStream & operator<<(const char * str);
HtmlTextStream & operator<<(const std::string * str);
HtmlTextStream & operator<<(const std::string & str);
HtmlTextStream & operator<<(const wchar_t * str);
HtmlTextStream & operator<<(const std::wstring * str);
HtmlTextStream & operator<<(const std::wstring & str);
HtmlTextStream & operator<<(char);
HtmlTextStream & operator<<(unsigned char);
HtmlTextStream & operator<<(wchar_t);
HtmlTextStream & operator<<(bool);
HtmlTextStream & operator<<(short);
HtmlTextStream & operator<<(int);
HtmlTextStream & operator<<(long);
HtmlTextStream & operator<<(long long);
HtmlTextStream & operator<<(unsigned short);
HtmlTextStream & operator<<(unsigned int);
HtmlTextStream & operator<<(unsigned long);
HtmlTextStream & operator<<(unsigned long long);
HtmlTextStream & operator<<(float);
HtmlTextStream & operator<<(double);
HtmlTextStream & operator<<(long double);
HtmlTextStream & operator<<(const void *);
HtmlTextStream & operator<<(const Stream & stream);
HtmlTextStream & operator<<(const pt::Space & space);
HtmlTextStream & operator<<(const pt::Date & Date);
HtmlTextStream & operator<<(morm::Model & model);
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
HtmlTextStream & operator<<(const pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
private:
//TextStream<std::wstring> tmp_stream;
std::wstring tmp_string;
pt::WTextStream buffer;
//std::wstring tmp_string;
bool escape;
};
@@ -221,12 +272,17 @@ private:
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
HtmlTextStream & HtmlTextStream::operator<<(RawText<pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> > raw)
{
TextStream<std::wstring>::operator<<(raw.par);
buffer.operator<<(raw.par);
return *this;
}
/*
* this method is the same as HtmlTextStream & HtmlTextStream::operator<<(const Stream & stream)
* but in the future we can use iterators here
*
*/
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
HtmlTextStream & HtmlTextStream::operator<<(const pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg)
{
@@ -234,15 +290,38 @@ typename pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size>:
if( escape )
{
/*
* warning: char* (utf-8) string will not be correctly handled here
*/
for(i=arg.begin() ; i != arg.end() ; ++i)
ETextPutChar(*i);
if(arg.is_char_stream())
{
/*
* IMPROVEME it would be better to have a better api from pikotools
* instead of index we can provide an iterator
*/
int res;
bool correct;
size_t len;
size_t index = 0;
size_t stream_len = arg.size();
// CHECKME
while( index < stream_len && (len = pt::utf8_to_int(arg, index, res, correct)) > 0 )
{
if( !correct )
res = 0xFFFD; // U+FFFD "replacement character"
ETextPutChar(static_cast<wchar_t>(res));
index += len;
}
}
else
if(arg.is_wchar_stream())
{
for(i=arg.begin() ; i != arg.end() ; ++i)
ETextPutChar(*i);
}
}
else
{
TextStream<std::wstring>::operator<<(arg);
buffer.operator<<(arg);
}
return *this;

View File

@@ -72,12 +72,12 @@ void insert_page_run(Info & i)
log << log4 << "Templates: insert_page_run: using generator number: " << insert_page_cur << logend;
insert_page_cur += 1;
info.run_content.Clear();
info.run_content.clear();
InitGenerator(info.ezc_gen, cur->request->models);
info.ezc_gen.SetPattern(*pat);
info.ezc_gen.Generate(info.run_content);
ItemContent::print_content(i.out, info.run_content.Str(), info.item.item_content.content_raw_type, config->html_filter);
ItemContent::print_content(i.out, info.run_content.get_buffer(), info.item.item_content.content_raw_type, config->html_filter);
insert_page_cur -= 1;
}