Skip to content

Commit

Permalink
Correct use and convert unicode from utf8
Browse files Browse the repository at this point in the history
Move StringUtf8ToWideChar and StringWideCharToUtf8 to CCWinRTUtils(
Replace CCUtf8ToUnicode)
Use StringUtf8ToWideChar for convert utf8 to unicode
Use StringWideCharToUtf8 for convert unicode to utf8
  • Loading branch information
perminovVS committed Aug 16, 2015
1 parent 487c99e commit d70a21a
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 224 deletions.
47 changes: 0 additions & 47 deletions cocos/audio/winrt/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,53 +472,6 @@ bool Audio::IsSoundEffectStarted(unsigned int sound)
return m_soundEffects[sound].m_soundEffectStarted;
}

std::wstring CCUtf8ToUnicode(const char * pszUtf8Str)
{
std::wstring ret;
do
{
if (! pszUtf8Str) break;
size_t len = strlen(pszUtf8Str);
if (len <= 0) break;
++len;
wchar_t * pwszStr = new wchar_t[len];
if (! pwszStr) break;
pwszStr[len - 1] = 0;
MultiByteToWideChar(CP_UTF8, 0, pszUtf8Str, len, pwszStr, len);
ret = pwszStr;

if(pwszStr) {
delete[] (pwszStr);
(pwszStr) = 0;
}


} while (0);
return ret;
}

std::string CCUnicodeToUtf8(const wchar_t* pwszStr)
{
std::string ret;
do
{
if(! pwszStr) break;
size_t len = wcslen(pwszStr);
if (len <= 0) break;

char * pszUtf8Str = new char[len*3 + 1];
WideCharToMultiByte(CP_UTF8, 0, pwszStr, len+1, pszUtf8Str, len*3 + 1, 0, 0);
ret = pszUtf8Str;

if(pszUtf8Str) {
delete[] (pszUtf8Str);
(pszUtf8Str) = 0;
}
}while(0);

return ret;
}

void Audio::PreloadSoundEffect(const char* pszFilePath, bool isMusic)
{
if (m_engineExperiencedCriticalError) {
Expand Down
2 changes: 0 additions & 2 deletions cocos/audio/winrt/AudioCachePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ void AudioCache::readDataTask()
return;
}

std::wstring path(_fileFullPath.begin(), _fileFullPath.end());

if (nullptr != _srcReader) {
delete _srcReader;
_srcReader = nullptr;
Expand Down
9 changes: 5 additions & 4 deletions cocos/audio/winrt/AudioSourceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ bool WAVReader::initialize(const std::string& filePath)
flushChunks();

_streamer = ref new MediaStreamer;
_streamer->Initialize(std::wstring(_filePath.begin(), _filePath.end()).c_str(), true);

_streamer->Initialize(StringUtf8ToWideChar(_filePath).c_str(), true);
_wfx = _streamer->GetOutputWaveFormatEx();
UINT32 dataSize = _streamer->GetMaxStreamLengthInBytes();

Expand Down Expand Up @@ -204,7 +205,7 @@ bool MP3Reader::initialize(const std::string& filePath)
ComPtr<IMFSourceReader> pReader;
ComPtr<IMFMediaType> ppDecomprsdAudioType;

if (FAILED(hr = MFCreateSourceReaderFromURL(std::wstring(_filePath.begin(), _filePath.end()).c_str(), NULL, &pReader))) {
if (FAILED(hr = MFCreateSourceReaderFromURL(StringUtf8ToWideChar(_filePath).c_str(), NULL, &pReader))) {
break;
}

Expand Down Expand Up @@ -483,7 +484,7 @@ void MP3Reader::readFromMappedWavFile(BYTE *data, size_t offset, int size, UINT
} while (false);
}

Wrappers::FileHandle MP3Reader::openFile(const std::string& path, bool append)
Wrappers::FileHandle MP3Reader::openFile(const std::string& filePath, bool append)
{
CREATEFILE2_EXTENDED_PARAMETERS extParams = { 0 };
extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
Expand All @@ -495,7 +496,7 @@ Wrappers::FileHandle MP3Reader::openFile(const std::string& path, bool append)

DWORD access = append ? GENERIC_WRITE : GENERIC_READ;
DWORD creation = append ? OPEN_ALWAYS : OPEN_EXISTING;
return Microsoft::WRL::Wrappers::FileHandle(CreateFile2(std::wstring(path.begin(), path.end()).c_str(), access, FILE_SHARE_READ, creation, &extParams));
return Microsoft::WRL::Wrappers::FileHandle(CreateFile2(StringUtf8ToWideChar(filePath).c_str(), access, FILE_SHARE_READ, creation, &extParams));
}


Expand Down
2 changes: 1 addition & 1 deletion cocos/platform/winrt/CCApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const char * Application::getCurrentLanguageCode()
result = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, pwszLanguagesBuffer, &cchLanguagesBuffer);
if (result) {

code = CCUnicodeToUtf8(pwszLanguagesBuffer);
code = StringWideCharToUtf8(pwszLanguagesBuffer);
}

if (pwszLanguagesBuffer)
Expand Down
6 changes: 3 additions & 3 deletions cocos/platform/winrt/CCCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ NS_CC_BEGIN

void MessageBox(const char * pszMsg, const char * pszTitle)
{
// Create the message dialog and set its content
Platform::String^ message = ref new Platform::String(CCUtf8ToUnicode(pszMsg, -1).c_str());
Platform::String^ title = ref new Platform::String(CCUtf8ToUnicode(pszTitle, -1).c_str());
#ifndef WP8_SHADER_COMPILER
// Create the message dialog and set its content
Platform::String^ message = PlatformStringFromString(pszMsg);
Platform::String^ title = PlatformStringFromString(pszTitle);
GLViewImpl::sharedOpenGLView()->ShowMessageBox(title, message);
#endif
}
Expand Down
150 changes: 32 additions & 118 deletions cocos/platform/winrt/CCFileUtilsWinRT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,55 +47,6 @@ static inline std::string convertPathFormatToUnixStyle(const std::string& path)
return ret;
}

static std::wstring StringUtf8ToWideChar(const std::string& strUtf8)
{
std::wstring ret;
if (!strUtf8.empty())
{
int nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, nullptr, 0);
if (nNum)
{
WCHAR* wideCharString = new WCHAR[nNum + 1];
wideCharString[0] = 0;

nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, wideCharString, nNum + 1);

ret = wideCharString;
delete[] wideCharString;
}
else
{
CCLOG("Wrong convert to WideChar code:0x%x", GetLastError());
}
}
return ret;
}

static std::string StringWideCharToUtf8(const std::wstring& strWideChar)
{
std::string ret;
if (!strWideChar.empty())
{
int nNum = WideCharToMultiByte(CP_UTF8, 0, strWideChar.c_str(), -1, nullptr, 0, nullptr, FALSE);
if (nNum)
{
char* utf8String = new char[nNum + 1];
utf8String[0] = 0;

nNum = WideCharToMultiByte(CP_UTF8, 0, strWideChar.c_str(), -1, utf8String, nNum + 1, nullptr, FALSE);

ret = utf8String;
delete[] utf8String;
}
else
{
CCLOG("Wrong convert to Utf8 code:0x%x", GetLastError());
}
}

return ret;
}

static std::string UTF8StringToMultiByte(const std::string& strUtf8)
{
std::string ret;
Expand Down Expand Up @@ -244,17 +195,16 @@ bool CCFileUtilsWinRT::createDirectory(const std::string& path)
}

WIN32_FILE_ATTRIBUTE_DATA wfad;
std::wstring wpath(path.begin(), path.end());
if (!(GetFileAttributesEx(wpath.c_str(), GetFileExInfoStandard, &wfad)))

if (!(GetFileAttributesEx(StringUtf8ToWideChar(path).c_str(), GetFileExInfoStandard, &wfad)))
{
subpath = "";
for (unsigned int i = 0; i < dirs.size(); ++i)
{
subpath += dirs[i];
if (i > 0 && !isDirectoryExist(subpath))
{
std::wstring wsubpath(subpath.begin(), subpath.end());
BOOL ret = CreateDirectory(wsubpath.c_str(), NULL);
BOOL ret = CreateDirectory(StringUtf8ToWideChar(subpath).c_str(), NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
Expand All @@ -267,7 +217,7 @@ bool CCFileUtilsWinRT::createDirectory(const std::string& path)

bool CCFileUtilsWinRT::removeDirectory(const std::string& path)
{
std::wstring wpath = std::wstring(path.begin(), path.end());
std::wstring wpath = StringUtf8ToWideChar(path);
std::wstring files = wpath + L"*.*";
WIN32_FIND_DATA wfd;
HANDLE search = FindFirstFileEx(files.c_str(), FindExInfoStandard, &wfd, FindExSearchNameMatch, NULL, 0);
Expand Down Expand Up @@ -316,102 +266,66 @@ bool CCFileUtilsWinRT::isAbsolutePath(const std::string& strPath) const

bool CCFileUtilsWinRT::removeFile(const std::string &path)
{
std::wstring wpath(path.begin(), path.end());
std::wstring wpath = StringUtf8ToWideChar(path);
if (DeleteFile(wpath.c_str()))
{
return true;
}
return false;
else
{
CCLOG("Remove file failed with error: %d", GetLastError());
return false;
}
}

bool CCFileUtilsWinRT::renameFile(const std::string &oldfullpath, const std::string& newfullpath)
{
CCASSERT(!oldfullpath.empty(), "Invalid path");
CCASSERT(!newfullpath.empty(), "Invalid path");

std::wstring oldfile(oldfullpath.begin(), oldfullpath.end());
std::wstring newfile(newfullpath.begin(), newfullpath.end());

if (MoveFileEx(oldfile.c_str(), newfile.c_str(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH))
{
return true;
}
CCLOG("Rename failed with error: %d", GetLastError());
return false;
}

bool CCFileUtilsWinRT::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
{
CCASSERT(!path.empty(), "Invalid path");
std::string oldPath = path + oldname;
std::string newPath = path + name;

std::regex pat("\\/");
std::string _old = std::regex_replace(oldPath, pat, "\\");
std::string _new = std::regex_replace(newPath, pat, "\\");
return renameFile(_old, _new);
}
std::string _oldfullpath = std::regex_replace(oldfullpath, pat, "\\");
std::string _newfullpath = std::regex_replace(newfullpath, pat, "\\");

static Data getData(const std::string& filename, bool forString)
{
if (filename.empty())
{
CCASSERT(!filename.empty(), "Invalid filename!");
}

Data ret;
unsigned char* buffer = nullptr;
ssize_t size = 0;
const char* mode = nullptr;
mode = "rb";
std::wstring _wNewfullpath = StringUtf8ToWideChar(_newfullpath);

do
if (FileUtils::getInstance()->isFileExist(_newfullpath))
{
// Read the file from hardware
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
FILE *fp = fopen(fullPath.c_str(), mode);
CC_BREAK_IF(!fp);
fseek(fp,0,SEEK_END);
size = ftell(fp);
fseek(fp,0,SEEK_SET);

if (forString)
{
buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));
buffer[size] = '\0';
}
else
if (!DeleteFile(_wNewfullpath.c_str()))
{
buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);
CCLOGERROR("Fail to delete file %s !Error code is 0x%x", newfullpath.c_str(), GetLastError());
}
}

size = fread(buffer, sizeof(unsigned char), size, fp);
fclose(fp);
} while (0);

if (nullptr == buffer || 0 == size)
if (MoveFileEx(StringUtf8ToWideChar(_oldfullpath).c_str(), _wNewfullpath.c_str(),
MOVEFILE_REPLACE_EXISTING & MOVEFILE_WRITE_THROUGH))
{
std::string msg = "Get data from file(";
msg.append(filename).append(") failed!");
CCLOG("%s", msg.c_str());
return true;
}
else
{
ret.fastSet(buffer, size);
CCLOGERROR("Fail to rename file %s to %s !Error code is 0x%x", oldfullpath.c_str(), newfullpath.c_str(), GetLastError());
return false;
}
}

return ret;
bool CCFileUtilsWinRT::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
{
CCASSERT(!path.empty(), "Invalid path");
std::string oldPath = path + oldname;
std::string newPath = path + name;

return renameFile(oldPath, newPath);
}

std::string CCFileUtilsWinRT::getStringFromFile(const std::string& filename)
{
Data data = getData(filename, true);
Data data = getDataFromFile(filename);
if (data.isNull())
{
return "";
}
std::string ret((const char*)data.getBytes());
std::string ret((const char*)data.getBytes(), data.getSize());
return ret;
}

Expand Down
6 changes: 3 additions & 3 deletions cocos/platform/winrt/CCPrecompiledShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void CCPrecompiledShaders::savePrecompiledPrograms(Windows::Storage::StorageFold
for (auto iter = m_programs.begin(); iter != m_programs.end(); ++iter)
{
CompiledProgram* p = (CompiledProgram*)iter->second;
Platform::String^ keyName = ref new Platform::String(CCUtf8ToUnicode(p->key.c_str()).c_str());
Platform::String^ keyName = PlatformStringFromString(p->key);
Platform::String^ programName = SHADER_NAME_PREFIX + keyName;

dataWriter->WriteString("const unsigned char ");
Expand All @@ -225,12 +225,12 @@ void CCPrecompiledShaders::savePrecompiledPrograms(Windows::Storage::StorageFold
if(i % 8 == 0)
dataWriter->WriteString("\n");
sprintf_s(temp, "%3i, ", buffer[i]);
dataWriter->WriteString(ref new Platform::String(CCUtf8ToUnicode(temp).c_str()));
dataWriter->WriteString(PlatformStringFromString(temp));
}
if((p->length - 1) % 8 == 0)
dataWriter->WriteString("\n");
sprintf_s(temp, "%3i, ", buffer[p->length - 1]);
dataWriter->WriteString(ref new Platform::String(CCUtf8ToUnicode(temp).c_str()));
dataWriter->WriteString(PlatformStringFromString(temp));
dataWriter->WriteString("\n};\n\n");

if(numPrograms != 0)
Expand Down
Loading

0 comments on commit d70a21a

Please sign in to comment.