Skip to content

Commit

Permalink
Fix sfztools#95: replace \ with / on Windows when saving file paths.
Browse files Browse the repository at this point in the history
... and do vice versa on restore.
  • Loading branch information
atsushieno committed Sep 20, 2023
1 parent a6b38b8 commit 92eeede
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
22 changes: 22 additions & 0 deletions plugins/common/plugin/NativeHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@
#include <cstdlib>
#include <fstream>


#if defined(_WIN32)
const fs::path toPlatformAgnosticPath(std::string& filePath)
{
return fs::u8path(filePath.replace(filePath.begin(), filePath.end(), '\\', '/'));
}
const fs::path fromPlatformAgnosticPath(const char *filePath)
{
std::string p{filePath};
return fs::u8path(p.replace(p.begin(), p.end(), '/', '\\'));
}
#else
const fs::path toPlatformAgnosticPath(std::string& filePath)
{
return fs::u8path(filePath);
}
const fs::path fromPlatformAgnosticPath(const char *filePath)
{
return fs::u8path(filePath);
}
#endif

#if defined(_WIN32)
#include <windows.h>
#include <shlobj.h>
Expand Down
3 changes: 3 additions & 0 deletions plugins/common/plugin/NativeHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

const fs::path& getUserDocumentsDirectory();

const fs::path toPlatformAgnosticPath(std::string& filePath);
const fs::path fromPlatformAgnosticPath(const char *filePath);

#if !defined(_WIN32) && !defined(__APPLE__)
const fs::path& getUserHomeDirectory();
const fs::path& getXdgConfigHome();
Expand Down
4 changes: 3 additions & 1 deletion plugins/vst/SfizzVstProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "sfizz/import/sfizz_import.h"
#include "plugin/SfizzFileScan.h"
#include "plugin/InstrumentDescription.h"
#include "plugin/NativeHelpers.h"
#include "base/source/fstreamer.h"
#include "base/source/updatehandler.h"
#include "pluginterfaces/vst/ivstevents.h"
Expand Down Expand Up @@ -178,7 +179,8 @@ tresult PLUGIN_API SfizzVstProcessor::setState(IBStream* stream)
if (statePath->empty())
continue;

fs::path pathOrig = fs::u8path(*statePath);
// save file path in platform-agnostic way. See sfizz-ui issue #95
fs::path pathOrig = toPlatformAgnosticPath(*statePath);
std::error_code ec;
if (fs::is_regular_file(pathOrig, ec))
continue;
Expand Down
5 changes: 3 additions & 2 deletions plugins/vst/SfizzVstState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz

#include "SfizzVstState.h"
#include "plugin/NativeHelpers.h"
#include <sfizz.h>
#include <mutex>
#include <cstring>
Expand All @@ -21,7 +22,7 @@ tresult SfizzVstState::load(IBStream* state)
return kResultFalse;

if (const char* str = s.readStr8())
sfzFile = str;
sfzFile = fromPlatformAgnosticPath(str);
else
return kResultFalse;

Expand All @@ -41,7 +42,7 @@ tresult SfizzVstState::load(IBStream* state)

if (version >= 1) {
if (const char* str = s.readStr8())
scalaFile = str;
scalaFile = fromPlatformAgnosticPath(str);
else
return kResultFalse;

Expand Down

0 comments on commit 92eeede

Please sign in to comment.