From d255f682e9cb3b9b02df8534e289b17e20982eaa Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Sat, 8 May 2021 16:15:08 +0200 Subject: [PATCH] Deduplicate win32 string conversions --- plugins/common/plugin/NativeHelpers.cpp | 22 ++++++++++++++++++++ plugins/common/plugin/NativeHelpers.h | 8 +++++++ plugins/common/plugin/SfizzSettings.cpp | 22 -------------------- plugins/editor/src/editor/NativeHelpers.cpp | 23 +-------------------- 4 files changed, 31 insertions(+), 44 deletions(-) diff --git a/plugins/common/plugin/NativeHelpers.cpp b/plugins/common/plugin/NativeHelpers.cpp index f6988de0..7df14292 100644 --- a/plugins/common/plugin/NativeHelpers.cpp +++ b/plugins/common/plugin/NativeHelpers.cpp @@ -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 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 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 diff --git a/plugins/common/plugin/NativeHelpers.h b/plugins/common/plugin/NativeHelpers.h index 22407662..0b534a19 100644 --- a/plugins/common/plugin/NativeHelpers.h +++ b/plugins/common/plugin/NativeHelpers.h @@ -7,6 +7,9 @@ #pragma once #include #include +#if defined(_WIN32) +#include +#endif const fs::path& getUserDocumentsDirectory(); @@ -16,3 +19,8 @@ const fs::path& getXdgConfigHome(); struct XdgUserDirsEntry { std::string name; fs::path value; }; std::vector 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 diff --git a/plugins/common/plugin/SfizzSettings.cpp b/plugins/common/plugin/SfizzSettings.cpp index 6e1bf520..091ad6f7 100644 --- a/plugins/common/plugin/SfizzSettings.cpp +++ b/plugins/common/plugin/SfizzSettings.cpp @@ -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 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 str(new char[strSize]); - if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0) - return {}; - return str.release(); -} - absl::optional SfizzSettings::load(const char* name) { std::unique_ptr nameW { stringToWideChar(name) }; diff --git a/plugins/editor/src/editor/NativeHelpers.cpp b/plugins/editor/src/editor/NativeHelpers.cpp index 0be3a57b..1fdfcedd 100644 --- a/plugins/editor/src/editor/NativeHelpers.cpp +++ b/plugins/editor/src/editor/NativeHelpers.cpp @@ -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 #include -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 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 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);