Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace std::codecvt by MultiByteToWideChar due to deprecation #29

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions source/utils/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// @author: see AUTHORS file

#include <algorithm>
#include <cassert>
#include <fstream>
#include <iterator>
#include <sstream>
Expand Down Expand Up @@ -230,13 +231,14 @@ const std::string &path::string() const
std::wstring path::wstring() const
{
const std::string &path_str = string();
int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path_str.c_str(), static_cast<int>(path_str.length()), nullptr, 0);
const int allocated_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path_str.c_str(), static_cast<int>(path_str.length()), nullptr, 0);

if (size > 0)
if (allocated_size > 0)
{
std::wstring path_converted(size, L'\0');
std::wstring path_converted(allocated_size, L'\0');
// Note: const_cast is NOT necessary in C++17 and newer!
size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path_str.c_str(), static_cast<int>(path_str.length()), const_cast<wchar_t *>(path_converted.data()), size);
const int actual_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path_str.c_str(), static_cast<int>(path_str.length()), &path_converted.at(0), allocated_size);
m7913d marked this conversation as resolved.
Show resolved Hide resolved
assert(allocated_size == actual_size); // unless a serious error happened, this MUST always be true!
return path_converted;
}
else
Expand Down