From efa384336d10d678123b020b534b14cd7f36cef6 Mon Sep 17 00:00:00 2001 From: dehnert Date: Thu, 17 Nov 2016 12:05:12 +0100 Subject: [PATCH] update cpptemplate to slightly fixed version Former-commit-id: 8b7db204a01d50c79918b003709252a1b0f3c8e8 [formerly 630083e7c696180e506a8df3bd01d2c05000971b] Former-commit-id: 49761de827bf68487855acaec0c863f650199053 --- resources/3rdparty/cpptemplate/LICENSE.txt | 0 resources/3rdparty/cpptemplate/README.rst | 9 +- resources/3rdparty/cpptemplate/cpptempl.cpp | 110 ++++++----- resources/3rdparty/cpptemplate/cpptempl.h | 173 ++++++------------ .../3rdparty/cpptemplate/cpptempl_test.cpp | 0 resources/3rdparty/cpptemplate/unit_testing.h | 0 6 files changed, 112 insertions(+), 180 deletions(-) mode change 100644 => 100755 resources/3rdparty/cpptemplate/LICENSE.txt mode change 100644 => 100755 resources/3rdparty/cpptemplate/README.rst mode change 100644 => 100755 resources/3rdparty/cpptemplate/cpptempl.cpp mode change 100644 => 100755 resources/3rdparty/cpptemplate/cpptempl.h mode change 100644 => 100755 resources/3rdparty/cpptemplate/cpptempl_test.cpp mode change 100644 => 100755 resources/3rdparty/cpptemplate/unit_testing.h diff --git a/resources/3rdparty/cpptemplate/LICENSE.txt b/resources/3rdparty/cpptemplate/LICENSE.txt old mode 100644 new mode 100755 diff --git a/resources/3rdparty/cpptemplate/README.rst b/resources/3rdparty/cpptemplate/README.rst old mode 100644 new mode 100755 index 029914b50..f323686ac --- a/resources/3rdparty/cpptemplate/README.rst +++ b/resources/3rdparty/cpptemplate/README.rst @@ -46,12 +46,11 @@ Lists, nested maps Example:: - cpptempl::data_map person ; - person["name"] = "Bob" ; - person["occupation"] = "Plumber" ; + cpptempl::data_map person_data ; + person_data["name"] = "Bob" ; + person_data["occupation"] = "Plumber" ; cpptempl::data_map content ; - content["person"] = person ; + content["person"] = person_data ; content["friends"].push_back("Alice") ; content["friends"].push_back("Bob") ; - string result = parse(text, data) ; diff --git a/resources/3rdparty/cpptemplate/cpptempl.cpp b/resources/3rdparty/cpptemplate/cpptempl.cpp old mode 100644 new mode 100755 index 85b59d525..01cc630fa --- a/resources/3rdparty/cpptemplate/cpptempl.cpp +++ b/resources/3rdparty/cpptemplate/cpptempl.cpp @@ -15,20 +15,21 @@ namespace cpptempl ////////////////////////////////////////////////////////////////////////// // data_map - data_ptr& data_map::operator [](const std::wstring& key) { - return data[key]; - } data_ptr& data_map::operator [](const std::string& key) { - return data[utf8_to_wide(key)]; + return data[key]; } bool data_map::empty() { return data.empty(); } - bool data_map::has(const wstring& key) { + bool data_map::has(const std::string& key) { return data.find(key) != data.end(); } // data_ptr + data_ptr::data_ptr(DataValue* data) : ptr(data) {} + data_ptr::data_ptr(DataList* data) : ptr(data) {} + data_ptr::data_ptr(DataMap* data) : ptr(data) {} + template<> inline void data_ptr::operator = (const data_ptr& data) { ptr = data.ptr; @@ -36,11 +37,6 @@ namespace cpptempl template<> void data_ptr::operator = (const std::string& data) { - ptr.reset(new DataValue(utf8_to_wide(data))); - } - - template<> - void data_ptr::operator = (const std::wstring& data) { ptr.reset(new DataValue(data)); } @@ -58,7 +54,7 @@ namespace cpptempl } // base data - wstring Data::getvalue() + std::string Data::getvalue() { throw TemplateException("Data item is not a value") ; } @@ -72,7 +68,7 @@ namespace cpptempl throw TemplateException("Data item is not a dictionary") ; } // data value - wstring DataValue::getvalue() + std::string DataValue::getvalue() { return m_value ; } @@ -102,28 +98,28 @@ namespace cpptempl ////////////////////////////////////////////////////////////////////////// // parse_val ////////////////////////////////////////////////////////////////////////// - data_ptr parse_val(wstring key, data_map &data) + data_ptr parse_val(std::string key, data_map &data) { // quoted string - if (key[0] == L'\"') + if (key[0] == '\"') { - return make_data(boost::trim_copy_if(key, boost::is_any_of(L"\""))) ; + return make_data(boost::trim_copy_if(key, boost::is_any_of("\""))) ; } // check for dotted notation, i.e [foo.bar] - size_t index = key.find(L".") ; - if (index == wstring::npos) + size_t index = key.find(".") ; + if (index == std::string::npos) { if (!data.has(key)) { - return make_data(L"{$" + key + L"}") ; + return make_data("{$" + key + "}") ; } return data[key] ; } - wstring sub_key = key.substr(0, index) ; + std::string sub_key = key.substr(0, index) ; if (!data.has(sub_key)) { - return make_data(L"{$" + key + L"}") ; + return make_data("{$" + key + "}") ; } data_ptr item = data[sub_key] ; return parse_val(key.substr(index+1), item->getmap()) ; @@ -150,7 +146,7 @@ namespace cpptempl return TOKEN_TYPE_TEXT ; } - void TokenText::gettext( std::wostream &stream, data_map & ) + void TokenText::gettext( std::ostream &stream, data_map & ) { stream << m_text ; } @@ -161,15 +157,15 @@ namespace cpptempl return TOKEN_TYPE_VAR ; } - void TokenVar::gettext( std::wostream &stream, data_map &data ) + void TokenVar::gettext( std::ostream &stream, data_map &data ) { stream << parse_val(m_key, data)->getvalue() ; } // TokenFor - TokenFor::TokenFor(wstring expr) + TokenFor::TokenFor(std::string expr) { - std::vector elements ; + std::vector elements ; boost::split(elements, expr, boost::is_space()) ; if (elements.size() != 4u) { @@ -184,16 +180,16 @@ namespace cpptempl return TOKEN_TYPE_FOR ; } - void TokenFor::gettext( std::wostream &stream, data_map &data ) + void TokenFor::gettext( std::ostream &stream, data_map &data ) { data_ptr value = parse_val(m_key, data) ; data_list &items = value->getlist() ; for (size_t i = 0 ; i < items.size() ; ++i) { data_map loop ; - loop[L"index"] = make_data(boost::lexical_cast(i+1)) ; - loop[L"index0"] = make_data(boost::lexical_cast(i)) ; - data[L"loop"] = make_data(loop); + loop["index"] = make_data(boost::lexical_cast(i+1)) ; + loop["index0"] = make_data(boost::lexical_cast(i)) ; + data["loop"] = make_data(loop); data[m_val] = items[i] ; for(size_t j = 0 ; j < m_children.size() ; ++j) { @@ -218,7 +214,7 @@ namespace cpptempl return TOKEN_TYPE_IF ; } - void TokenIf::gettext( std::wostream &stream, data_map &data ) + void TokenIf::gettext( std::ostream &stream, data_map &data ) { if (is_true(m_expr, data)) { @@ -229,12 +225,12 @@ namespace cpptempl } } - bool TokenIf::is_true( wstring expr, data_map &data ) + bool TokenIf::is_true( std::string expr, data_map &data ) { - std::vector elements ; + std::vector elements ; boost::split(elements, expr, boost::is_space()) ; - if (elements[1] == L"not") + if (elements[1] == "not") { return parse_val(elements[2], data)->empty() ; } @@ -244,7 +240,7 @@ namespace cpptempl } data_ptr lhs = parse_val(elements[1], data) ; data_ptr rhs = parse_val(elements[3], data) ; - if (elements[2] == L"==") + if (elements[2] == "==") { return lhs->getvalue() == rhs->getvalue() ; } @@ -264,10 +260,10 @@ namespace cpptempl // TokenEnd TokenType TokenEnd::gettype() { - return m_type == L"endfor" ? TOKEN_TYPE_ENDFOR : TOKEN_TYPE_ENDIF ; + return m_type == "endfor" ? TOKEN_TYPE_ENDFOR : TOKEN_TYPE_ENDIF ; } - void TokenEnd::gettext( std::wostream &, data_map &) + void TokenEnd::gettext( std::ostream &, data_map &) { throw TemplateException("End-of-control statements have no associated text") ; } @@ -275,9 +271,9 @@ namespace cpptempl // gettext // generic helper for getting text from tokens. - wstring gettext(token_ptr token, data_map &data) + std::string gettext(token_ptr token, data_map &data) { - std::wostringstream stream ; + std::ostringstream stream ; token->gettext(stream, data) ; return stream.str() ; } @@ -316,12 +312,12 @@ namespace cpptempl // tokenize // parses a template into tokens (text, for, if, variable) ////////////////////////////////////////////////////////////////////////// - token_vector & tokenize(wstring text, token_vector &tokens) + token_vector & tokenize(std::string text, token_vector &tokens) { while(! text.empty()) { - size_t pos = text.find(L"{") ; - if (pos == wstring::npos) + size_t pos = text.find("{") ; + if (pos == std::string::npos) { if (! text.empty()) { @@ -329,7 +325,7 @@ namespace cpptempl } return tokens ; } - wstring pre_text = text.substr(0, pos) ; + std::string pre_text = text.substr(0, pos) ; if (! pre_text.empty()) { tokens.push_back(token_ptr(new TokenText(pre_text))) ; @@ -337,33 +333,33 @@ namespace cpptempl text = text.substr(pos+1) ; if (text.empty()) { - tokens.push_back(token_ptr(new TokenText(L"{"))) ; + tokens.push_back(token_ptr(new TokenText("{"))) ; return tokens ; } // variable - if (text[0] == L'$') + if (text[0] == '$') { - pos = text.find(L"}") ; - if (pos != wstring::npos) + pos = text.find("}") ; + if (pos != std::string::npos) { tokens.push_back(token_ptr (new TokenVar(text.substr(1, pos-1)))) ; text = text.substr(pos+1) ; } } // control statement - else if (text[0] == L'%') + else if (text[0] == '%') { - pos = text.find(L"}") ; - if (pos != wstring::npos) + pos = text.find("}") ; + if (pos != std::string::npos) { - wstring expression = boost::trim_copy(text.substr(1, pos-2)) ; + std::string expression = boost::trim_copy(text.substr(1, pos-2)) ; text = text.substr(pos+1) ; - if (boost::starts_with(expression, L"for")) + if (boost::starts_with(expression, "for")) { tokens.push_back(token_ptr (new TokenFor(expression))) ; } - else if (boost::starts_with(expression, L"if")) + else if (boost::starts_with(expression, "if")) { tokens.push_back(token_ptr (new TokenIf(expression))) ; } @@ -375,7 +371,7 @@ namespace cpptempl } else { - tokens.push_back(token_ptr(new TokenText(L"{"))) ; + tokens.push_back(token_ptr(new TokenText("{"))) ; } } return tokens ; @@ -389,17 +385,13 @@ namespace cpptempl * 3. resolves template * 4. returns converted text ************************************************************************/ - wstring parse(wstring templ_text, data_map &data) + std::string parse(std::string templ_text, data_map &data) { - std::wostringstream stream ; + std::ostringstream stream ; parse(stream, templ_text, data) ; return stream.str() ; } - std::string parse(std::string templ_text, data_map &data) - { - return wide_to_utf8(parse(utf8_to_wide(templ_text), data)); - } - void parse(std::wostream &stream, wstring templ_text, data_map &data) + void parse(std::ostream &stream, std::string templ_text, data_map &data) { token_vector tokens ; tokenize(templ_text, tokens) ; diff --git a/resources/3rdparty/cpptemplate/cpptempl.h b/resources/3rdparty/cpptemplate/cpptempl.h old mode 100644 new mode 100755 index d5770d999..bd7c77510 --- a/resources/3rdparty/cpptemplate/cpptempl.h +++ b/resources/3rdparty/cpptemplate/cpptempl.h @@ -16,24 +16,24 @@ MIT License Usage ======================= - wstring text = L"{% if item %}{$item}{% endif %}\n" - L"{% if thing %}{$thing}{% endif %}" ; + std::string text = "{% if item %}{$item}{% endif %}\n" + "{% if thing %}{$thing}{% endif %}" ; cpptempl::data_map data ; - data[L"item"] = cpptempl::make_data(L"aaa") ; - data[L"thing"] = cpptempl::make_data(L"bbb") ; + data["item"] = cpptempl::make_data("aaa") ; + data["thing"] = cpptempl::make_data("bbb") ; - wstring result = cpptempl::parse(text, data) ; + std::string result = cpptempl::parse(text, data) ; Handy Functions ======================== make_data() : Feed it a string, data_map, or data_list to create a data entry. Example: data_map person ; - person[L"name"] = make_data(L"Bob") ; - person[L"occupation"] = make_data(L"Plumber") ; + person["name"] = make_data("Bob") ; + person["occupation"] = make_data("Plumber") ; data_map data ; - data[L"person"] = make_data(person) ; - wstring result = parse(templ_text, data) ; + data["person"] = make_data(person) ; + std::string result = parse(templ_text, data) ; */ #pragma once @@ -46,38 +46,16 @@ Example: #include #include #include -#include -#ifndef _MSC_VER -#include -#include -#else -#include -#include "windows.h" -#include "winnls.h" // unicode-multibyte conversion -#endif -#include +#include +#include #include #include namespace cpptempl { - using std::wstring ; // various typedefs - class data_ptr; - typedef std::vector data_list ; - - class data_map { - public: - data_ptr& operator [](const std::wstring& key); - data_ptr& operator [](const std::string& key); - bool empty(); - bool has(const wstring& key); - private: - boost::unordered_map data; - }; - // data classes class Data ; class DataValue ; @@ -90,9 +68,9 @@ namespace cpptempl template data_ptr(const T& data) { this->operator =(data); } - data_ptr(DataValue* data) : ptr(data) {} - data_ptr(DataList* data) : ptr(data) {} - data_ptr(DataMap* data) : ptr(data) {} + data_ptr(DataValue* data); + data_ptr(DataList* data); + data_ptr(DataMap* data); data_ptr(const data_ptr& data) { ptr = data.ptr; } @@ -103,66 +81,32 @@ namespace cpptempl return ptr.get(); } private: - boost::shared_ptr ptr; + std::shared_ptr ptr; + }; + typedef std::vector data_list ; + + class data_map { + public: + data_ptr& operator [](const std::string& key); + bool empty(); + bool has(const std::string& key); + private: + std::unordered_map data; }; template<> inline void data_ptr::operator = (const data_ptr& data); template<> void data_ptr::operator = (const std::string& data); - template<> void data_ptr::operator = (const std::wstring& data); + template<> void data_ptr::operator = (const std::string& data); template<> void data_ptr::operator = (const data_map& data); template void data_ptr::operator = (const T& data) { -#ifndef _MSC_VER - std::wstring data_str = boost::lexical_cast(data); -#else - -#endif + std::string data_str = boost::lexical_cast(data); this->operator =(data_str); } - // convenience functions for recoding utf8 string to wstring and back - inline std::wstring utf8_to_wide(const std::string& text) { -#ifndef _MSC_VER - std::wstring result; - if (sizeof(wchar_t) == 2) { - utf8::utf8to16(text.begin(), text.end(), std::back_inserter(result)); - } else { - assert(sizeof(wchar_t) == 4); - utf8::utf8to32(text.begin(), text.end(), std::back_inserter(result)); - } - return result; - //return boost::locale::conv::to_utf(text, "UTF-8"); -#else - // Calculate the required length of the buffer - const size_t len_needed = ::MultiByteToWideChar(CP_UTF8, 0, text.c_str(), (UINT)(text.length()) , NULL, 0 ); - boost::scoped_array buff(new wchar_t[len_needed+1]) ; - const size_t num_copied = ::MultiByteToWideChar(CP_UTF8, 0, text.c_str(), text.size(), buff.get(), len_needed+1) ; - return std::wstring(buff.get(), num_copied) ; -#endif - } - - inline std::string wide_to_utf8(const std::wstring& text) { -#ifndef _MSC_VER - std::string result; - if (sizeof(wchar_t) == 2) { - utf8::utf16to8(text.begin(), text.end(), std::back_inserter(result)); - } else { - assert(sizeof(wchar_t) == 4); - utf8::utf32to8(text.begin(), text.end(), std::back_inserter(result)); - } - return result; - //return boost::locale::conv::from_utf<>(text, "UTF-8"); -#else - const size_t len_needed = ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), (UINT)(text.length()) , NULL, 0, NULL, NULL) ; - boost::scoped_array buff(new char[len_needed+1]) ; - const size_t num_copied = ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), (UINT)(text.length()) , buff.get(), len_needed+1, NULL, NULL) ; - return std::string(buff.get(), num_copied) ; -#endif - } - // token classes class Token ; - typedef boost::shared_ptr token_ptr ; + typedef std::shared_ptr token_ptr ; typedef std::vector token_vector ; // Custom exception class for library errors @@ -171,7 +115,7 @@ namespace cpptempl public: TemplateException(std::string reason) : m_reason(reason){} ~TemplateException() throw() {} - const char* what() const throw() { + const char* what() throw() { return m_reason.c_str(); } private: @@ -182,21 +126,18 @@ namespace cpptempl class Data { public: - virtual ~Data() { - // Intentionally left empty. - } virtual bool empty() = 0 ; - virtual wstring getvalue(); + virtual std::string getvalue(); virtual data_list& getlist(); virtual data_map& getmap() ; }; class DataValue : public Data { - wstring m_value ; + std::string m_value ; public: - DataValue(wstring value) : m_value(value){} - wstring getvalue(); + DataValue(std::string value) : m_value(value){} + std::string getvalue(); bool empty(); }; @@ -219,7 +160,7 @@ namespace cpptempl }; // convenience functions for making data objects - inline data_ptr make_data(wstring val) + inline data_ptr make_data(std::string val) { return data_ptr(new DataValue(val)) ; } @@ -233,7 +174,7 @@ namespace cpptempl } // get a data value from a data map // e.g. foo.bar => data["foo"]["bar"] - data_ptr parse_val(wstring key, data_map &data) ; + data_ptr parse_val(std::string key, data_map &data) ; typedef enum { @@ -252,7 +193,7 @@ namespace cpptempl { public: virtual TokenType gettype() = 0 ; - virtual void gettext(std::wostream &stream, data_map &data) = 0 ; + virtual void gettext(std::ostream &stream, data_map &data) = 0 ; virtual void set_children(token_vector &children); virtual token_vector & get_children(); }; @@ -260,33 +201,33 @@ namespace cpptempl // normal text class TokenText : public Token { - wstring m_text ; + std::string m_text ; public: - TokenText(wstring text) : m_text(text){} + TokenText(std::string text) : m_text(text){} TokenType gettype(); - void gettext(std::wostream &stream, data_map &data); + void gettext(std::ostream &stream, data_map &data); }; // variable class TokenVar : public Token { - wstring m_key ; + std::string m_key ; public: - TokenVar(wstring key) : m_key(key){} + TokenVar(std::string key) : m_key(key){} TokenType gettype(); - void gettext(std::wostream &stream, data_map &data); + void gettext(std::ostream &stream, data_map &data); }; // for block class TokenFor : public Token { public: - wstring m_key ; - wstring m_val ; + std::string m_key ; + std::string m_val ; token_vector m_children ; - TokenFor(wstring expr); + TokenFor(std::string expr); TokenType gettype(); - void gettext(std::wostream &stream, data_map &data); + void gettext(std::ostream &stream, data_map &data); void set_children(token_vector &children); token_vector &get_children(); }; @@ -295,12 +236,12 @@ namespace cpptempl class TokenIf : public Token { public: - wstring m_expr ; + std::string m_expr ; token_vector m_children ; - TokenIf(wstring expr) : m_expr(expr){} + TokenIf(std::string expr) : m_expr(expr){} TokenType gettype(); - void gettext(std::wostream &stream, data_map &data); - bool is_true(wstring expr, data_map &data); + void gettext(std::ostream &stream, data_map &data); + bool is_true(std::string expr, data_map &data); void set_children(token_vector &children); token_vector &get_children(); }; @@ -308,21 +249,21 @@ namespace cpptempl // end of block class TokenEnd : public Token // end of control block { - wstring m_type ; + std::string m_type ; public: - TokenEnd(wstring text) : m_type(text){} + TokenEnd(std::string text) : m_type(text){} TokenType gettype(); - void gettext(std::wostream &stream, data_map &data); + void gettext(std::ostream &stream, data_map &data); }; - wstring gettext(token_ptr token, data_map &data) ; + std::string gettext(token_ptr token, data_map &data) ; void parse_tree(token_vector &tokens, token_vector &tree, TokenType until=TOKEN_TYPE_NONE) ; - token_vector & tokenize(wstring text, token_vector &tokens) ; + token_vector & tokenize(std::string text, token_vector &tokens) ; // The big daddy. Pass in the template and data, // and get out a completed doc. - void parse(std::wostream &stream, wstring templ_text, data_map &data) ; - wstring parse(wstring templ_text, data_map &data); + void parse(std::ostream &stream, std::string templ_text, data_map &data) ; + std::string parse(std::string templ_text, data_map &data); std::string parse(std::string templ_text, data_map &data); } diff --git a/resources/3rdparty/cpptemplate/cpptempl_test.cpp b/resources/3rdparty/cpptemplate/cpptempl_test.cpp old mode 100644 new mode 100755 diff --git a/resources/3rdparty/cpptemplate/unit_testing.h b/resources/3rdparty/cpptemplate/unit_testing.h old mode 100644 new mode 100755