96 lines
2.6 KiB
C++
96 lines
2.6 KiB
C++
|
|
#pragma once
|
|||
|
|
#include <Windows.h>
|
|||
|
|
#include <locale>
|
|||
|
|
#include <codecvt>
|
|||
|
|
|
|||
|
|
#include "squirrel.h"
|
|||
|
|
#include "sqstdaux.h"
|
|||
|
|
#include "sqstdblob.h"
|
|||
|
|
#include "sqstdio.h"
|
|||
|
|
#include "sqstdmath.h"
|
|||
|
|
#include "sqstdstring.h"
|
|||
|
|
#include "sqstdsystem.h"
|
|||
|
|
|
|||
|
|
class TOOL
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
TOOL() {};
|
|||
|
|
~TOOL() {};
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
|
|||
|
|
static char* U8ToU16(const char* szU8)
|
|||
|
|
{
|
|||
|
|
//Ԥת<D4A4><D7AA><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC>Ĵ<EFBFBD>С
|
|||
|
|
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>Ҫ<EFBFBD><D2AA>'\0'<27><><EFBFBD><EFBFBD><EFBFBD>ռ䣬MultiByteToWideChar<61><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'\0'<27>ռ<EFBFBD>
|
|||
|
|
wchar_t* wszString = new wchar_t[wcsLen + 1];
|
|||
|
|
//ת<><D7AA>
|
|||
|
|
::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'\0'
|
|||
|
|
wszString[wcsLen] = '\0';
|
|||
|
|
|
|||
|
|
char* m_char;
|
|||
|
|
int len = WideCharToMultiByte(CP_ACP, 0, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
|
|||
|
|
m_char = new char[len + 1];
|
|||
|
|
WideCharToMultiByte(CP_ACP, 0, wszString, wcslen(wszString), m_char, len, NULL, NULL);
|
|||
|
|
delete[]wszString;
|
|||
|
|
m_char[len] = '\0';
|
|||
|
|
return m_char;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static std::string SquirrelU2W(const SQChar* Str)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
char* wbuffer = (char*)(Str);
|
|||
|
|
size_t len = 0;
|
|||
|
|
while (wbuffer[len] != 0 || wbuffer[len - 1] != 0)
|
|||
|
|
{
|
|||
|
|
++len;
|
|||
|
|
}
|
|||
|
|
char* cbuffer = new char[len / 2 + 1];
|
|||
|
|
int k = 0;
|
|||
|
|
for (size_t i = 0; i < len; i += 2)
|
|||
|
|
{
|
|||
|
|
cbuffer[k] = wbuffer[i];
|
|||
|
|
++k;
|
|||
|
|
}
|
|||
|
|
cbuffer[k] = '\0';
|
|||
|
|
char* Text = U8ToU16(cbuffer);
|
|||
|
|
delete[]cbuffer;
|
|||
|
|
std::string RetStr(Text);
|
|||
|
|
delete[]Text;
|
|||
|
|
return RetStr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static char* ConvertAnsiToUtf8(const char* szAnsi) {
|
|||
|
|
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ANSI<53>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD>ַ<EFBFBD><D6B7>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
int wcsLen = ::MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, NULL, 0);
|
|||
|
|
wchar_t* wszString = new wchar_t[wcsLen];
|
|||
|
|
::MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, wszString, wcsLen);
|
|||
|
|
|
|||
|
|
// <20>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>UTF-8<>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
int utf8Len = ::WideCharToMultiByte(CP_UTF8, 0, wszString, wcsLen, NULL, 0, NULL, NULL);
|
|||
|
|
char* szUtf8 = new char[utf8Len];
|
|||
|
|
::WideCharToMultiByte(CP_UTF8, 0, wszString, wcsLen, szUtf8, utf8Len, NULL, NULL);
|
|||
|
|
|
|||
|
|
delete[] wszString; // <20>ͷſ<CDB7><C5BF>ַ<EFBFBD><D6B7>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
|
|||
|
|
|
|||
|
|
szUtf8[utf8Len - 1] = '\0'; // ȷ<><C8B7><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>'\0'<27><>β
|
|||
|
|
return szUtf8;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static std::wstring charTowchar_t(std::string Str)
|
|||
|
|
{
|
|||
|
|
char* Sn = ConvertAnsiToUtf8(Str.c_str());
|
|||
|
|
std::string B(Sn);
|
|||
|
|
std::wstring Ret(B.begin(), B.end());
|
|||
|
|
delete[]Sn;
|
|||
|
|
//std::wcout << Ret << std::endl;
|
|||
|
|
return Ret;
|
|||
|
|
}
|
|||
|
|
};
|