结果...充分验证了墨菲定律(如果一件事情有可能发生,那么她一定会发生...)
关于UTF-8的介绍,有一篇文章写的还是不错,百度的解释,云里雾里,魂淡.
ansi的给力解释:
写一些转换函数:
//--------------------begin------------------------------------------------------------------
string UnicodeToUTF8(const wchar_t* str)
{
char* result;
int textlen;
textlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
result = (char *)malloc((textlen + 1)*sizeof(char));
memset(result, 0, sizeof(char)* (textlen + 1));
WideCharToMultiByte(CP_UTF8, 0, str, -1, result, textlen, NULL, NULL);
return result;
}
wchar_t * ANSIToUnicode(const char* str)
{
int textlen;
wchar_t * result;
textlen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
result = (wchar_t *)malloc((textlen + 1)*sizeof(wchar_t));
memset(result, 0, (textlen + 1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)result, textlen);
return result;
}
string ANSIToUTF8(const string str)
{
return UnicodeToUTF8(ANSIToUnicode(str.c_str()));
}
//--------------------end------------------------------------------------------------------
代码如下:
void DirectXDataManager::addResourceLocation(const std::string& _name, bool _recursive)
{
ArhivInfo info;
//将路径名字转成utf-8格式编码:
string utf8Name = ANSIToUTF8(_name);
info.name = MyGUI::UString(utf8Name).asWStr();
info.recursive = _recursive;
mPaths.push_back(info);
}
代码如下:
添加 Utf8到ANSI的转换函数:
string Utf8ToGBK(const std::string& strUTF8)//转GBK测试结果也对,不知道可不可以.以后验证了再改
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, (LPWSTR)wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, szGBK, len, NULL, NULL);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}
修改MyGUI_DirectXDataManager.cpp中getData函数:
代码如下:
IDataStream* DirectXDataManager::getData(const std::string& _name)
{
std::string filepath = getDataPath(_name);
//utf8转ansi
string relpath = Utf8ToGBK(filepath);
if (relpath.empty())//filepath
return nullptr;
std::ifstream* stream = new std::ifstream();
stream->open(relpath.c_str(), std::ios_base::binary);
if (!stream->is_open())
{
delete stream;
return nullptr;
}
DataFileStream* data = new DataFileStream(stream);
return data;
}