commit 48f182ff46be49bdc1fea7406c2d5e94469f0b59
parent 263982b348d224ecc5177c1f9f925315b1d27c1c
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date: Wed, 23 Oct 2024 19:41:20 +0200
add helper func to retrieve OS specific special folders
Diffstat:
2 files changed, 67 insertions(+), 1 deletion(-)
diff --git a/source/synthLib/os.cpp b/source/synthLib/os.cpp
@@ -6,6 +6,8 @@
// filesystem is only available on macOS Catalina 10.15+
// filesystem causes linker errors in gcc-8 if linked statically
#define USE_DIRENT
+#include <cstdlib>
+#include <cstring>
#endif
#ifdef USE_DIRENT
@@ -20,6 +22,7 @@
#define NOMINMAX
#define NOSERVICE
#include <Windows.h>
+#include <shlobj_core.h>
#else
#include <dlfcn.h>
#endif
@@ -411,5 +414,60 @@ namespace synthLib
#else
return false;
#endif
- }
+ }
+
+ std::string getSpecialFolderPath(const SpecialFolderType _type)
+ {
+#ifdef _WIN32
+ std::array<char, MAX_PATH<<1> path;
+
+ int csidl;
+ switch (_type)
+ {
+ case SpecialFolderType::UserDocuments:
+ csidl = CSIDL_PERSONAL;
+ break;
+ case SpecialFolderType::PrivateAppData:
+ csidl = CSIDL_APPDATA;
+ break;
+ default:
+ return {};
+ }
+ if (SHGetSpecialFolderPathA (nullptr, path.data(), csidl, FALSE))
+ return validatePath(path.data());
+#elif defined(__APPLE__)
+ switch (_type)
+ {
+ case SpecialFolderType::UserDocuments:
+ return "~/Documents/"
+ case SpecialFolderType::PrivateAppData:
+ return "~/Library/Application Support/"
+ break;
+ default:
+ return {};
+ }
+#else
+ // https://specifications.freedesktop.org/basedir-spec/latest/
+ switch (_type)
+ {
+ case SpecialFolderType::UserDocuments:
+ {
+ const auto* docDir = std::getenv("XDG_DATA_HOME");
+ if(docDir && strlen(docDir) > 0)
+ return validatePath(docDir);
+ return "~/.local/share/";
+ }
+ case SpecialFolderType::PrivateAppData:
+ {
+ const auto* confDir = std::getenv("XDG_CONFIG_HOME");
+ if(confDir && strlen(confDir) > 0)
+ return validatePath(confDir);
+ return "~/.config/";
+ }
+ default:
+ return {};
+ }
+#endif
+ return {};
+ }
} // namespace synthLib
diff --git a/source/synthLib/os.h b/source/synthLib/os.h
@@ -47,4 +47,12 @@ namespace synthLib
FILE* openFile(const std::string& _name, const char* _mode);
bool isRunningUnderRosetta();
+
+ enum class SpecialFolderType
+ {
+ UserDocuments,
+ PrivateAppData
+ };
+
+ std::string getSpecialFolderPath(SpecialFolderType _type);
} // namespace synthLib