667 lines
14 KiB
C++
667 lines
14 KiB
C++
/*
|
|
* This file is a part of Winix
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2010-2021, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
*/
|
|
|
|
#ifndef headerfile_winix_core_textstream
|
|
#define headerfile_winix_core_textstream
|
|
|
|
#include <string>
|
|
#include <ctime>
|
|
#include "misc.h"
|
|
#include "space/space.h"
|
|
#include "date/date.h"
|
|
#include "textstream/textstream.h"
|
|
#include "utf8/utf8.h"
|
|
|
|
|
|
namespace Winix
|
|
{
|
|
|
|
|
|
|
|
|
|
/*
|
|
a special class representing a stream buffer
|
|
similar to std::ostringstream
|
|
|
|
StringType can be either std::string or std::wstring
|
|
|
|
this class uses UTF-8 <-> wide characters conversions:
|
|
if StringType is std::string:
|
|
operator<<(const char*) only copies the input string
|
|
operator<<(const wchar_t*) converts from wide characters to UTF-8
|
|
(similary for an operator with std::string and std::wstring)
|
|
if StringType is std::wstring:
|
|
operator<<(const char*) converts from UTF-8 to wide characters
|
|
operator<<(const wchar_t*) only copies the input string
|
|
(similary for an operator with std::string and std::wstring)
|
|
|
|
*/
|
|
template<class StringType>
|
|
class TextStream
|
|
{
|
|
public:
|
|
|
|
typedef typename StringType::value_type CharType;
|
|
typedef typename StringType::value_type char_type;
|
|
typedef typename StringType::iterator iterator;
|
|
typedef typename StringType::const_iterator const_iterator;
|
|
|
|
void Clear();
|
|
void clear(); // utf8 methods call clear(), in the future Clear() will be renamed to clear()
|
|
bool Empty() const;
|
|
size_t Size() const;
|
|
void Reserve(size_t len);
|
|
|
|
iterator begin();
|
|
iterator end();
|
|
const_iterator begin() const;
|
|
const_iterator end() const;
|
|
|
|
const StringType & Str() const;
|
|
const CharType * CStr() const;
|
|
|
|
void Str(const StringType & str);
|
|
void Str(const StringType && str);
|
|
|
|
void to_str(std::string & str, bool clear_string = true) const;
|
|
void to_str(std::wstring & str, bool clear_string = true) const;
|
|
void to_string(std::string & str, bool clear_string = true) const;
|
|
void to_string(std::wstring & str, bool clear_string = true) const;
|
|
|
|
CharType operator[](size_t index);
|
|
|
|
TextStream & operator<<(const char * str);
|
|
TextStream & operator<<(const std::string * str);
|
|
TextStream & operator<<(const std::string & str);
|
|
|
|
TextStream & operator<<(const wchar_t * str);
|
|
TextStream & operator<<(const std::wstring * str);
|
|
TextStream & operator<<(const std::wstring & str);
|
|
|
|
TextStream & operator<<(char);
|
|
TextStream & operator<<(wchar_t);
|
|
TextStream & operator<<(int);
|
|
TextStream & operator<<(long);
|
|
TextStream & operator<<(unsigned int);
|
|
TextStream & operator<<(unsigned long);
|
|
TextStream & operator<<(double);
|
|
TextStream & operator<<(const void *);// printing a pointer
|
|
TextStream & operator<<(const pt::Space & space);
|
|
TextStream & operator<<(const pt::Date & date);
|
|
|
|
TextStream & operator<<(const TextStream & stream);
|
|
|
|
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
|
TextStream & operator<<(const pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
|
|
|
|
TextStream & Write(const char * buf, size_t len);
|
|
TextStream & Write(const wchar_t * buf, size_t len);
|
|
TextStream & write(const char * buf, size_t len); // for compatibility with standard library (Ezc uses it)
|
|
TextStream & write(const wchar_t * buf, size_t len);
|
|
|
|
|
|
protected:
|
|
|
|
StringType buffer;
|
|
|
|
void Convert(wchar_t c, std::string & dst);
|
|
void Convert(wchar_t c, std::wstring & dst);
|
|
|
|
void Convert(const char * src, size_t len, std::wstring & dst);
|
|
void Convert(const char * src, std::wstring & dst);
|
|
void Convert(const std::string & src, std::wstring & dst);
|
|
|
|
void Convert(const wchar_t * src, size_t len, std::string & dst);
|
|
void Convert(const wchar_t * src, std::string & dst);
|
|
void Convert(const std::wstring & src, std::string & dst);
|
|
|
|
void Convert(const char * src, size_t len, std::string & dst);
|
|
void Convert(const char * src, std::string & dst);
|
|
void Convert(const std::string & src, std::string & dst);
|
|
|
|
void Convert(const wchar_t * src, size_t len, std::wstring & dst);
|
|
void Convert(const wchar_t * src, std::wstring & dst);
|
|
void Convert(const std::wstring & src, std::wstring & dst);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Clear()
|
|
{
|
|
buffer.clear();
|
|
}
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::clear()
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
template<class StringType>
|
|
bool TextStream<StringType>::Empty() const
|
|
{
|
|
return buffer.empty();
|
|
}
|
|
|
|
template<class StringType>
|
|
size_t TextStream<StringType>::Size() const
|
|
{
|
|
return buffer.size();
|
|
}
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Reserve(size_t len)
|
|
{
|
|
buffer.reserve(len);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
typename TextStream<StringType>::iterator TextStream<StringType>::begin()
|
|
{
|
|
return buffer.begin();
|
|
}
|
|
|
|
template<class StringType>
|
|
typename TextStream<StringType>::iterator TextStream<StringType>::end()
|
|
{
|
|
return buffer.end();
|
|
}
|
|
|
|
template<class StringType>
|
|
typename TextStream<StringType>::const_iterator TextStream<StringType>::begin() const
|
|
{
|
|
return buffer.begin();
|
|
}
|
|
|
|
template<class StringType>
|
|
typename TextStream<StringType>::const_iterator TextStream<StringType>::end() const
|
|
{
|
|
return buffer.end();
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
const StringType & TextStream<StringType>::Str() const
|
|
{
|
|
return buffer;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
const typename TextStream<StringType>::CharType * TextStream<StringType>::CStr() const
|
|
{
|
|
return buffer.c_str();
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Str(const StringType & str)
|
|
{
|
|
buffer = str;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Str(const StringType && str)
|
|
{
|
|
buffer = std::move(str);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::to_str(std::string & str, bool clear_string) const
|
|
{
|
|
return to_string(str, clear_string);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::to_str(std::wstring & str, bool clear_string) const
|
|
{
|
|
return to_string(str, clear_string);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::to_string(std::string & str, bool clear_string) const
|
|
{
|
|
if( clear_string )
|
|
str.clear();
|
|
|
|
if constexpr (sizeof(typename StringType::value_type) == sizeof(char))
|
|
{
|
|
str.append(buffer);
|
|
}
|
|
else
|
|
{
|
|
pt::wide_to_utf8(buffer, str, false);
|
|
}
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::to_string(std::wstring & str, bool clear_string) const
|
|
{
|
|
if( clear_string )
|
|
str.clear();
|
|
|
|
if constexpr (sizeof(typename StringType::value_type) == sizeof(wchar_t))
|
|
{
|
|
str.append(buffer);
|
|
}
|
|
else
|
|
{
|
|
pt::utf8_to_wide(buffer, str, false);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
typename TextStream<StringType>::CharType TextStream<StringType>::operator[](size_t index)
|
|
{
|
|
return buffer[index];
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const char * str)
|
|
{
|
|
Convert(str, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::string * str)
|
|
{
|
|
Convert(*str, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::string & str)
|
|
{
|
|
Convert(str, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const wchar_t * str)
|
|
{
|
|
Convert(str, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::wstring * str)
|
|
{
|
|
Convert(*str, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::wstring & str)
|
|
{
|
|
Convert(str, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(char v)
|
|
{
|
|
/*
|
|
* there is no any possibility to treat 'v' as UTF-8 character if we have got
|
|
* only one character so we only copy it
|
|
*/
|
|
buffer += v;
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(wchar_t v)
|
|
{
|
|
Convert(v, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(int v)
|
|
{
|
|
wchar_t buf[50];
|
|
size_t len = sizeof(buf) / sizeof(wchar_t);
|
|
|
|
Toa(v, buf, len);
|
|
Convert(buf, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(long v)
|
|
{
|
|
wchar_t buf[50];
|
|
size_t len = sizeof(buf) / sizeof(wchar_t);
|
|
|
|
Toa(v, buf, len);
|
|
Convert(buf, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(unsigned int v)
|
|
{
|
|
wchar_t buf[50];
|
|
size_t len = sizeof(buf) / sizeof(wchar_t);
|
|
|
|
Toa(v, buf, len);
|
|
Convert(buf, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(unsigned long v)
|
|
{
|
|
wchar_t buf[50];
|
|
size_t len = sizeof(buf) / sizeof(wchar_t);
|
|
|
|
Toa(v, buf, len);
|
|
Convert(buf, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(double v)
|
|
{
|
|
char buf[50];
|
|
|
|
sprintf(buf, "%f", v);
|
|
Convert(buf, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const void * v)
|
|
{
|
|
wchar_t buf[50];
|
|
size_t len = sizeof(buf) / sizeof(wchar_t);
|
|
|
|
buf[0] = '0';
|
|
buf[1] = 'x';
|
|
|
|
Toa(reinterpret_cast<unsigned long>(v), buf+2, len-2, 16);
|
|
Convert(buf, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::Write(const char * buf, size_t len)
|
|
{
|
|
Convert(buf, len, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::write(const char * buf, size_t len)
|
|
{
|
|
return Write(buf, len);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::Write(const wchar_t * buf, size_t len)
|
|
{
|
|
Convert(buf, len, buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::write(const wchar_t * buf, size_t len)
|
|
{
|
|
return Write(buf, len);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const pt::Space & space)
|
|
{
|
|
space.serialize_to_space_stream(*this, true);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const pt::Date & date)
|
|
{
|
|
date.Serialize(*this);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const TextStream<StringType> & stream)
|
|
{
|
|
buffer.append(stream.buffer);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(
|
|
const pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg)
|
|
{
|
|
typename pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size>::const_iterator i;
|
|
|
|
for(i=arg.begin() ; i != arg.end() ; ++i)
|
|
buffer += static_cast<char_type>(*i);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(wchar_t c, std::string & dst)
|
|
{
|
|
pt::int_to_utf8((int)c, dst, false);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(wchar_t c, std::wstring & dst)
|
|
{
|
|
dst += c;
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const char * src, size_t len, std::wstring & dst)
|
|
{
|
|
pt::utf8_to_wide(src, len, dst, false);
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const char * src, std::wstring & dst)
|
|
{
|
|
pt::utf8_to_wide(src, dst, false);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const std::string & src, std::wstring & dst)
|
|
{
|
|
pt::utf8_to_wide(src, dst, false);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const wchar_t * src, size_t len, std::string & dst)
|
|
{
|
|
pt::wide_to_utf8(src, len, dst, false);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const wchar_t * src, std::string & dst)
|
|
{
|
|
pt::wide_to_utf8(src, dst, false);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const std::wstring & src, std::string & dst)
|
|
{
|
|
pt::wide_to_utf8(src, dst, false);
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const char * src, size_t len, std::string & dst)
|
|
{
|
|
// we suppose that append is smart enough and we don't have to use reserve()
|
|
dst.append(src, len);
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const char * src, std::string & dst)
|
|
{
|
|
size_t len;
|
|
|
|
for(len=0 ; src[len] ; ++len){}
|
|
|
|
Convert(src, len, dst);
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const std::string & src, std::string & dst)
|
|
{
|
|
dst.append(src);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const wchar_t * src, size_t len, std::wstring & dst)
|
|
{
|
|
// we suppose that append is smart enough and we don't have to use reserve()
|
|
dst.append(src, len);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const wchar_t * src, std::wstring & dst)
|
|
{
|
|
size_t len;
|
|
|
|
for(len=0 ; src[len] ; ++len){}
|
|
|
|
Convert(src, len, dst);
|
|
}
|
|
|
|
|
|
|
|
template<class StringType>
|
|
void TextStream<StringType>::Convert(const std::wstring & src, std::wstring & dst)
|
|
{
|
|
dst.append(src);
|
|
}
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
#endif
|
|
|