/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * 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 #include #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 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 TextStream & operator<<(const pt::TextStreamBase & 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 void TextStream::Clear() { buffer.clear(); } template void TextStream::clear() { Clear(); } template bool TextStream::Empty() const { return buffer.empty(); } template size_t TextStream::Size() const { return buffer.size(); } template void TextStream::Reserve(size_t len) { buffer.reserve(len); } template typename TextStream::iterator TextStream::begin() { return buffer.begin(); } template typename TextStream::iterator TextStream::end() { return buffer.end(); } template typename TextStream::const_iterator TextStream::begin() const { return buffer.begin(); } template typename TextStream::const_iterator TextStream::end() const { return buffer.end(); } template const StringType & TextStream::Str() const { return buffer; } template const typename TextStream::CharType * TextStream::CStr() const { return buffer.c_str(); } template void TextStream::Str(const StringType & str) { buffer = str; } template void TextStream::Str(const StringType && str) { buffer = std::move(str); } template void TextStream::to_str(std::string & str, bool clear_string) const { return to_string(str, clear_string); } template void TextStream::to_str(std::wstring & str, bool clear_string) const { return to_string(str, clear_string); } template void TextStream::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 void TextStream::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 typename TextStream::CharType TextStream::operator[](size_t index) { return buffer[index]; } template TextStream & TextStream::operator<<(const char * str) { Convert(str, buffer); return *this; } template TextStream & TextStream::operator<<(const std::string * str) { Convert(*str, buffer); return *this; } template TextStream & TextStream::operator<<(const std::string & str) { Convert(str, buffer); return *this; } template TextStream & TextStream::operator<<(const wchar_t * str) { Convert(str, buffer); return *this; } template TextStream & TextStream::operator<<(const std::wstring * str) { Convert(*str, buffer); return *this; } template TextStream & TextStream::operator<<(const std::wstring & str) { Convert(str, buffer); return *this; } template TextStream & TextStream::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 TextStream & TextStream::operator<<(wchar_t v) { Convert(v, buffer); return *this; } template TextStream & TextStream::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 TextStream & TextStream::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 TextStream & TextStream::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 TextStream & TextStream::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 TextStream & TextStream::operator<<(double v) { char buf[50]; sprintf(buf, "%f", v); Convert(buf, buffer); return *this; } template TextStream & TextStream::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(v), buf+2, len-2, 16); Convert(buf, buffer); return *this; } template TextStream & TextStream::Write(const char * buf, size_t len) { Convert(buf, len, buffer); return *this; } template TextStream & TextStream::write(const char * buf, size_t len) { return Write(buf, len); } template TextStream & TextStream::Write(const wchar_t * buf, size_t len) { Convert(buf, len, buffer); return *this; } template TextStream & TextStream::write(const wchar_t * buf, size_t len) { return Write(buf, len); } template TextStream & TextStream::operator<<(const pt::Space & space) { space.serialize_to_space_stream(*this, true); return *this; } template TextStream & TextStream::operator<<(const pt::Date & date) { date.Serialize(*this); return *this; } template TextStream & TextStream::operator<<(const TextStream & stream) { buffer.append(stream.buffer); return *this; } template template TextStream & TextStream::operator<<( const pt::TextStreamBase & arg) { typename pt::TextStreamBase::const_iterator i; for(i=arg.begin() ; i != arg.end() ; ++i) buffer += static_cast(*i); return *this; } template void TextStream::Convert(wchar_t c, std::string & dst) { pt::int_to_utf8((int)c, dst, false); } template void TextStream::Convert(wchar_t c, std::wstring & dst) { dst += c; } template void TextStream::Convert(const char * src, size_t len, std::wstring & dst) { pt::utf8_to_wide(src, len, dst, false); } template void TextStream::Convert(const char * src, std::wstring & dst) { pt::utf8_to_wide(src, dst, false); } template void TextStream::Convert(const std::string & src, std::wstring & dst) { pt::utf8_to_wide(src, dst, false); } template void TextStream::Convert(const wchar_t * src, size_t len, std::string & dst) { pt::wide_to_utf8(src, len, dst, false); } template void TextStream::Convert(const wchar_t * src, std::string & dst) { pt::wide_to_utf8(src, dst, false); } template void TextStream::Convert(const std::wstring & src, std::string & dst) { pt::wide_to_utf8(src, dst, false); } template void TextStream::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 void TextStream::Convert(const char * src, std::string & dst) { size_t len; for(len=0 ; src[len] ; ++len){} Convert(src, len, dst); } template void TextStream::Convert(const std::string & src, std::string & dst) { dst.append(src); } template void TextStream::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 void TextStream::Convert(const wchar_t * src, std::wstring & dst) { size_t len; for(len=0 ; src[len] ; ++len){} Convert(src, len, dst); } template void TextStream::Convert(const std::wstring & src, std::wstring & dst) { dst.append(src); } } // namespace Winix #endif