code_app/utility/HGString.cpp

51 lines
1.3 KiB
C++

#include "HGString.h"
#include "../modules/base/HGDef.h"
#include "../modules/base/HGInc.h"
#include <algorithm>
#if defined(HG_CMP_MSC)
static std::string AnsiToUtf8(const char* text)
{
int wlen = ::MultiByteToWideChar(CP_ACP, 0, text, -1, NULL, 0);
WCHAR* pUnicode = new WCHAR[wlen];
::MultiByteToWideChar(CP_ACP, 0, text, -1, pUnicode, wlen);
int len = ::WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, NULL, 0, NULL, NULL);
CHAR* pUTF8 = new CHAR[len];
::WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, pUTF8, len, NULL, NULL);
delete[] pUnicode;
std::string ret = pUTF8;
delete[] pUTF8;
return ret;
}
static std::string Utf8ToAnsi(const char* text)
{
int wlen = ::MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0);
WCHAR* pUnicode = new WCHAR[wlen];
::MultiByteToWideChar(CP_UTF8, 0, text, -1, pUnicode, wlen);
int len = ::WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, NULL, 0, NULL, NULL);
CHAR* pAnsi = new CHAR[len];
::WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsi, len, NULL, NULL);
delete[] pUnicode;
std::string ret = pAnsi;
delete[] pAnsi;
return ret;
}
#endif
std::string Utf8ToStdString(const char* utf8)
{
#if defined(HG_CMP_MSC)
return Utf8ToAnsi(utf8);
#else
return utf8;
#endif
}
std::string StdStringToUtf8(const char* str)
{
#if defined(HG_CMP_MSC)
return AnsiToUtf8(str);
#else
return str;
#endif
}