From c6cc59b5bc1b1beacde4c1381ec4ffd0ee51c614 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Fri, 20 Oct 2023 11:04:54 +0200 Subject: [PATCH] Remove usage of deprecated `std::codecvt_utf8_utf16`. `std::codecvt_utf8_utf16` is deprecated in c++17. We replace it with the usage of windows' `MultiByteToWideChar` function. The new `converter` method is taken from libzim's `FS::toWideChar` in `src/fs_windows.cpp` with the only difference to return `std::wstring` instead of `std::unique_ptr`. --- src/zimdump.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/zimdump.cpp b/src/zimdump.cpp index 0dd75240..8095d264 100644 --- a/src/zimdump.cpp +++ b/src/zimdump.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include "version.h" @@ -49,7 +48,22 @@ #define ERRORSDIR "_exceptions/" -std::wstring_convert> converter; + +#ifdef _WIN32 +std::wstring utf8ToUtf16(const std::string& string) { + auto size = MultiByteToWideChar(CP_UTF8, 0, + path.c_str(), -1, nullptr, 0); + auto wdata = std::wstring(size, 0); + auto ret = MultiByteToWideChar(CP_UTF8, 0, + path.c_str(), -1, wdata.get(), size); + if (0 == ret) { + std::ostringstream oss; + oss << "Cannot convert string to wchar : " << GetLastError(); + throw std::runtime_error(oss.str()); + } + return wdata; +} +#endif inline static void createdir(const std::string &path, const std::string &base) { @@ -59,7 +73,7 @@ inline static void createdir(const std::string &path, const std::string &base) if (position != std::string::npos) { std::string fulldir = base + SEPARATOR + path.substr(0, position); #if defined(_WIN32) - std::wstring wfulldir = converter.from_bytes(fulldir); + std::wstring wfulldir = utf8ToUtf16(fulldir); CreateDirectoryW(wfulldir.c_str(), NULL); #else ::mkdir(fulldir.c_str(), 0777); @@ -220,7 +234,7 @@ void write_to_error_directory(const std::string& base, const std::string relpath #ifdef _WIN32 auto fullpath = std::string(base + ERRORSDIR + url); - std::wstring wpath = converter.from_bytes(fullpath); + std::wstring wpath = utf8ToUtf16(fullpath); auto fd = _wopen(wpath.c_str(), _O_WRONLY | _O_CREAT | _O_TRUNC, S_IWRITE); if (fd == -1) { @@ -249,7 +263,7 @@ void write_to_error_directory(const std::string& base, const std::string relpath inline void write_to_file(const std::string &base, const std::string& path, const char* data, ssize_t size) { std::string fullpath = base + path; #ifdef _WIN32 - std::wstring wpath = converter.from_bytes(fullpath); + std::wstring wpath = utf8ToUtf16(fullpath); auto fd = _wopen(wpath.c_str(), _O_WRONLY | _O_CREAT | _O_TRUNC, S_IWRITE); #else auto fd = open(fullpath.c_str(), O_WRONLY | O_CREAT | O_TRUNC, @@ -275,7 +289,7 @@ void ZimDumper::dumpFiles(const std::string& directory, bool symlinkdump, std::f { unsigned int truncatedFiles = 0; #if defined(_WIN32) - std::wstring wdir = converter.from_bytes(directory); + std::wstring wdir = utf8ToUtf16(directory); CreateDirectoryW(wdir.c_str(), NULL); #else ::mkdir(directory.c_str(), 0777);