diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 95930c0..f505cfd 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -2,6 +2,7 @@ #include #include +#include std::string formatCentAsEuroString(const int cent, int width) { @@ -21,44 +22,9 @@ std::string formatCentAsEuroString(const int cent, int width) } #ifdef _WIN32 -// UTF-16 -> UTF-8 conversion -const std::string convert_to_utf8(const std::wstring& utf16_string) +std::string convertFromUtf16ToUtf8(std::u16string& utf16String) { - // 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; - } + std::string u8_conv = std::wstring_convert, char16_t>{}.to_bytes(utf16String); + return u8_conv; } -// 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 +#endif diff --git a/src/core/utils.h b/src/core/utils.h index 565a257..d3e8e62 100644 --- a/src/core/utils.h +++ b/src/core/utils.h @@ -2,16 +2,12 @@ #define UTILS_H #include - -#ifdef _WIN32 -#include -#endif +#include 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); +std::string convertFromUtf16ToUtf8(std::u16string& utf16String); #endif -#endif \ No newline at end of file +#endif diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 29f4efd..5fe2a88 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -376,8 +376,8 @@ void MainWindow::onImportSellerExcelActionTriggered() if (filename.isEmpty()) return; - - ExcelReader::readSellersFromFile(filename.toStdString(), marketplace_.get()); + std::u16string fname16 = filename.toStdU16String(); + ExcelReader::readSellersFromFile(convertFromUtf16ToUtf8(fname16), marketplace_.get()); } void MainWindow::onImportSellerJsonActionTriggered()