/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2012-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_templates_textextstream #define headerfile_winix_templates_textextstream #include #include "core/textstream.h" #include "textstream/textstream.h" namespace Winix { /* TexTextStream is used as a buffer for creating a TeX input By default all operators<< escape its string arguments. If you don't want to escape an argument you should use a helper function R() (raw argument) note: you have to define the function yourself, we do not provide it because such a short name would make a mess in namespaces sample: create a helper function R as follows: template TexTextStream::RawText R(const RawType & par) { return TexTextStream::RawText(par); } now you can use TexTextStream in an easy way: TexTextStream page; std::string key = "some text with $# ^{} tex \\ special characters"; page << key << R("\\def\\myfun{...}"); everything in 'key' is property escaped for using with TeX */ class TexTextStream : public TextStream { public: TexTextStream(); /* a helper struct to select a proper operator<< (for non-escaping versions of these operators) */ template struct RawText { const RawType & par; RawText(const RawText & p) : par(p.par) {} RawText(const RawType & p) : par(p) {} }; /* without escaping */ TexTextStream & PutChar(char); TexTextStream & PutChar(wchar_t); TexTextStream & PutText(const char *); TexTextStream & PutText(const char *, size_t len); TexTextStream & PutText(const std::string *); TexTextStream & PutText(const std::string &); TexTextStream & PutText(const wchar_t * str); TexTextStream & PutText(const wchar_t * str, size_t len); TexTextStream & PutText(const std::wstring * str); TexTextStream & PutText(const std::wstring & str); /* we need this template operator for such calling: HtmlTextStream_object << R("some string"); "some string" is actually a table (not a pointer) */ template TexTextStream & operator<<(const RawText & raw) { return PutText(raw.par); } template TexTextStream & operator<<(const RawText & raw) { return PutText(raw.par); } TexTextStream & operator<<(const RawText & raw); TexTextStream & operator<<(const RawText & raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); TexTextStream & operator<<(RawText raw); template TexTextStream & operator<<(RawText > raw); // 'write' don't escapes too // with these methods you can write a zero character too TexTextStream & Write(const char * buf, size_t len); TexTextStream & Write(const wchar_t * buf, size_t len); // for compatibility with standard library (Ezc uses it) TexTextStream & write(const char * buf, size_t len); TexTextStream & write(const wchar_t * buf, size_t len); /* with escaping */ TexTextStream & ETextPutChar(char c); TexTextStream & ETextPutChar(wchar_t c); TexTextStream & EPutText(const char * str); TexTextStream & EPutText(const char * str, size_t len); TexTextStream & EPutText(const std::string * str); TexTextStream & EPutText(const std::string & str); TexTextStream & EPutText(const wchar_t * str); TexTextStream & EPutText(const wchar_t * str, size_t len); TexTextStream & EPutText(const std::wstring * str); TexTextStream & EPutText(const std::wstring & str); TexTextStream & operator<<(const char * str); TexTextStream & operator<<(const std::string * str); TexTextStream & operator<<(const std::string & str); TexTextStream & operator<<(const wchar_t * str); TexTextStream & operator<<(const std::wstring * str); TexTextStream & operator<<(const std::wstring & str); TexTextStream & operator<<(char); TexTextStream & operator<<(wchar_t); TexTextStream & operator<<(int); TexTextStream & operator<<(long); TexTextStream & operator<<(unsigned int); TexTextStream & operator<<(unsigned long); TexTextStream & operator<<(double); TexTextStream & operator<<(const void *); TexTextStream & operator<<(const pt::Space & space); TexTextStream & operator<<(const pt::Date & Date); template TexTextStream & operator<<(const pt::TextStreamBase & arg); private: TextStream tmp_stream; }; template TexTextStream & TexTextStream::operator<<(RawText > raw) { TextStream::operator<<(raw.par); return *this; } template TexTextStream & TexTextStream::operator<<(const pt::TextStreamBase & arg) { typename pt::TextStreamBase::const_iterator i; for(i=arg.begin() ; i != arg.end() ; ++i) ETextPutChar(*i); return *this; } } // namespace Winix #endif