kima2/src/core/utils.cpp

64 lines
2.1 KiB
C++
Raw Normal View History

#include "utils.h"
#include <iomanip>
#include <numeric>
std::string formatCentAsEuroString(const int cent, int width)
{
std::stringstream currStream;
try {
std::locale myLocale("de_DE.utf8");
currStream.imbue(myLocale);
currStream << std::right << std::setw(width) << std::showbase
<< std::put_money(cent, false);
} catch (std::runtime_error& err) {
2018-08-08 15:55:35 +02:00
currStream << std::fixed << std::setw(width >= 4 ? width - 4 : width)
<< std::setprecision(2) << cent / 100.0L << "";
}
return currStream.str();
2018-08-08 09:25:37 +02:00
}
2018-08-08 15:55:35 +02:00
#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<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
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