GCC missing codecvt support

This commit is contained in:
Charles J. Cliffe 2016-06-12 23:36:38 -04:00
parent 61d67e69d6
commit 8b3fba02cc

View File

@ -28,7 +28,7 @@
#include <math.h>
#include <iomanip>
#include <locale>
#include <codecvt>
#include <stdlib.h>
/* 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<std::codecvt_utf8<wchar_t> > 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<string> &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<std::codecvt_utf8<wchar_t> > 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<std::codecvt_utf8<wchar_t> > 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<std::codecvt_utf8<wchar_t>> 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) {