2010-09-18 02:51:12 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010, Tomasz Sowa
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef headerfile_winix_core_textstream
|
|
|
|
#define headerfile_winix_core_textstream
|
|
|
|
|
|
|
|
#include <string>
|
2010-11-21 01:19:17 +01:00
|
|
|
#include "misc.h"
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
template<class StringType>
|
2010-09-18 02:51:12 +02:00
|
|
|
class TextStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
typedef typename StringType::value_type CharType;
|
|
|
|
typedef typename StringType::value_type char_type;
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
void Clear();
|
2010-11-21 01:19:17 +01:00
|
|
|
bool Empty() const;
|
|
|
|
size_t Size() const;
|
|
|
|
const StringType & Str() const;
|
|
|
|
const CharType * CStr() const;
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
TextStream & operator<<(const char * str);
|
|
|
|
TextStream & operator<<(const std::string * str);
|
|
|
|
TextStream & operator<<(const std::string & str);
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
TextStream & operator<<(const wchar_t * str);
|
|
|
|
TextStream & operator<<(const std::wstring * str);
|
|
|
|
TextStream & operator<<(const std::wstring & str);
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
TextStream & operator<<(char);
|
2010-11-21 01:19:17 +01:00
|
|
|
TextStream & operator<<(wchar_t);
|
2010-09-18 02:51:12 +02:00
|
|
|
TextStream & operator<<(int);
|
|
|
|
TextStream & operator<<(long);
|
|
|
|
TextStream & operator<<(unsigned int);
|
|
|
|
TextStream & operator<<(unsigned long);
|
|
|
|
TextStream & operator<<(double);
|
|
|
|
TextStream & operator<<(const void *);// printing a pointer
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
TextStream & Write(const CharType * buf, size_t len);
|
|
|
|
TextStream & write(const CharType * buf, size_t len); // for compatibility with standard library (Ezc uses it)
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
protected:
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
StringType buffer;
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
template<class StringType>
|
|
|
|
void TextStream<StringType>::Clear()
|
|
|
|
{
|
|
|
|
buffer.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>
|
|
|
|
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>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const char * str)
|
|
|
|
{
|
|
|
|
AssignString(str, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::string * str)
|
|
|
|
{
|
|
|
|
AssignString(*str, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::string & str)
|
|
|
|
{
|
|
|
|
AssignString(str, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const wchar_t * str)
|
|
|
|
{
|
|
|
|
AssignString(str, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::wstring * str)
|
|
|
|
{
|
|
|
|
AssignString(*str, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const std::wstring & str)
|
|
|
|
{
|
|
|
|
AssignString(str, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(char v)
|
|
|
|
{
|
|
|
|
buffer += v;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(wchar_t v)
|
|
|
|
{
|
|
|
|
buffer += static_cast<CharType>(v);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(int v)
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
|
|
|
|
sprintf(buf, "%d", v);
|
|
|
|
AssignString(buf, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(long v)
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
|
|
|
|
sprintf(buf, "%ld", v);
|
|
|
|
AssignString(buf, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(unsigned int v)
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
|
|
|
|
sprintf(buf, "%u", v);
|
|
|
|
AssignString(buf, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(unsigned long v)
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
|
|
|
|
sprintf(buf, "%lu", v);
|
|
|
|
AssignString(buf, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(double v)
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
|
|
|
|
sprintf(buf, "%f", v);
|
|
|
|
AssignString(buf, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::operator<<(const void * v)
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
|
|
|
|
sprintf(buf, "%p", v);
|
|
|
|
AssignString(buf, buffer, false);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::Write(const TextStream<StringType>::CharType * buf, size_t len)
|
|
|
|
{
|
2010-11-23 22:52:25 +01:00
|
|
|
if( buffer.capacity() < buffer.size() + len )
|
|
|
|
buffer.reserve(buffer.size() + len + 256); // !! add some constant
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
for(size_t i=0 ; i<len ; ++i)
|
|
|
|
buffer += buf[i];
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
TextStream<StringType> & TextStream<StringType>::write(const TextStream<StringType>::CharType * buf, size_t len)
|
|
|
|
{
|
|
|
|
return Write(buf, len);
|
|
|
|
}
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|