winix/winixd/templates/textextstream.cpp

505 lines
8.5 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) 2012-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.
*
*/
#include "textextstream.h"
namespace Winix
{
TexTextStream::TexTextStream()
{
}
/*
without escaping
*/
TexTextStream & TexTextStream::PutChar(char c)
{
TextStream<std::wstring>::operator<<(c);
return *this;
}
TexTextStream & TexTextStream::PutChar(wchar_t c)
{
TextStream<std::wstring>::operator<<(c);
return *this;
}
TexTextStream & TexTextStream::PutText(const char * str)
{
TextStream<std::wstring>::operator<<(str);
return *this;
}
TexTextStream & TexTextStream::PutText(const char * str, size_t len)
{
TextStream<std::wstring>::operator<<(str);
return *this;
}
TexTextStream & TexTextStream::PutText(const std::string * str)
{
return PutText(str->c_str());
}
TexTextStream & TexTextStream::PutText(const std::string & str)
{
return PutText(str.c_str());
}
TexTextStream & TexTextStream::PutText(const wchar_t * str)
{
TextStream<std::wstring>::operator<<(str);
return *this;
}
TexTextStream & TexTextStream::PutText(const std::wstring * str)
{
return PutText(str->c_str());
}
TexTextStream & TexTextStream::PutText(const std::wstring & str)
{
return PutText(str.c_str());
}
TexTextStream & TexTextStream::operator<<(const RawText<const char*> & raw)
{
return PutText(raw.par);
}
TexTextStream & TexTextStream::operator<<(const RawText<const wchar_t*> & raw)
{
return PutText(raw.par);
}
TexTextStream & TexTextStream::operator<<(RawText<const std::string*> raw)
{
return PutText(raw.par);
}
TexTextStream & TexTextStream::operator<<(RawText<const std::wstring*> raw)
{
return PutText(raw.par);
}
TexTextStream & TexTextStream::operator<<(RawText<std::string> raw)
{
return PutText(raw.par);
}
TexTextStream & TexTextStream::operator<<(RawText<std::wstring> raw)
{
return PutText(raw.par);
}
TexTextStream & TexTextStream::operator<<(RawText<char> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::operator<<(RawText<wchar_t> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::operator<<(RawText<int> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::operator<<(RawText<long> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::operator<<(RawText<unsigned int> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::operator<<(RawText<unsigned long> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::operator<<(RawText<double> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::operator<<(RawText<void*> raw)
{
TextStream<std::wstring>::operator<<(raw.par);
return *this;
}
TexTextStream & TexTextStream::Write(const char * buf, size_t len)
{
TextStream<std::wstring>::Write(buf, len);
return *this;
}
TexTextStream & TexTextStream::Write(const wchar_t * buf, size_t len)
{
TextStream<std::wstring>::Write(buf, len);
return *this;
}
TexTextStream & TexTextStream::write(const char * buf, size_t len)
{
TextStream<std::wstring>::write(buf, len);
return *this;
}
TexTextStream & TexTextStream::write(const wchar_t * buf, size_t len)
{
TextStream<std::wstring>::write(buf, len);
return *this;
}
/*
with escaping
*/
TexTextStream & TexTextStream::ETextPutChar(char c)
{
return ETextPutChar(static_cast<wchar_t>(c));
}
TexTextStream & TexTextStream::ETextPutChar(wchar_t c)
{
if( c == '$' )
buffer += L"\\$";
else
if( c == '#' )
buffer += L"\\#";
else
if( c == '%' )
buffer += L"\\%";
else
if( c == '&' )
buffer += L"\\&";
else
if( c == '\\' )
buffer += L"$\\backslash$";
else
if( c == '{' )
buffer += L"$\\{$";
else
if( c == '}' )
buffer += L"$\\}$";
else
if( c == '^' )
buffer += L""; // !! IMPROVE ME add \char with specific code
else
if( c == '_' )
buffer += L"\\_";
else
if( c == '~' )
buffer += L""; // !! IMPROVE ME add \char with specific code
else
if( c == '-' )
buffer += L"{-}";
else
if( c != 0 )
buffer += c;
return *this;
}
TexTextStream & TexTextStream::EPutText(const char * str)
{
for( ; *str ; ++str )
ETextPutChar(*str);
return *this;
}
TexTextStream & TexTextStream::EPutText(const char * str, size_t len)
{
for(size_t i=0 ; i<len ; ++i)
ETextPutChar(str[i]);
return *this;
}
TexTextStream & TexTextStream::EPutText(const std::string * str)
{
return EPutText(str->c_str(), str->size());
}
TexTextStream & TexTextStream::EPutText(const std::string & str)
{
return EPutText(str.c_str(), str.size());
}
TexTextStream & TexTextStream::EPutText(const wchar_t * str)
{
for( ; *str ; ++str )
ETextPutChar(*str);
return *this;
}
TexTextStream & TexTextStream::EPutText(const wchar_t * str, size_t len)
{
for(size_t i=0 ; i<len ; ++i)
ETextPutChar(str[i]);
return *this;
}
TexTextStream & TexTextStream::EPutText(const std::wstring * str)
{
return EPutText(str->c_str(), str->size());
}
TexTextStream & TexTextStream::EPutText(const std::wstring & str)
{
return EPutText(str.c_str(), str.size());
}
TexTextStream & TexTextStream::operator<<(const char * str)
{
return EPutText(str);
}
TexTextStream & TexTextStream::operator<<(const std::string * str)
{
return EPutText(str);
}
TexTextStream & TexTextStream::operator<<(const std::string & str)
{
return EPutText(str);
}
TexTextStream & TexTextStream::operator<<(const wchar_t * str)
{
return EPutText(str);
}
TexTextStream & TexTextStream::operator<<(const std::wstring * str)
{
return EPutText(str);
}
TexTextStream & TexTextStream::operator<<(const std::wstring & str)
{
return EPutText(str);
}
TexTextStream & TexTextStream::operator<<(char v)
{
ETextPutChar(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(wchar_t v)
{
ETextPutChar(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(int v)
{
TextStream<std::wstring>::operator<<(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(long v)
{
TextStream<std::wstring>::operator<<(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(unsigned int v)
{
TextStream<std::wstring>::operator<<(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(unsigned long v)
{
TextStream<std::wstring>::operator<<(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(double v)
{
TextStream<std::wstring>::operator<<(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(const void * v)
{
TextStream<std::wstring>::operator<<(v);
return *this;
}
TexTextStream & TexTextStream::operator<<(const pt::Space & space)
{
tmp_stream.Clear();
// !! IMPROVE ME
// we can calculate how many memory is needed beforehand
space.serialize_to_space_stream(tmp_stream, true);
operator<<(tmp_stream.Str());
tmp_stream.Clear();
return *this;
}
TexTextStream & TexTextStream::operator<<(const pt::Date & date)
{
tmp_stream.Clear();
// !! IMPROVE ME
// we can calculate how many memory is needed beforehand
date.Serialize(tmp_stream);
operator<<(tmp_stream.Str());
tmp_stream.Clear();
return *this;
}
} // namespace Winix