51 lines
1.3 KiB
C++
51 lines
1.3 KiB
C++
|
#include "HGString.h"
|
|||
|
#include "base/HGDef.h"
|
|||
|
#include "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 std::string& utf8Str)
|
|||
|
{
|
|||
|
#if defined(HG_CMP_MSC)
|
|||
|
return Utf8ToAnsi(utf8Str.c_str());
|
|||
|
#else
|
|||
|
return utf8Str;
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
std::string StdStringToUtf8(const std::string& stdStr)
|
|||
|
{
|
|||
|
#if defined(HG_CMP_MSC)
|
|||
|
return AnsiToUtf8(stdStr.c_str());
|
|||
|
#else
|
|||
|
return stdStr;
|
|||
|
#endif
|
|||
|
}
|