moved winix directories to winixd subdirectory
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@1027 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
556
winixd/core/textstream.h
Normal file
556
winixd/core/textstream.h
Normal file
@@ -0,0 +1,556 @@
|
||||
/*
|
||||
* 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-2014, 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;
|
||||
|
||||
void Clear();
|
||||
bool Empty() const;
|
||||
size_t Size() const;
|
||||
void Reserve(size_t len);
|
||||
|
||||
const StringType & Str() const;
|
||||
const CharType * CStr() const;
|
||||
|
||||
void Str(const StringType & str);
|
||||
void Str(const StringType && str);
|
||||
|
||||
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);
|
||||
|
||||
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>
|
||||
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>
|
||||
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 = str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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(*this, true, false);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
TextStream<StringType> & TextStream<StringType>::operator<<(const PT::Date & date)
|
||||
{
|
||||
date.Serialize(*this);
|
||||
|
||||
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::IntToUTF8((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::UTF8ToWide(src, len, dst, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const char * src, std::wstring & dst)
|
||||
{
|
||||
PT::UTF8ToWide(src, dst, false);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const std::string & src, std::wstring & dst)
|
||||
{
|
||||
PT::UTF8ToWide(src, dst, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const wchar_t * src, size_t len, std::string & dst)
|
||||
{
|
||||
PT::WideToUTF8(src, len, dst, false);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const wchar_t * src, std::string & dst)
|
||||
{
|
||||
PT::WideToUTF8(src, dst, false);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void TextStream<StringType>::Convert(const std::wstring & src, std::string & dst)
|
||||
{
|
||||
PT::WideToUTF8(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
|
||||
|
||||
Reference in New Issue
Block a user