/* * This file is a part of Winix * and is not publicly distributed * * Copyright (c) 2010-2012, Tomasz Sowa * All rights reserved. * */ #ifndef headerfile_winix_core_textstream #define headerfile_winix_core_textstream #include #include "misc.h" #include "confparser/space.h" /* a special class representing a stream buffer similar to std::ostringstream StringType can be either std::string or std::wstring this class doesn't use UTF-8 in any kind */ template class TextStream { public: typedef typename StringType::value_type CharType; typedef typename StringType::value_type char_type; void Clear(); bool Empty() const; size_t Size() const; void Reserve(size_t len); const StringType & Str() const; const CharType * CStr() 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::Space & space); 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; std::wstring space_str; // for using with spaces }; template void TextStream::Clear() { buffer.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 const StringType & TextStream::Str() const { return buffer; } template const typename TextStream::CharType * TextStream::CStr() const { return buffer.c_str(); } template typename TextStream::CharType TextStream::operator[](size_t index) { return buffer[index]; } template TextStream & TextStream::operator<<(const char * str) { AssignString(str, buffer, false); return *this; } template TextStream & TextStream::operator<<(const std::string * str) { AssignString(*str, buffer, false); return *this; } template TextStream & TextStream::operator<<(const std::string & str) { AssignString(str, buffer, false); return *this; } template TextStream & TextStream::operator<<(const wchar_t * str) { AssignString(str, buffer, false); return *this; } template TextStream & TextStream::operator<<(const std::wstring * str) { AssignString(*str, buffer, false); return *this; } template TextStream & TextStream::operator<<(const std::wstring & str) { AssignString(str, buffer, false); return *this; } template TextStream & TextStream::operator<<(char v) { buffer += v; return *this; } template TextStream & TextStream::operator<<(wchar_t v) { buffer += static_cast(v); return *this; } template TextStream & TextStream::operator<<(int v) { wchar_t buf[50]; size_t len = sizeof(buf) / sizeof(wchar_t); Toa(v, buf, len); AssignString(buf, buffer, false); return *this; } template TextStream & TextStream::operator<<(long v) { wchar_t buf[50]; size_t len = sizeof(buf) / sizeof(wchar_t); Toa(v, buf, len); AssignString(buf, buffer, false); 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); AssignString(buf, buffer, false); 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); AssignString(buf, buffer, false); return *this; } template TextStream & TextStream::operator<<(double v) { char buf[50]; sprintf(buf, "%f", v); AssignString(buf, buffer, false); 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); AssignString(buf, buffer, false); return *this; } template TextStream & TextStream::Write(const char * buf, size_t len) { AssignString(buf, len, buffer, false); 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) { AssignString(buf, len, buffer, false); 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) { // !! check me pls space->Serialize(*this, true, false); return *this; } template TextStream & TextStream::operator<<(const PT::Space & space) { return operator<<(&space); } #endif