diff --git a/rwcore/CMakeLists.txt b/rwcore/CMakeLists.txt index fda9e65d..95a2c35e 100644 --- a/rwcore/CMakeLists.txt +++ b/rwcore/CMakeLists.txt @@ -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}" diff --git a/rwcore/platform/RWWindows.cpp b/rwcore/platform/RWWindows.cpp new file mode 100644 index 00000000..f8a68cc6 --- /dev/null +++ b/rwcore/platform/RWWindows.cpp @@ -0,0 +1,30 @@ +#include "RWWindows.hpp" + +#include "rw/Debug.hpp" + +#define WIN32_LEAN_AND_MEAN +#include + +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; +} diff --git a/rwcore/platform/RWWindows.hpp b/rwcore/platform/RWWindows.hpp new file mode 100644 index 00000000..a28e8c9d --- /dev/null +++ b/rwcore/platform/RWWindows.hpp @@ -0,0 +1,14 @@ +#ifndef _RWCORE_PLATFORM_RWWINDOWS_HPP_ +#define _RWCORE_PLATFORM_RWWINDOWS_HPP_ + +#include + +/** + * @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