Add new and refactor existing char<->wchar routines for Win32

pull/3/head
Hleb Valoshka 2019-07-14 23:13:39 +03:00
parent f05679db8e
commit 6d81fc780a
2 changed files with 48 additions and 12 deletions

View File

@ -1,5 +1,6 @@
// winutil.h // winutil.cpp
// //
// Copyright (C) 2019, Celestia Development Team
// Copyright (C) 2002, Chris Laurel <claurel@shatters.net> // Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
@ -55,7 +56,8 @@ const char* CurrentCP()
{ {
static bool set = false; static bool set = false;
static char cp[20] = "CP"; static char cp[20] = "CP";
if (!set) { if (!set)
{
GetLocaleInfo(GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE, cp+2, 18); GetLocaleInfo(GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE, cp+2, 18);
set = true; set = true;
} }
@ -64,13 +66,42 @@ const char* CurrentCP()
string UTF8ToCurrentCP(const string& str) string UTF8ToCurrentCP(const string& str)
{ {
string localeStr; return WideToCurrentCP(UTF8ToWide(str));
LPWSTR wout = new wchar_t[str.length() + 1]; }
LPSTR out = new char[str.length() + 1];
int wlength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wout, str.length() + 1); string CurrentCPToUTF8(const string& str)
WideCharToMultiByte(CP_ACP, 0, wout, -1, out, str.length() + 1, nullptr, nullptr); {
localeStr = out; return WideToUTF8(CurrentCPToWide(str));
delete [] wout; }
delete [] out;
return localeStr; string WideToCurrentCP(const wstring& ws)
{
string out(ws.length()+1, 0);
WideCharToMultiByte(CP_ACP, 0, ws.c_str(), -1, &out[0], ws.length()+1, nullptr, nullptr);
return out;
}
wstring CurrentCPToWide(const string& str)
{
wstring w(str.length()+1, 0);
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &w[0], str.length()+1);
return w;
}
string WideToUTF8(const wstring& ws)
{
// get a converted string length
auto len = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), -1, nullptr, 0, nullptr, nullptr);
string out(len, 0);
WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), -1, &out[0], len, nullptr, nullptr);
return out;
}
wstring UTF8ToWide(const string& s)
{
// get a converted string length
auto len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
wstring out(len, 0);
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, &out[0], len);
return out;
} }

View File

@ -24,5 +24,10 @@ void RemoveButtonDefaultStyle(HWND hWnd);
void AddButtonDefaultStyle(HWND hWnd); void AddButtonDefaultStyle(HWND hWnd);
const char* CurrentCP(); const char* CurrentCP();
string UTF8ToCurrentCP(const string& str); string UTF8ToCurrentCP(const string& str);
string CurrentCPToUTF8(const string& str);
string WideToCurrentCP(const wstring& ws);
wstring CurrentCPToWide(const string& str);
string WideToUTF8(const wstring& ws);
wstring UTF8ToWide(const string& str);
#endif #endif