From 8b3fba02cc9aca44c92a4536d6700cb271a359f1 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Sun, 12 Jun 2016 23:36:38 -0400 Subject: [PATCH] GCC missing codecvt support --- src/util/DataTree.cpp | 46 +++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/util/DataTree.cpp b/src/util/DataTree.cpp index e774457..6be2fc5 100755 --- a/src/util/DataTree.cpp +++ b/src/util/DataTree.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include /* DataElement class */ @@ -113,11 +113,13 @@ void DataElement::set(const string &str_in) { void DataElement::set(const wstring &wstr_in) { data_type = DATA_WSTRING; - std::wstring_convert > utf8conv; - std::string wstrVal = utf8conv.to_bytes(wstr_in); + int maxLen = wstr_in.length()*2+1; + char *tmp_str = (char *)malloc(maxLen); + wcstombs(tmp_str, wstr_in.c_str(), maxLen); - data_init(wstrVal.length() + 1); - memcpy(data_val, wstrVal.c_str(), data_size); + data_init(strlen(tmp_str) + 1); + memcpy(data_val, tmp_str, data_size); + free(tmp_str); } void DataElement::set(vector &strvect_in) { @@ -294,16 +296,17 @@ void DataElement::get(wstring &wstr_in) { if (data_type != DATA_WSTRING) throw(new DataTypeMismatchException("Type mismatch, not a WSTRING")); - std::wstring_convert > utf8conv; - std::wstring wstrVal = utf8conv.from_bytes((char *)data_val); - if (!wstr_in.empty()) // flush the string { wstr_in.erase(wstr_in.begin(), wstr_in.end()); } if (data_val) { - wstr_in.append(wstrVal); + int maxLen = strlen(data_val)*2+1; + wchar_t *tmp_wstr = (wchar_t *)malloc(maxLen*sizeof(wchar_t)); + mbstowcs(tmp_wstr, data_val, maxLen); + wstr_in.append(tmp_wstr); + free(tmp_wstr); } } @@ -576,9 +579,15 @@ std::string trim(std::string& s, const std::string& drop = " ") { string DataTree::wsEncode(const wstring wstr) { stringstream encStream; - std::wstring_convert > utf8conv; - std::string byte_str = utf8conv.to_bytes(wstr); + int bufSize = wstr.length()*2+1; + char *data_str = (char *)malloc(bufSize); + + wcstombs(data_str, wstr.c_str(), bufSize); + + std::string byte_str(data_str); + free(data_str); + encStream << std::hex; for(auto i = byte_str.begin(); i != byte_str.end(); i++) { @@ -591,7 +600,7 @@ string DataTree::wsEncode(const wstring wstr) { wstring DataTree::wsDecode(const string str) { std::stringstream decStream; - std::stringstream utf8str; + std::stringstream mbstr; unsigned int x; string decStr = str; @@ -599,16 +608,19 @@ wstring DataTree::wsDecode(const string str) { decStream << trim(decStr); string sResult; - wstring wsResult; - - std::wstring_convert> utf8conv; + int maxLen = decStr.length(); + wchar_t *wc_str = (wchar_t *) malloc(maxLen * sizeof(wchar_t)); while (!decStream.eof()) { decStream >> std::hex >> x; - utf8str << (unsigned char) x; + mbstr << (unsigned char) x; } - return utf8conv.from_bytes(utf8str.str().c_str()); + mbstowcs(wc_str, mbstr.str().c_str(), maxLen); + + free(wc_str); + + return wstring(wc_str); } void DataTree::decodeXMLText(DataNode *elem, const char *src_text, DT_FloatingPointPolicy fpp) {