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 <angt@huggingface.co>
This commit is contained in:
Adrien Gallouët 2025-12-04 11:06:49 +01:00 committed by GitHub
parent 0d1324856f
commit 83c1171529
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 2 deletions

View File

@ -786,11 +786,29 @@ bool fs_validate_filename(const std::string & filename, bool allow_subdirs) {
#include <iostream> #include <iostream>
#ifdef _WIN32
static std::wstring utf8_to_wstring(const std::string & str) {
if (str.empty()) {
return std::wstring();
}
int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), NULL, 0);
if (size <= 0) {
return std::wstring();
}
std::wstring wstr(size, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstr[0], size);
return wstr;
}
#endif
// returns true if successful, false otherwise // returns true if successful, false otherwise
bool fs_create_directory_with_parents(const std::string & path) { bool fs_create_directory_with_parents(const std::string & path) {
#ifdef _WIN32 #ifdef _WIN32
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; std::wstring wpath = utf8_to_wstring(path);
std::wstring wpath = converter.from_bytes(path);
// if the path already exists, check whether it's a directory // if the path already exists, check whether it's a directory
const DWORD attributes = GetFileAttributesW(wpath.c_str()); const DWORD attributes = GetFileAttributesW(wpath.c_str());