Deduplicate win32 string conversions
This commit is contained in:
parent
60ad6a5e57
commit
d255f682e9
4 changed files with 31 additions and 44 deletions
|
|
@ -25,6 +25,28 @@ const fs::path& getUserDocumentsDirectory()
|
|||
}();
|
||||
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__)
|
||||
// implemented in NativeHelpers.mm
|
||||
#else
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@
|
|||
#pragma once
|
||||
#include <ghc/fs_std.hpp>
|
||||
#include <vector>
|
||||
#if defined(_WIN32)
|
||||
#include <cwchar>
|
||||
#endif
|
||||
|
||||
const fs::path& getUserDocumentsDirectory();
|
||||
|
||||
|
|
@ -16,3 +19,8 @@ const fs::path& getXdgConfigHome();
|
|||
struct XdgUserDirsEntry { std::string name; fs::path value; };
|
||||
std::vector<XdgUserDirsEntry> parseXdgUserDirs(const fs::path& userDirsPath);
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
wchar_t *stringToWideChar(const char *str, int strCch = -1);
|
||||
char* stringToUTF8(const wchar_t *strW, int strWCch = -1);
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -37,28 +37,6 @@ static HKEY openRegistryKey()
|
|||
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)
|
||||
{
|
||||
std::unique_ptr<WCHAR[]> nameW { stringToWideChar(name) };
|
||||
|
|
|
|||
|
|
@ -5,34 +5,13 @@
|
|||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
||||
|
||||
#include "NativeHelpers.h"
|
||||
#include "plugin/NativeHelpers.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include "ghc/fs_std.hpp"
|
||||
#include <windows.h>
|
||||
#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)
|
||||
{
|
||||
std::wstring path = stringToWideChar(filename);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue