Skip to content

Commit

Permalink
Use fs::poath in SrugeStorage (surge-synthesizer#5041)
Browse files Browse the repository at this point in the history
  • Loading branch information
baconpaul authored Sep 9, 2021
1 parent 512b063 commit f9b96f1
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 142 deletions.
5 changes: 2 additions & 3 deletions src/common/FxPresetAndClipboardManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void FxUserPreset::doPresetRescan(SurgeStorage *storage, bool forceRescan)
haveScannedPresets = true;

auto ud = storage->userFXPath;
auto fd = string_to_path(storage->datapath) / "fx_presets";
auto fd = storage->datapath / "fx_presets";

std::vector<std::pair<fs::path, bool>> sfxfiles;

Expand Down Expand Up @@ -115,8 +115,7 @@ void FxUserPreset::doPresetRescan(SurgeStorage *storage, bool forceRescan)
if (f.second)
rpath = f.first.lexically_relative(fd).parent_path();
else
rpath =
f.first.lexically_relative(string_to_path(storage->userFXPath)).parent_path();
rpath = f.first.lexically_relative(storage->userFXPath).parent_path();

auto startCatPath = rpath.begin();
if (*(startCatPath) == fx_type_names[t])
Expand Down
6 changes: 3 additions & 3 deletions src/common/ModulatorPresetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void ModulatorPreset::savePresetToUser(const fs::path &location, SurgeStorage *s
auto lfo = &(s->getPatch().scene[scene].lfo[lfoid]);
int lfotype = lfo->shape.val.i;

auto containingPath = fs::path{string_to_path(s->userDataPath) / fs::path{PresetDir}};
auto containingPath = s->userDataPath / fs::path{PresetDir};

if (lfotype == lt_mseg)
containingPath = containingPath / fs::path{"MSEG"};
Expand Down Expand Up @@ -233,8 +233,8 @@ std::vector<ModulatorPreset::Category> ModulatorPreset::getPresets(SurgeStorage

// Do a dual directory traversal of factory and user data with the fs::directory_iterator stuff
// looking for .lfopreset
auto factoryPath = fs::path({string_to_path(s->datapath) / fs::path{"modulator_presets"}});
auto userPath = fs::path({string_to_path(s->userDataPath) / fs::path{PresetDir}});
auto factoryPath = s->datapath / fs::path{"modulator_presets"};
auto userPath = s->userDataPath / fs::path{PresetDir};

std::map<std::string, Category> resMap; // handy it is sorted!

Expand Down
3 changes: 2 additions & 1 deletion src/common/PatchDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ CREATE TABLE Category (

explicit workerS(SurgeStorage *storage) : storage(storage)
{
auto dbname = storage->userDataPath + "/SurgePatches.db";
auto dbpath = storage->userDataPath / fs::path{"SurgePatches.db"};
auto dbname = path_to_string(dbpath);
auto flag = SQLITE_OPEN_FULLMUTEX; // basically lock
flag |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;

Expand Down
81 changes: 33 additions & 48 deletions src/common/SurgeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,25 +293,27 @@ SurgeStorage::SurgeStorage(std::string suppliedDataPath) : otherscene_clients(0)
** If local directory doesn't exists - we probably came here through an installer -
** check for /usr/share/surge and use /usr/share/Surge as our last guess
*/
if (!fs::is_directory(string_to_path(datapath)))
if (!fs::is_directory(datapath))
{
if (fs::is_directory(string_to_path(std::string(Surge::Build::CMAKE_INSTALL_PREFIX) +
"/share/surge-xt")))
if (fs::is_directory(string_to_path(std::string(Surge::Build::CMAKE_INSTALL_PREFIX)) /
"share" / "surge-xt"))
{
datapath = std::string() + Surge::Build::CMAKE_INSTALL_PREFIX + "/share/surge-xt";
datapath =
string_to_path(Surge::Build::CMAKE_INSTALL_PREFIX) / "share" / "surge-xt";
}
else if (fs::is_directory(string_to_path(
std::string(Surge::Build::CMAKE_INSTALL_PREFIX) + "/share/Surge XT")))
else if (fs::is_directory(string_to_path(Surge::Build::CMAKE_INSTALL_PREFIX) / "share" /
"Surge XT"))
{
datapath = std::string() + Surge::Build::CMAKE_INSTALL_PREFIX + "/share/Surge XT";
datapath =
string_to_path(Surge::Build::CMAKE_INSTALL_PREFIX) / "share" / "Surge XT";
}
else
{
std::string systemDataPath = "/usr/share/surge-xt/";
if (fs::is_directory(string_to_path(systemDataPath)))
datapath = systemDataPath;
datapath = string_to_path(systemDataPath);
else
datapath = "/usr/share/Surge XT/";
datapath = string_to_path("/usr/share/Surge XT/");
}
}

Expand Down Expand Up @@ -397,7 +399,7 @@ SurgeStorage::SurgeStorage(std::string suppliedDataPath) : otherscene_clients(0)
path /= L"SurgeXTData";
if (fs::is_directory(path))
{
datapath = path_to_string(path);
datapath = path;
}
}
bailOnPortable:
Expand Down Expand Up @@ -455,14 +457,13 @@ SurgeStorage::SurgeStorage(std::string suppliedDataPath) : otherscene_clients(0)
}

// append separator if not present
datapath = Surge::Storage::appendDirectory(datapath, std::string());
userPatchesPath = Surge::Storage::appendDirectory(userDataPath, "Patches");
userWavetablesPath = Surge::Storage::appendDirectory(userDataPath, "Wavetables");
userWavetablesExportPath = Surge::Storage::appendDirectory(userWavetablesPath, "Exported");
userFXPath = Surge::Storage::appendDirectory(userDataPath, "FX Presets");
userMidiMappingsPath = Surge::Storage::appendDirectory(userDataPath, "MIDI Mappings");
userModulatorSettingsPath = Surge::Storage::appendDirectory(userDataPath, "Modulator Presets");
userSkinsPath = Surge::Storage::appendDirectory(userDataPath, "Skins");
userPatchesPath = userDataPath / "Patches";
userWavetablesPath = userDataPath / "Wavetables";
userWavetablesExportPath = userWavetablesPath / "Exported";
userFXPath = userDataPath / "FX Presets";
userMidiMappingsPath = userDataPath / "MIDI Mappings";
userModulatorSettingsPath = userDataPath / "Modulator Presets";
userSkinsPath = userDataPath / "Skins";
createUserDirectory();

/*
Expand All @@ -482,9 +483,8 @@ SurgeStorage::SurgeStorage(std::string suppliedDataPath) : otherscene_clients(0)
"\n";
if (!snapshotloader.Parse(cxmlData.c_str()))
{
reportError("Cannot parse 'configuration.xml' in path '" + datapath +
"'. Please reinstall Surge!",
"Surge Incorrectly Installed");
reportError("Cannot parse 'configuration.xml' from memory. Internal Software Error.",
"Surge Incorrectly Built");
}

load_midi_controllers();
Expand Down Expand Up @@ -655,7 +655,7 @@ SurgeStorage::SurgeStorage(std::string suppliedDataPath) : otherscene_clients(0)

void SurgeStorage::createUserDirectory()
{
auto p = string_to_path(userDataPath);
auto p = userDataPath;
auto needToBuild = false;
if (!fs::is_directory(p))
{
Expand All @@ -669,13 +669,12 @@ void SurgeStorage::createUserDirectory()
for (auto &s : {userDataPath, userDefaultFilePath, userPatchesPath, userWavetablesPath,
userModulatorSettingsPath, userFXPath, userWavetablesExportPath,
userSkinsPath, userMidiMappingsPath})
fs::create_directories(string_to_path(s));
fs::create_directories(s);

auto rd = std::string(SurgeSharedBinary::README_UserArea_txt,
SurgeSharedBinary::README_UserArea_txtSize) +
"\n";
auto of =
std::ofstream(string_to_path(userDataPath) / "README.txt", std::ofstream::out);
auto of = std::ofstream(userDataPath / "README.txt", std::ofstream::out);
if (of.is_open())
of << rd << std::endl;
of.close();
Expand Down Expand Up @@ -809,9 +808,9 @@ void SurgeStorage::refreshPatchOrWTListAddDir(bool userDir, string subdir,

try
{
fs::path patchpath = string_to_path(userDir ? userDataPath : datapath);
fs::path patchpath = userDir ? userDataPath : datapath;
if (!subdir.empty())
patchpath /= string_to_path(subdir);
patchpath /= subdir;

if (!fs::is_directory(patchpath))
{
Expand Down Expand Up @@ -1624,9 +1623,8 @@ void SurgeStorage::write_midi_controllers_to_user_default()

try
{
auto dir = string_to_path(userDataPath);
fs::create_directories(dir);
auto f = string_to_path(userDataPath) / "SurgeMIDIDefaults.xml";
fs::create_directories(userDataPath);
auto f = userDataPath / "SurgeMIDIDefaults.xml";
doc.SaveFile(f);
}
catch (const fs::filesystem_error &e)
Expand Down Expand Up @@ -1659,7 +1657,7 @@ void SurgeStorage::setSamplerate(float sr)

void SurgeStorage::load_midi_controllers()
{
auto mcp = string_to_path(userDataPath) / "SurgeMIDIDefaults.xml";
auto mcp = userDataPath / "SurgeMIDIDefaults.xml";
TiXmlDocument mcd;
TiXmlElement *midiRoot = nullptr;
if (mcd.LoadFile(mcp))
Expand Down Expand Up @@ -2000,7 +1998,7 @@ void SurgeStorage::rescanUserMidiMappings()
const auto extension{fs::path{".srgmid"}.native()};
try
{
for (const fs::path &d : fs::directory_iterator{string_to_path(userMidiMappingsPath), ec})
for (const fs::path &d : fs::directory_iterator{userMidiMappingsPath, ec})
{
if (d.extension().native() == extension)
{
Expand Down Expand Up @@ -2125,10 +2123,10 @@ void SurgeStorage::storeMidiMappingToName(std::string name)

doc.InsertEndChild(sm);

fs::create_directories(string_to_path(userMidiMappingsPath));
std::string fn = Surge::Storage::appendDirectory(userMidiMappingsPath, name + ".srgmid");
fs::create_directories(userMidiMappingsPath);
auto fn = userMidiMappingsPath / (name + ".srgmid");

if (!doc.SaveFile(string_to_path(fn)))
if (!doc.SaveFile(path_to_string(fn)))
{
std::ostringstream oss;
oss << "Unable to save MIDI settings to '" << fn << "'!";
Expand Down Expand Up @@ -2340,18 +2338,5 @@ string findReplaceSubstring(string &source, const string &from, const string &to
return newString;
}

std::string appendDirectory(const std::string &root, const std::string &path1)
{
if (root[root.size() - 1] == PATH_SEPARATOR)
return root + path1;
else
return root + PATH_SEPARATOR + path1;
}
std::string appendDirectory(const std::string &root, const std::string &path1,
const std::string &path2)
{
return appendDirectory(appendDirectory(root, path1), path2);
}

} // namespace Storage
} // namespace Surge
28 changes: 12 additions & 16 deletions src/common/SurgeStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1023,19 +1023,18 @@ class alignas(16) SurgeStorage
std::unique_ptr<Surge::Storage::FxUserPreset> fxUserPreset;
std::unique_ptr<Surge::Storage::ModulatorPreset> modulatorPreset;

std::string wtpath;
std::string datapath;
std::string userDefaultFilePath;
std::string userDataPath;
std::string userPatchesPath;
std::string userWavetablesPath;
std::string userModulatorSettingsPath;
std::string userFXPath;
std::string userWavetablesExportPath;
std::string userSkinsPath;
std::string userMidiMappingsPath;

std::string installedPath;
fs::path datapath;
fs::path userDefaultFilePath;
fs::path userDataPath;
fs::path userPatchesPath;
fs::path userWavetablesPath;
fs::path userModulatorSettingsPath;
fs::path userFXPath;
fs::path userWavetablesExportPath;
fs::path userSkinsPath;
fs::path userMidiMappingsPath;

fs::path installedPath;

std::map<std::string, TiXmlDocument> userMidiMappingsXMLByName;
void rescanUserMidiMappings();
Expand Down Expand Up @@ -1365,9 +1364,6 @@ bool isValidUTF8(const std::string &testThis);
std::string findReplaceSubstring(std::string &source, const std::string &from,
const std::string &to);

std::string appendDirectory(const std::string &root, const std::string &path1);
std::string appendDirectory(const std::string &root, const std::string &path1,
const std::string &path2);
} // namespace Storage
} // namespace Surge

Expand Down
2 changes: 1 addition & 1 deletion src/common/SurgeSynthesizerIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ void SurgeSynthesizer::savePatch()
if (storage.getPatch().category.empty())
storage.getPatch().category = "Default";

fs::path savepath = string_to_path(storage.userPatchesPath);
fs::path savepath = storage.userPatchesPath;

try
{
Expand Down
4 changes: 2 additions & 2 deletions src/common/UserDefaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool haveReadDefaultsFile = false;

std::string defaultsFileName(SurgeStorage *storage)
{
std::string fn = storage->userDefaultFilePath + PATH_SEPARATOR + "SurgeXTUserDefaults.xml";
std::string fn = path_to_string(storage->userDefaultFilePath / "SurgeXTUserDefaults.xml");
return fn;
}

Expand Down Expand Up @@ -230,7 +230,7 @@ bool storeUserDefaultValue(SurgeStorage *storage, const DefaultKey &key, const s
** See SurgeSytnehsizer::savePatch for instance
** and so we have to do the same here
*/
fs::create_directories(string_to_path(storage->userDefaultFilePath));
fs::create_directories(storage->userDefaultFilePath);

UserDefaultValue v;
v.key = key;
Expand Down
11 changes: 11 additions & 0 deletions src/common/UserDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <vector>
#include <filesystem/import.h>

/*
** Surge has a variety of settings which users can update and save across
Expand Down Expand Up @@ -72,6 +73,11 @@ enum DefaultKey // streamed as strings so feel free to change the order to whate
std::string getUserDefaultValue(SurgeStorage *storage, const DefaultKey &key,
const std::string &valueIfMissing);
int getUserDefaultValue(SurgeStorage *storage, const DefaultKey &key, int valueIfMissing);
inline fs::path getUserDefaultPath(SurgeStorage *storage, const DefaultKey &key,
const fs::path &valueIfMissing)
{
return string_to_path(getUserDefaultValue(storage, key, path_to_string(valueIfMissing)));
}

/**
* updateUserDefaultValue
Expand All @@ -80,6 +86,11 @@ int getUserDefaultValue(SurgeStorage *storage, const DefaultKey &key, int valueI
*/
bool updateUserDefaultValue(SurgeStorage *storage, const DefaultKey &key, const std::string &value);
bool updateUserDefaultValue(SurgeStorage *storage, const DefaultKey &key, const int value);
inline bool updateUserDefaultPath(SurgeStorage *storage, const DefaultKey &key,
const fs::path &path)
{
return updateUserDefaultValue(storage, key, path_to_string(path));
}

} // namespace Storage
} // namespace Surge
17 changes: 8 additions & 9 deletions src/common/WAVFileSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,15 @@ bool SurgeStorage::load_wt_wav_portable(std::string fn, Wavetable *wt)

std::string SurgeStorage::export_wt_wav_portable(std::string fbase, Wavetable *wt)
{
std::string path;
path = Surge::Storage::appendDirectory(userDataPath, "Wavetables", "Exported");
fs::create_directories(string_to_path(path));
auto path = userDataPath / "Wavetables" / "Exported";
fs::create_directories(path);

auto fnamePre = fbase + ".wav";
auto fname = Surge::Storage::appendDirectory(path, fbase + ".wav");
auto fname = path / (fbase + ".wav");
int fnum = 1;
while (fs::exists(fs::path(fname)))
while (fs::exists(fname))
{
fname = Surge::Storage::appendDirectory(path, fbase + "_" + std::to_string(fnum) + ".wav");
fname = path / (fbase + "_" + std::to_string(fnum) + ".wav");
fnamePre = fbase + "_" + std::to_string(fnum) + ".wav";
fnum++;
}
Expand All @@ -486,9 +485,9 @@ std::string SurgeStorage::export_wt_wav_portable(std::string fbase, Wavetable *w

{
std::filebuf wfp;
if (!wfp.open(string_to_path(fname), std::ios::binary | std::ios::out))
if (!wfp.open(fname, std::ios::binary | std::ios::out))
{
errorMessage = "Unable to open file " + fname + "!";
errorMessage = "Unable to open file " + fname.u8string() + "!";
errorMessage += std::strerror(errno);

reportError(errorMessage, "Wavetable Export");
Expand Down Expand Up @@ -574,5 +573,5 @@ std::string SurgeStorage::export_wt_wav_portable(std::string fbase, Wavetable *w

refresh_wtlist();

return fname;
return path_to_string(fname);
}
9 changes: 4 additions & 5 deletions src/gui/SkinSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ void SkinDB::rescanForSkins(SurgeStorage *storage)
#endif
availableSkins.clear();

std::array<fs::path, 2> paths = {string_to_path(storage->datapath),
string_to_path(storage->userSkinsPath)};
std::array<fs::path, 2> paths = {storage->datapath, storage->userSkinsPath};

for (auto &source : paths)
{
Entry::RootType rt = UNKNOWN;
if (path_to_string(source) == path_to_string(paths[0]))
if (source == paths[0])
rt = FACTORY;
if (path_to_string(source) == path_to_string(paths[1]))
if (source == paths[1])
rt = USER;

std::vector<fs::path> alldirs;
Expand Down Expand Up @@ -1338,7 +1337,7 @@ Maybe<SkinDB::Entry> SkinDB::installSkinFromPathToUserDirectory(SurgeStorage *s,
{
auto parentP = p.parent_path();
auto nmP = p.lexically_relative(parentP);
auto tgPath = string_to_path(s->userSkinsPath) / nmP;
auto tgPath = s->userSkinsPath / nmP;

try
{
Expand Down
Loading

0 comments on commit f9b96f1

Please sign in to comment.