UTF-8 everywhere

This commit is contained in:
Tamás Bálint Misius 2021-02-17 21:20:01 +01:00
parent 31dda85504
commit d155b0ffc1
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
3 changed files with 90 additions and 68 deletions

View File

@ -28,9 +28,14 @@ ByteString ExecutableName()
{
ByteString ret;
#if defined(WIN)
char *name = (char *)malloc(64);
using Char = wchar_t;
#else
using Char = char;
#endif
#if defined(WIN)
wchar_t *name = (wchar_t *)malloc(sizeof(wchar_t) * 64);
DWORD max = 64, res;
while ((res = GetModuleFileName(NULL, name, max)) >= max)
while ((res = GetModuleFileNameW(NULL, name, max)) >= max)
{
#elif defined MACOSX
char *fn = (char*)malloc(64),*name = (char*)malloc(PATH_MAX);
@ -59,10 +64,10 @@ ByteString ExecutableName()
#endif
#ifndef MACOSX
max *= 2;
char* realloced_name = (char *)realloc(name, max);
Char* realloced_name = (Char *)realloc(name, sizeof(Char) * max);
assert(realloced_name != NULL);
name = realloced_name;
memset(name, 0, max);
memset(name, 0, sizeof(Char) * max);
}
#endif
if (res <= 0)
@ -70,7 +75,11 @@ ByteString ExecutableName()
free(name);
return "";
}
#if defined(WIN)
ret = WinNarrow(name);
#else
ret = name;
#endif
free(name);
return ret;
}
@ -81,7 +90,7 @@ void DoRestart()
if (exename.length())
{
#ifdef WIN
int ret = int(INT_PTR(ShellExecute(NULL, NULL, exename.c_str(), NULL, NULL, SW_SHOWNORMAL)));
int ret = int(INT_PTR(ShellExecuteW(NULL, NULL, WinWiden(exename).c_str(), NULL, NULL, SW_SHOWNORMAL)));
if (ret <= 32)
{
fprintf(stderr, "cannot restart: ShellExecute(...) failed: code %i\n", ret);
@ -109,7 +118,7 @@ void DoRestart()
void OpenURI(ByteString uri)
{
#if defined(WIN)
if (int(INT_PTR(ShellExecute(NULL, NULL, uri.c_str(), NULL, NULL, SW_SHOWNORMAL))) <= 32)
if (int(INT_PTR(ShellExecuteW(NULL, NULL, WinWiden(uri).c_str(), NULL, NULL, SW_SHOWNORMAL))) <= 32)
{
fprintf(stderr, "cannot open URI: ShellExecute(...) failed\n");
}
@ -167,4 +176,36 @@ void LoadFileInResource(int name, int type, unsigned int& size, const char*& dat
#endif
}
#ifdef WIN
ByteString WinNarrow(const std::wstring &source)
{
int buffer_size = WideCharToMultiByte(CP_UTF8, 0, source.c_str(), source.size(), nullptr, 0, NULL, NULL);
if (!buffer_size)
{
return "";
}
std::string output(buffer_size, 0);
if (!WideCharToMultiByte(CP_UTF8, 0, source.c_str(), source.size(), &output[0], buffer_size, NULL, NULL))
{
return "";
}
return output;
}
std::wstring WinWiden(const ByteString &source)
{
int buffer_size = MultiByteToWideChar(CP_UTF8, 0, source.c_str(), source.size(), nullptr, 0);
if (!buffer_size)
{
return L"";
}
std::wstring output(buffer_size, 0);
if (!MultiByteToWideChar(CP_UTF8, 0, source.c_str(), source.size(), &output[0], buffer_size))
{
return L"";
}
return output;
}
#endif
}

View File

@ -4,6 +4,10 @@
#include "common/String.h"
#ifdef WIN
# include <string>
#endif
namespace Platform
{
ByteString ExecutableName();
@ -15,6 +19,11 @@ namespace Platform
long unsigned int GetTime();
void LoadFileInResource(int name, int type, unsigned int& size, const char*& data);
#ifdef WIN
ByteString WinNarrow(const std::wstring &source);
std::wstring WinWiden(const ByteString &source);
#endif
}
#endif

View File

@ -162,57 +162,33 @@ bool Client::DoInstallation()
int returnval;
LONG rresult;
HKEY newkey;
ByteString currentfilename2 = Platform::ExecutableName();
// this isn't necessary but I don't feel like c++ifying this code right now
const char *currentfilename = currentfilename2.c_str();
char *iconname = NULL;
char *opencommand = NULL;
char *protocolcommand = NULL;
//char AppDataPath[MAX_PATH];
char *AppDataPath = NULL;
iconname = (char*)malloc(strlen(currentfilename)+6);
sprintf(iconname, "%s,-102", currentfilename);
ByteString currentfilename = Platform::ExecutableName();
ByteString iconname = currentfilename + ",-102";
ByteString AppDataPath = Platform::WinNarrow(_wgetcwd(NULL, 0));
ByteString opencommand = "\"" + currentfilename + "\" open \"%%1\" ddir \"" + AppDataPath + "\"";
ByteString protocolcommand = "\"" + currentfilename + "\" ddir \"" + AppDataPath + "\" ptsave \"%%1\"";
//Create Roaming application data folder
/*if(!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE, NULL, 0, AppDataPath)))
{
returnval = 0;
goto finalise;
}*/
AppDataPath = _getcwd(NULL, 0);
//Move Game executable into application data folder
//TODO: Implement
opencommand = (char*)malloc(strlen(currentfilename)+53+strlen(AppDataPath));
protocolcommand = (char*)malloc(strlen(currentfilename)+53+strlen(AppDataPath));
/*if((strlen(AppDataPath)+strlen(APPDATA_SUBDIR "\\Powder Toy"))<MAX_PATH)
{
strappend(AppDataPath, APPDATA_SUBDIR);
_mkdir(AppDataPath);
strappend(AppDataPath, "\\Powder Toy");
_mkdir(AppDataPath);
} else {
returnval = 0;
goto finalise;
}*/
sprintf(opencommand, "\"%s\" open \"%%1\" ddir \"%s\"", currentfilename, AppDataPath);
sprintf(protocolcommand, "\"%s\" ddir \"%s\" ptsave \"%%1\"", currentfilename, AppDataPath);
auto createKey = [](ByteString s, HKEY &k) {
return RegCreateKeyExW(HKEY_CURRENT_USER, Platform::WinWiden(s).c_str(), 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &k, NULL);
};
auto setValue = [](HKEY k, ByteString s) {
auto w = Platform::WinWiden(s);
return RegSetValueExW(k, NULL, 0, REG_SZ, (LPBYTE)&w[0], w.size() + 1);
};
//Create protocol entry
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\ptsave", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\ptsave", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)"Powder Toy Save", strlen("Powder Toy Save")+1);
rresult = setValue(newkey, "Powder Toy Save");
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, (LPCSTR)"URL Protocol", 0, REG_SZ, (LPBYTE)"", strlen("")+1);
rresult = RegSetValueExW(newkey, (LPWSTR)L"URL Protocol", 0, REG_SZ, (LPBYTE)L"", 1);
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -221,12 +197,12 @@ bool Client::DoInstallation()
RegCloseKey(newkey);
//Set Protocol DefaultIcon
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\ptsave\\DefaultIcon", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\ptsave\\DefaultIcon", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)iconname, strlen(iconname)+1);
rresult = setValue(newkey, iconname);
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -235,12 +211,12 @@ bool Client::DoInstallation()
RegCloseKey(newkey);
//Set Protocol Launch command
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\ptsave\\shell\\open\\command", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\ptsave\\shell\\open\\command", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)protocolcommand, strlen(protocolcommand)+1);
rresult = setValue(newkey, protocolcommand);
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -249,12 +225,12 @@ bool Client::DoInstallation()
RegCloseKey(newkey);
//Create extension entry
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\.cps", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\.cps", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)"PowderToySave", strlen("PowderToySave")+1);
rresult = setValue(newkey, "PowderToySave");
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -262,12 +238,12 @@ bool Client::DoInstallation()
}
RegCloseKey(newkey);
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\.stm", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\.stm", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)"PowderToySave", strlen("PowderToySave")+1);
rresult = setValue(newkey, "PowderToySave");
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -276,12 +252,12 @@ bool Client::DoInstallation()
RegCloseKey(newkey);
//Create program entry
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\PowderToySave", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\PowderToySave", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)"Powder Toy Save", strlen("Powder Toy Save")+1);
rresult = setValue(newkey, "Powder Toy Save");
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -290,12 +266,12 @@ bool Client::DoInstallation()
RegCloseKey(newkey);
//Set DefaultIcon
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\PowderToySave\\DefaultIcon", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\PowderToySave\\DefaultIcon", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)iconname, strlen(iconname)+1);
rresult = setValue(newkey, iconname);
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -304,12 +280,12 @@ bool Client::DoInstallation()
RegCloseKey(newkey);
//Set Launch command
rresult = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\PowderToySave\\shell\\open\\command", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &newkey, NULL);
rresult = createKey("Software\\Classes\\PowderToySave\\shell\\open\\command", newkey);
if (rresult != ERROR_SUCCESS) {
returnval = 0;
goto finalise;
}
rresult = RegSetValueEx(newkey, 0, 0, REG_SZ, (LPBYTE)opencommand, strlen(opencommand)+1);
rresult = setValue(newkey, opencommand);
if (rresult != ERROR_SUCCESS) {
RegCloseKey(newkey);
returnval = 0;
@ -320,10 +296,6 @@ bool Client::DoInstallation()
returnval = 1;
finalise:
free(iconname);
free(opencommand);
free(protocolcommand);
return returnval;
#elif defined(LIN)
#include "icondoc.h"
@ -451,10 +423,10 @@ std::vector<ByteString> Client::DirectorySearch(ByteString directory, ByteString
std::vector<ByteString> directoryList;
#if defined(WIN) && !defined(__GNUC__)
//Windows
struct _finddata_t currentFile;
struct _wfinddata_t currentFile;
intptr_t findFileHandle;
ByteString fileMatch = directory + "*.*";
findFileHandle = _findfirst(fileMatch.c_str(), &currentFile);
findFileHandle = _wfindfirst(Platform::WinWiden(fileMatch).c_str(), &currentFile);
if (findFileHandle == -1L)
{
#ifdef DEBUG
@ -464,11 +436,11 @@ std::vector<ByteString> Client::DirectorySearch(ByteString directory, ByteString
}
do
{
ByteString currentFileName = ByteString(currentFile.name);
ByteString currentFileName = Platform::WinNarrow(currentFile.name);
if(currentFileName.length()>4)
directoryList.push_back(directory+currentFileName);
}
while (_findnext(findFileHandle, &currentFile) == 0);
while (_wfindnext(findFileHandle, &currentFile) == 0);
_findclose(findFileHandle);
#else
//Linux or MinGW
@ -519,7 +491,7 @@ std::vector<ByteString> Client::DirectorySearch(ByteString directory, ByteString
int Client::MakeDirectory(const char * dirName)
{
#ifdef WIN
return _mkdir(dirName);
return _wmkdir(Platform::WinWiden(dirName).c_str());
#else
return mkdir(dirName, 0755);
#endif