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

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