From e8db619e631a5791fb6cd259754d1ad223504f48 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 8 Aug 2018 15:55:35 +0200 Subject: [PATCH] test --- src/core/utils.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++-- src/core/utils.h | 9 +++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/core/utils.cpp b/src/core/utils.cpp index da33ada..95930c0 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -13,9 +13,52 @@ std::string formatCentAsEuroString(const int cent, int width) currStream << std::right << std::setw(width) << std::showbase << std::put_money(cent, false); } catch (std::runtime_error& err) { - currStream << std::fixed << std::setw(width >= 4 ? width - 4 : width) << std::setprecision(2) << cent / 100.0L - << " €"; + currStream << std::fixed << std::setw(width >= 4 ? width - 4 : width) + << std::setprecision(2) << cent / 100.0L << " €"; } return currStream.str(); } + +#ifdef _WIN32 +// UTF-16 -> UTF-8 conversion +const std::string convert_to_utf8(const std::wstring& utf16_string) +{ + // get length + int length = + WideCharToMultiByte(CP_UTF8, 0, utf16_string.c_str(), static_cast(utf16_string.size()), + nullptr, 0, nullptr, nullptr); + if (!(length > 0)) + return std::string(); + else { + string result; + result.resize(static_cast(length)); + + if (WideCharToMultiByte(CP_UTF8, 0, utf16_string.c_str(), + static_cast(utf16_string.size()), &result[0], + static_cast(result.size()), nullptr, nullptr) == 0) + throw error("Failure to execute convert_to_utf8: call to WideCharToMultiByte failed."); + else + return result; + } +} +// UTF-8 -> UTF-16 conversion +const std::wstring convert_to_utf16(const std::string& utf8_string) +{ + // get length + int length = MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), static_cast(utf8_string.size()), nullptr, 0); + if(!(length > 0)) + return std::wstring(); + else + { + wstring result; + result.resize(static_cast(length)); + + if(MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), static_cast(utf8_string.size()), + &result[0], static_cast(result.size())) == 0 ) + throw error("Failure to execute convert_to_utf16: call to MultiByteToWideChar failed."); + else + return result; + } +} +#endif \ No newline at end of file diff --git a/src/core/utils.h b/src/core/utils.h index a2adb20..565a257 100644 --- a/src/core/utils.h +++ b/src/core/utils.h @@ -3,6 +3,15 @@ #include +#ifdef _WIN32 +#include +#endif + std::string formatCentAsEuroString(const int cent, int width = 10); +#ifdef __WIN32 +const std::string convert_to_utf8(const std::wstring& utf16_string); +const std::wstring convert_to_utf16(const std::string& utf8_string); +#endif + #endif \ No newline at end of file