1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-10-06 09:07:19 +02:00

rwcore: convert windows wide string to ACP (active code page)

This commit is contained in:
Anonymous Maarten 2018-08-16 19:42:58 +02:00
parent c0f11c2935
commit 22437f9f25
3 changed files with 52 additions and 0 deletions

View File

@ -44,6 +44,14 @@ add_library(rwcore
loaders/LoaderTXD.cpp
)
if(WIN32)
target_sources(rwcore
PRIVATE
platform/RWWindows.hpp
platform/RWWindows.cpp
)
endif()
target_include_directories(rwcore
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"

View File

@ -0,0 +1,30 @@
#include "RWWindows.hpp"
#include "rw/Debug.hpp"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
std::string wideStringToACP(const wchar_t* wString) {
std::string returnValue;
bool result = true;
int nbChars = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wString, -1,
nullptr, 0, nullptr, nullptr);
if (nbChars == 0) {
if (GetLastError() != ERROR_SUCCESS) {
RW_ERROR("Unable to calculate length of wide string");
return returnValue;
}
}
returnValue.resize(nbChars);
nbChars =
WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wString, -1,
&returnValue.front(), nbChars, nullptr, nullptr);
if (nbChars == 0) {
returnValue.resize(0);
return returnValue;
}
returnValue.resize(nbChars - 1);
return returnValue;
}

View File

@ -0,0 +1,14 @@
#ifndef _RWCORE_PLATFORM_RWWINDOWS_HPP_
#define _RWCORE_PLATFORM_RWWINDOWS_HPP_
#include <string>
/**
* @brief Convert a wide string to a ACP string (=active code page)
* This function converts to the active code page instead of utf8
* because Windows functions need to understand the encoding.
* @param str The wide string to convert
*/
std::string wideStringToACP(const wchar_t* str);
#endif