[セットトップ]一般的な文字列処理クラスは、GBK、UTF 8、UNICODの3種類の前に変換されます.
4851 ワード
#pragma once
#include <windows.h>
#include <string>
class CharsetConvertor
{
public:
CharsetConvertor()
{
iText = NULL;
iOutput = NULL;
}
CharsetConvertor(const CharsetConvertor& aOther)
{
*this = aOther;
}
~CharsetConvertor()
{
delete[] iText;
iText = NULL;
delete[] iOutput;
iOutput = NULL;
}
CharsetConvertor& operator=(const CharsetConvertor& aOther)
{
delete[] iText;
iText = NULL;
delete[] iOutput;
iOutput = NULL;
int nTmpLen = wcslen(aOther.iText) + 1;
iText = new wchar_t[nTmpLen];
ZeroMemory(iText,nTmpLen*sizeof(wchar_t));
wcsncpy_s(iText, nTmpLen, aOther.iText, nTmpLen);
nTmpLen = strlen(aOther.iOutput) + 1;
iOutput = new char[nTmpLen];
ZeroMemory(iOutput,nTmpLen*sizeof(char));
strncpy_s(iOutput, nTmpLen, aOther.iOutput, nTmpLen);
return *this;
}
bool SetGbk(const char* aText)
{
if (aText == NULL)
aText = "";
return PCharToWChar(aText, CP_ACP);
}
bool SetUtf8(const char* aText)
{
if (aText == NULL)
aText = "";
return PCharToWChar(aText, CP_UTF8);
}
bool SetUnicode(const wchar_t* aText)
{
bool ret = false;
if (aText == NULL)
aText = L"";
delete[] iText;
iText = NULL;
iText = new wchar_t[wcslen(aText) + 1];
if (iText != NULL)
{
wcscpy_s(iText, wcslen(aText) + 1, aText);
ret = true;
}
else
{
ret = false;
}
return ret;
}
const char* GetGbk()
{
const char* ret = NULL;
if (WCharToPChar(CP_ACP))
ret = iOutput;
else
ret = NULL;
return ret;
}
const char* GetUtf8()
{
const char* ret = NULL;
if (WCharToPChar(CP_UTF8))
ret = iOutput;
else
ret = NULL;
return ret;
}
const wchar_t* GetUnicode()
{
return iText;
}
static std::wstring GbkToUnicode(const char* aText)
{
CharsetConvertor charsetConvertor;
charsetConvertor.SetGbk(aText);
return charsetConvertor.GetUnicode();
}
static std::string UnicodeToGbk(const wchar_t* aText)
{
CharsetConvertor charsetConvertor;
charsetConvertor.SetUnicode(aText);
return charsetConvertor.GetGbk();
}
static std::wstring Utf8ToUnicode(const char* aText)
{
CharsetConvertor charsetConvertor;
charsetConvertor.SetUtf8(aText);
return charsetConvertor.GetUnicode();
}
static std::string UnicodeToUtf8(const wchar_t* aText)
{
CharsetConvertor charsetConvertor;
charsetConvertor.SetUnicode(aText);
return charsetConvertor.GetUtf8();
}
static std::string GbkToUtf8(const char* aText)
{
CharsetConvertor charsetConvertor;
charsetConvertor.SetGbk(aText);
return charsetConvertor.GetUtf8();
}
static std::string Utf8ToGbk(const char* aText)
{
CharsetConvertor charsetConvertor;
charsetConvertor.SetUtf8(aText);
return charsetConvertor.GetGbk();
}
protected:
wchar_t* iText;
char* iOutput;
bool PCharToWChar(const char* aText, unsigned int aCodePage)
{
bool ret = false;
int length = 0;
try
{
if (aText == NULL)
aText = "";
if (strlen(aText) == 0)
{
delete[] iText;
iText = NULL;
iText = new wchar_t[1];
iText[0] = L'\0';
}
else
{
length = MultiByteToWideChar(
aCodePage,
0,
aText,
strlen(aText),
NULL,
0);
if (length == 0)
throw -1;
delete[] iText;
iText = NULL;
iText = new wchar_t[length + 1];
length = MultiByteToWideChar(
aCodePage,
0,
aText,
strlen(aText),
iText,
length + 1);
if (length == 0)
throw -1;
iText[length] = L'\0';
}
ret = true;
}
catch (...)
{
ret = false;
}
return ret;
}
bool WCharToPChar(unsigned int aCodePage)
{
bool ret = false;
int length = 0;
try
{
if (wcslen(iText) == 0)
{
delete[] iOutput;
iOutput = NULL;
iOutput = new char[1];
iOutput[0] = '\0';
}
else
{
length = WideCharToMultiByte(
aCodePage,
0,
iText,
wcslen(iText),
NULL,
0,
NULL,
NULL);
if (length == 0)
throw -1;
delete[] iOutput;
iOutput = NULL;
iOutput = new char[length + 1];
length = WideCharToMultiByte(
aCodePage,
0,
iText,
wcslen(iText),
iOutput,
length + 1,
NULL,
NULL);
if (length == 0)
throw -1;
iOutput[length] = '\0';
}
ret = true;
}
catch (...)
{
ret = false;
}
return ret;
}
};
//
#define UniToGbkEx(sz) CharsetConvertor::UnicodeToGbk(sz).c_str()
#define UniToUtf8Ex(sz) CharsetConvertor::UnicodeToUtf8(sz).c_str()
#define GbkToUniEx(sz) CharsetConvertor::GbkToUnicode(sz).c_str()
#define GbkToUtf8Ex(sz) CharsetConvertor::GbkToUtf8(sz).c_str()
#define Utf8ToUniEx(sz) CharsetConvertor::Utf8ToUnicode(sz).c_str()
#define Utf8ToGbkEx(sz) CharsetConvertor::Utf8ToGbk(sz).c_str()