mirror of
https://github.com/edubart/otclient.git
synced 2025-12-14 20:59:47 +01:00
Fix platform issues regarding charsets
* IMPORTANT: A new dependency is required, boost_locale, comes with boost 1.50.0 or later * Copying and pasting special characters should now work * Running otclient from filepaths with special characters should work now too
This commit is contained in:
@@ -24,6 +24,8 @@
|
||||
#include "format.h"
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <ctype.h>
|
||||
#include <physfs.h>
|
||||
#include <boost/locale.hpp>
|
||||
|
||||
namespace stdext {
|
||||
|
||||
@@ -67,36 +69,44 @@ uint64_t hex_to_dec(const std::string& str)
|
||||
return num;
|
||||
}
|
||||
|
||||
bool is_valid_utf8(const std::string& src)
|
||||
{
|
||||
try {
|
||||
boost::locale::conv::from_utf(src, "ISO-8859-1", boost::locale::conv::stop);
|
||||
return true;
|
||||
} catch(...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string utf8_to_latin1(const std::string& src)
|
||||
{
|
||||
auto utf8CharToLatin1 = [](const uchar *utf8, int *read) -> char {
|
||||
char c = '?';
|
||||
uchar opt1 = utf8[0];
|
||||
*read = 1;
|
||||
if(opt1 == 0xc3) {
|
||||
*read = 2;
|
||||
uchar opt2 = utf8[1];
|
||||
c = 64 + opt2;
|
||||
} else if(opt1 == 0xc2) {
|
||||
*read = 2;
|
||||
uchar opt2 = utf8[1];
|
||||
if(opt2 > 0xa1 && opt2 < 0xbb)
|
||||
c = opt2;
|
||||
} else if(opt1 < 0xc2) {
|
||||
c = opt1;
|
||||
}
|
||||
return c;
|
||||
};
|
||||
return boost::locale::conv::from_utf(src, "ISO-8859-1");
|
||||
}
|
||||
|
||||
std::string out;
|
||||
int len = src.length();
|
||||
for(int i=0; i<len;) {
|
||||
int read = 0;
|
||||
uchar *utf8char = (uchar*)&src[i];
|
||||
out += utf8CharToLatin1(utf8char, &read);
|
||||
i += read;
|
||||
}
|
||||
return out;
|
||||
std::wstring utf8_to_utf16(const std::string& src)
|
||||
{
|
||||
return boost::locale::conv::utf_to_utf<wchar_t>(src);
|
||||
}
|
||||
|
||||
std::string utf16_to_utf8(const std::wstring& src)
|
||||
{
|
||||
return boost::locale::conv::utf_to_utf<char>(src);
|
||||
}
|
||||
|
||||
std::string utf16_to_latin1(const std::wstring& src)
|
||||
{
|
||||
return boost::locale::conv::from_utf(src, "ISO-8859-1");
|
||||
}
|
||||
|
||||
std::string latin1_to_utf8(const std::string& src)
|
||||
{
|
||||
return boost::locale::conv::to_utf<char>(src, "ISO-8859-1");
|
||||
}
|
||||
|
||||
std::wstring latin1_to_utf16(const std::string& src)
|
||||
{
|
||||
return boost::locale::conv::to_utf<wchar_t>(src, "ISO-8859-1");
|
||||
}
|
||||
|
||||
void tolower(std::string& str)
|
||||
|
||||
@@ -42,7 +42,6 @@ std::string date_time_string();
|
||||
|
||||
std::string dec_to_hex(uint64_t num);
|
||||
uint64_t hex_to_dec(const std::string& str);
|
||||
std::string utf8_to_latin1(const std::string& src);
|
||||
void tolower(std::string& str);
|
||||
void toupper(std::string& str);
|
||||
void trim(std::string& str);
|
||||
@@ -53,6 +52,14 @@ bool ends_with(const std::string& str, const std::string& test);
|
||||
bool starts_with(const std::string& str, const std::string& test);
|
||||
void replace_all(std::string& str, const std::string& search, const std::string& replacement);
|
||||
|
||||
bool is_valid_utf8(const std::string& src);
|
||||
std::string utf8_to_latin1(const std::string& src);
|
||||
std::wstring utf8_to_utf16(const std::string& src);
|
||||
std::string utf16_to_utf8(const std::wstring& src);
|
||||
std::string utf16_to_latin1(const std::wstring& src);
|
||||
std::string latin1_to_utf8(const std::string& src);
|
||||
std::wstring latin1_to_utf16(const std::string& src);
|
||||
|
||||
std::vector<std::string> split(const std::string& str, const std::string& separators = " ");
|
||||
template<typename T> std::vector<T> split(const std::string& str, const std::string& separators = " ") {
|
||||
std::vector<std::string> splitted = split(str, separators);
|
||||
|
||||
Reference in New Issue
Block a user