epee.string_tools: add conversion between UTF-8 and UTF-16

This commit is contained in:
stoffu 2018-06-25 09:44:07 +09:00
parent 2329d2f4c8
commit 1d176473e9
No known key found for this signature in database
GPG key ID: 41DAB8343A9EC012
3 changed files with 63 additions and 26 deletions

View file

@ -33,6 +33,7 @@
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#ifdef WIN32 #ifdef WIN32
#include <windows.h> #include <windows.h>
#include "string_tools.h"
#endif #endif
// On Windows there is a problem with non-ASCII characters in path and file names // On Windows there is a problem with non-ASCII characters in path and file names
@ -72,11 +73,9 @@ namespace file_io_utils
bool save_string_to_file(const std::string& path_to_file, const std::string& str) bool save_string_to_file(const std::string& path_to_file, const std::string& str)
{ {
#ifdef WIN32 #ifdef WIN32
WCHAR wide_path[1000]; std::wstring wide_path;
int chars = MultiByteToWideChar(CP_UTF8, 0, path_to_file.c_str(), path_to_file.size() + 1, wide_path, 1000); try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
if (chars == 0) HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
return false;
HANDLE file_handle = CreateFileW(wide_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE) if (file_handle == INVALID_HANDLE_VALUE)
return false; return false;
DWORD bytes_written; DWORD bytes_written;
@ -131,11 +130,9 @@ namespace file_io_utils
bool load_file_to_string(const std::string& path_to_file, std::string& target_str, size_t max_size = 1000000000) bool load_file_to_string(const std::string& path_to_file, std::string& target_str, size_t max_size = 1000000000)
{ {
#ifdef WIN32 #ifdef WIN32
WCHAR wide_path[1000]; std::wstring wide_path;
int chars = MultiByteToWideChar(CP_UTF8, 0, path_to_file.c_str(), path_to_file.size() + 1, wide_path, 1000); try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
if (chars == 0) HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
return false;
HANDLE file_handle = CreateFileW(wide_path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE) if (file_handle == INVALID_HANDLE_VALUE)
return false; return false;
DWORD file_size = GetFileSize(file_handle, NULL); DWORD file_size = GetFileSize(file_handle, NULL);
@ -202,11 +199,9 @@ namespace file_io_utils
bool get_file_size(const std::string& path_to_file, uint64_t &size) bool get_file_size(const std::string& path_to_file, uint64_t &size)
{ {
#ifdef WIN32 #ifdef WIN32
WCHAR wide_path[1000]; std::wstring wide_path;
int chars = MultiByteToWideChar(CP_UTF8, 0, path_to_file.c_str(), path_to_file.size() + 1, wide_path, 1000); try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
if (chars == 0) HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
return false;
HANDLE file_handle = CreateFileW(wide_path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE) if (file_handle == INVALID_HANDLE_VALUE)
return false; return false;
LARGE_INTEGER file_size; LARGE_INTEGER file_size;

View file

@ -381,6 +381,41 @@ POP_WARNINGS
res = str.substr(0, pos); res = str.substr(0, pos);
return res; return res;
} }
//----------------------------------------------------------------------------
#ifdef _WIN32
inline std::wstring utf8_to_utf16(const std::string& str)
{
if (str.empty())
return {};
int wstr_size = MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), NULL, 0);
if (wstr_size == 0)
{
throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
}
std::wstring wstr(wstr_size, wchar_t{});
if (!MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), &wstr[0], wstr_size))
{
throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
}
return wstr;
}
inline std::string utf16_to_utf8(const std::wstring& wstr)
{
if (wstr.empty())
return {};
int str_size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), NULL, 0, NULL, NULL);
if (str_size == 0)
{
throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
}
std::string str(str_size, char{});
if (!WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &str[0], str_size, NULL, NULL))
{
throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
}
return str;
}
#endif
} }
} }
#endif //_STRING_TOOLS_H_ #endif //_STRING_TOOLS_H_

View file

@ -451,10 +451,15 @@ std::string get_nix_version_display_string()
if (SHGetSpecialFolderPathW(NULL, psz_path, nfolder, iscreate)) if (SHGetSpecialFolderPathW(NULL, psz_path, nfolder, iscreate))
{ {
int size_needed = WideCharToMultiByte(CP_UTF8, 0, psz_path, wcslen(psz_path), NULL, 0, NULL, NULL); try
std::string folder_name(size_needed, 0); {
WideCharToMultiByte(CP_UTF8, 0, psz_path, wcslen(psz_path), &folder_name[0], size_needed, NULL, NULL); return string_tools::utf16_to_utf8(psz_path);
return folder_name; }
catch (const std::exception &e)
{
MERROR("utf16_to_utf8 failed: " << e.what());
return "";
}
} }
LOG_ERROR("SHGetSpecialFolderPathW() failed, could not obtain requested path."); LOG_ERROR("SHGetSpecialFolderPathW() failed, could not obtain requested path.");
@ -515,18 +520,20 @@ std::string get_nix_version_display_string()
int code; int code;
#if defined(WIN32) #if defined(WIN32)
// Maximizing chances for success // Maximizing chances for success
WCHAR wide_replacement_name[1000]; std::wstring wide_replacement_name;
MultiByteToWideChar(CP_UTF8, 0, replacement_name.c_str(), replacement_name.size() + 1, wide_replacement_name, 1000); try { wide_replacement_name = string_tools::utf8_to_utf16(replacement_name); }
WCHAR wide_replaced_name[1000]; catch (...) { return std::error_code(GetLastError(), std::system_category()); }
MultiByteToWideChar(CP_UTF8, 0, replaced_name.c_str(), replaced_name.size() + 1, wide_replaced_name, 1000); std::wstring wide_replaced_name;
try { wide_replaced_name = string_tools::utf8_to_utf16(replaced_name); }
catch (...) { return std::error_code(GetLastError(), std::system_category()); }
DWORD attributes = ::GetFileAttributesW(wide_replaced_name); DWORD attributes = ::GetFileAttributesW(wide_replaced_name.c_str());
if (INVALID_FILE_ATTRIBUTES != attributes) if (INVALID_FILE_ATTRIBUTES != attributes)
{ {
::SetFileAttributesW(wide_replaced_name, attributes & (~FILE_ATTRIBUTE_READONLY)); ::SetFileAttributesW(wide_replaced_name.c_str(), attributes & (~FILE_ATTRIBUTE_READONLY));
} }
bool ok = 0 != ::MoveFileExW(wide_replacement_name, wide_replaced_name, MOVEFILE_REPLACE_EXISTING); bool ok = 0 != ::MoveFileExW(wide_replacement_name.c_str(), wide_replaced_name.c_str(), MOVEFILE_REPLACE_EXISTING);
code = ok ? 0 : static_cast<int>(::GetLastError()); code = ok ? 0 : static_cast<int>(::GetLastError());
#else #else
bool ok = 0 == std::rename(replacement_name.c_str(), replaced_name.c_str()); bool ok = 0 == std::rename(replacement_name.c_str(), replaced_name.c_str());