This commit is contained in:
Martin Brodbeck 2018-08-09 08:31:38 +02:00
parent e8db619e63
commit fe6b858f32
3 changed files with 10 additions and 48 deletions

View File

@ -2,6 +2,7 @@
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <codecvt>
std::string formatCentAsEuroString(const int cent, int width) std::string formatCentAsEuroString(const int cent, int width)
{ {
@ -21,44 +22,9 @@ std::string formatCentAsEuroString(const int cent, int width)
} }
#ifdef _WIN32 #ifdef _WIN32
// UTF-16 -> UTF-8 conversion std::string convertFromUtf16ToUtf8(std::u16string& utf16String)
const std::string convert_to_utf8(const std::wstring& utf16_string)
{ {
// get length std::string u8_conv = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(utf16String);
int length = return u8_conv;
WideCharToMultiByte(CP_UTF8, 0, utf16_string.c_str(), static_cast<int>(utf16_string.size()),
nullptr, 0, nullptr, nullptr);
if (!(length > 0))
return std::string();
else {
string result;
result.resize(static_cast<std::string::size_type>(length));
if (WideCharToMultiByte(CP_UTF8, 0, utf16_string.c_str(),
static_cast<int>(utf16_string.size()), &result[0],
static_cast<int>(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 #endif
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<int>(utf8_string.size()), nullptr, 0);
if(!(length > 0))
return std::wstring();
else
{
wstring result;
result.resize(static_cast<std::string::size_type>(length));
if(MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), static_cast<int>(utf8_string.size()),
&result[0], static_cast<int>(result.size())) == 0 )
throw error("Failure to execute convert_to_utf16: call to MultiByteToWideChar failed.");
else
return result;
}
}
#endif

View File

@ -2,16 +2,12 @@
#define UTILS_H #define UTILS_H
#include <string> #include <string>
#include <locale>
#ifdef _WIN32
#include <windows.h>
#endif
std::string formatCentAsEuroString(const int cent, int width = 10); std::string formatCentAsEuroString(const int cent, int width = 10);
#ifdef __WIN32 #ifdef __WIN32
const std::string convert_to_utf8(const std::wstring& utf16_string); std::string convertFromUtf16ToUtf8(std::u16string& utf16String);
const std::wstring convert_to_utf16(const std::string& utf8_string);
#endif #endif
#endif #endif

View File

@ -376,8 +376,8 @@ void MainWindow::onImportSellerExcelActionTriggered()
if (filename.isEmpty()) if (filename.isEmpty())
return; return;
std::u16string fname16 = filename.toStdU16String();
ExcelReader::readSellersFromFile(filename.toStdString(), marketplace_.get()); ExcelReader::readSellersFromFile(convertFromUtf16ToUtf8(fname16), marketplace_.get());
} }
void MainWindow::onImportSellerJsonActionTriggered() void MainWindow::onImportSellerJsonActionTriggered()