Deduplicate win32 string conversions

This commit is contained in:
Jean Pierre Cimalando 2021-05-08 16:15:08 +02:00
parent 60ad6a5e57
commit d255f682e9
4 changed files with 31 additions and 44 deletions

View file

@ -25,6 +25,28 @@ const fs::path& getUserDocumentsDirectory()
}(); }();
return directory; return directory;
} }
wchar_t *stringToWideChar(const char *str, int strCch)
{
unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0);
if (strSize == 0)
return {};
std::unique_ptr<wchar_t[]> strW(new wchar_t[strSize]);
if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0)
return {};
return strW.release();
}
char* stringToUTF8(const wchar_t *strW, int strWCch)
{
unsigned strSize = WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, nullptr, 0, nullptr, nullptr);
if (strSize == 0)
return {};
std::unique_ptr<char[]> str(new char[strSize]);
if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0)
return {};
return str.release();
}
#elif defined(__APPLE__) #elif defined(__APPLE__)
// implemented in NativeHelpers.mm // implemented in NativeHelpers.mm
#else #else

View file

@ -7,6 +7,9 @@
#pragma once #pragma once
#include <ghc/fs_std.hpp> #include <ghc/fs_std.hpp>
#include <vector> #include <vector>
#if defined(_WIN32)
#include <cwchar>
#endif
const fs::path& getUserDocumentsDirectory(); const fs::path& getUserDocumentsDirectory();
@ -16,3 +19,8 @@ const fs::path& getXdgConfigHome();
struct XdgUserDirsEntry { std::string name; fs::path value; }; struct XdgUserDirsEntry { std::string name; fs::path value; };
std::vector<XdgUserDirsEntry> parseXdgUserDirs(const fs::path& userDirsPath); std::vector<XdgUserDirsEntry> parseXdgUserDirs(const fs::path& userDirsPath);
#endif #endif
#if defined(_WIN32)
wchar_t *stringToWideChar(const char *str, int strCch = -1);
char* stringToUTF8(const wchar_t *strW, int strWCch = -1);
#endif

View file

@ -37,28 +37,6 @@ static HKEY openRegistryKey()
return key; return key;
} }
static WCHAR* stringToWideChar(const char *str, int strCch = -1)
{
unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0);
if (strSize == 0)
return {};
std::unique_ptr<WCHAR[]> strW(new WCHAR[strSize]);
if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0)
return {};
return strW.release();
}
static char* stringToUTF8(const wchar_t *strW, int strWCch = -1)
{
unsigned strSize = WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, nullptr, 0, nullptr, nullptr);
if (strSize == 0)
return {};
std::unique_ptr<char[]> str(new char[strSize]);
if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0)
return {};
return str.release();
}
absl::optional<std::string> SfizzSettings::load(const char* name) absl::optional<std::string> SfizzSettings::load(const char* name)
{ {
std::unique_ptr<WCHAR[]> nameW { stringToWideChar(name) }; std::unique_ptr<WCHAR[]> nameW { stringToWideChar(name) };

View file

@ -5,34 +5,13 @@
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include "NativeHelpers.h" #include "NativeHelpers.h"
#include "plugin/NativeHelpers.h"
#if defined(_WIN32) #if defined(_WIN32)
#include "ghc/fs_std.hpp" #include "ghc/fs_std.hpp"
#include <windows.h> #include <windows.h>
#include <cstring> #include <cstring>
static WCHAR *stringToWideChar(const char *str, int strCch = -1)
{
unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0);
if (strSize == 0)
return {};
std::unique_ptr<WCHAR[]> strW(new WCHAR[strSize]);
if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0)
return {};
return strW.release();
}
static char* stringToUTF8(const wchar_t *strW, int strWCch = -1)
{
unsigned strSize = WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, nullptr, 0, nullptr, nullptr);
if (strSize == 0)
return {};
std::unique_ptr<char[]> str(new char[strSize]);
if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0)
return {};
return str.release();
}
bool openFileInExternalEditor(const char *filename) bool openFileInExternalEditor(const char *filename)
{ {
std::wstring path = stringToWideChar(filename); std::wstring path = stringToWideChar(filename);