Skip to content

Commit 83c1171

Browse files
authored
common: use native MultiByteToWideChar (#17738)
`std::codecvt_utf8<wchar_t>` is deprecated and produces warnings: common/common.cpp:792:31: warning: 'codecvt_utf8<wchar_t>' is deprecated [-Wdeprecated-declarations] 792 | std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; | Signed-off-by: Adrien Gallouët <[email protected]>
1 parent 0d13248 commit 83c1171

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

common/common.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,11 +786,29 @@ bool fs_validate_filename(const std::string & filename, bool allow_subdirs) {
786786
#include <iostream>
787787

788788

789+
#ifdef _WIN32
790+
static std::wstring utf8_to_wstring(const std::string & str) {
791+
if (str.empty()) {
792+
return std::wstring();
793+
}
794+
795+
int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), NULL, 0);
796+
797+
if (size <= 0) {
798+
return std::wstring();
799+
}
800+
801+
std::wstring wstr(size, 0);
802+
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstr[0], size);
803+
804+
return wstr;
805+
}
806+
#endif
807+
789808
// returns true if successful, false otherwise
790809
bool fs_create_directory_with_parents(const std::string & path) {
791810
#ifdef _WIN32
792-
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
793-
std::wstring wpath = converter.from_bytes(path);
811+
std::wstring wpath = utf8_to_wstring(path);
794812

795813
// if the path already exists, check whether it's a directory
796814
const DWORD attributes = GetFileAttributesW(wpath.c_str());

0 commit comments

Comments
 (0)