From 9a93b150f022474dd3b4e6cb4a321450297a45c8 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sat, 7 May 2022 14:55:33 +0200 Subject: [PATCH] input: properly log hid_error (strfmt wchar_t) --- Utilities/StrFmt.cpp | 19 ++++++++++++++++++- Utilities/StrFmt.h | 11 +++++++++++ Utilities/StrUtil.h | 2 -- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Utilities/StrFmt.cpp b/Utilities/StrFmt.cpp index 06c7ca9e35..d462c69974 100644 --- a/Utilities/StrFmt.cpp +++ b/Utilities/StrFmt.cpp @@ -13,27 +13,39 @@ #include #else #include +#include +#include #endif -#ifdef _WIN32 std::string wchar_to_utf8(std::wstring_view src) { +#ifdef _WIN32 std::string utf8_string; const auto tmp_size = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(), nullptr, 0, nullptr, nullptr); utf8_string.resize(tmp_size); WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(), utf8_string.data(), tmp_size, nullptr, nullptr); return utf8_string; +#else + std::wstring_convert, wchar_t> converter{}; + return converter.to_bytes(src.data()); +#endif } std::wstring utf8_to_wchar(std::string_view src) { +#ifdef _WIN32 std::wstring wchar_string; const auto tmp_size = MultiByteToWideChar(CP_UTF8, 0, src.data(), src.size(), nullptr, 0); wchar_string.resize(tmp_size); MultiByteToWideChar(CP_UTF8, 0, src.data(), src.size(), wchar_string.data(), tmp_size); return wchar_string; +#else + std::wstring_convert, wchar_t> converter{}; + return converter.from_bytes(src.data()); +#endif } +#ifdef _WIN32 std::string fmt::win_error_to_string(unsigned long error, void* module_handle) { std::string message; @@ -134,6 +146,11 @@ void fmt_class_string::format(std::string& out, u64 arg) } } +void fmt_class_string::format(std::string& out, u64 arg) +{ + out += wchar_to_utf8(reinterpret_cast(arg)); +} + template <> void fmt_class_string::format(std::string& out, u64 arg) { diff --git a/Utilities/StrFmt.h b/Utilities/StrFmt.h index 3f4f55cd88..702375717c 100644 --- a/Utilities/StrFmt.h +++ b/Utilities/StrFmt.h @@ -226,6 +226,17 @@ struct fmt_class_string : fmt_class_string { }; +template <> +struct fmt_class_string +{ + static void format(std::string& out, u64 arg); +}; + +template <> +struct fmt_class_string : fmt_class_string +{ +}; + namespace fmt { // Both uchar and std::byte are allowed diff --git a/Utilities/StrUtil.h b/Utilities/StrUtil.h index 5d63ebce2c..643bc7909b 100644 --- a/Utilities/StrUtil.h +++ b/Utilities/StrUtil.h @@ -8,10 +8,8 @@ #include "util/types.hpp" -#ifdef _WIN32 std::wstring utf8_to_wchar(std::string_view src); std::string wchar_to_utf8(std::wstring_view src); -#endif // Copy null-terminated string from a std::string or a char array to a char array with truncation template