aboutsummaryrefslogtreecommitdiff
path: root/source/lib/src/system/xdg.cpp
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2024-05-17 17:58:38 +0200
committerFelix Morgner <felix.morgner@gmail.com>2024-05-17 17:58:38 +0200
commit577fc0845718ed8ad5bebf02a277c0579a817f77 (patch)
tree3d1cdc53c426a0ba60a7996619a7b787850bb3b3 /source/lib/src/system/xdg.cpp
parentde5bf7ca3b7a2bf6be35b86486b00dc6a071b950 (diff)
downloadwanda-577fc0845718ed8ad5bebf02a277c0579a817f77.tar.xz
wanda-577fc0845718ed8ad5bebf02a277c0579a817f77.zip
wanda: restructure source layoutHEADdevelop
Diffstat (limited to 'source/lib/src/system/xdg.cpp')
-rw-r--r--source/lib/src/system/xdg.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/source/lib/src/system/xdg.cpp b/source/lib/src/system/xdg.cpp
new file mode 100644
index 0000000..109beb1
--- /dev/null
+++ b/source/lib/src/system/xdg.cpp
@@ -0,0 +1,46 @@
+#include "wanda/system/xdg.hpp"
+
+#include <unistd.h>
+
+namespace wanda::system
+{
+ std::string xdg_variable(xdg_directory directory)
+ {
+ switch (directory)
+ {
+ case xdg_directory::data_home:
+ return "XDG_DATA_HOME";
+ case xdg_directory::config_home:
+ return "XDG_CONFIG_HOME";
+ case xdg_directory::cache_home:
+ return "XDG_CACHE_HOME";
+ case xdg_directory::runtime_dir:
+ return "XDG_RUNTIME_DIR";
+ }
+ return "XDG_INVALID_PATH";
+ }
+
+ std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment)
+ {
+ if (auto path = environment[xdg_variable(directory)]; !path.empty())
+ {
+ return path;
+ }
+
+ auto home = std::filesystem::path{environment["HOME"]};
+ switch (directory)
+ {
+ case xdg_directory::data_home:
+ return home / ".local/share";
+ case xdg_directory::config_home:
+ return home / ".config";
+ case xdg_directory::cache_home:
+ return home / ".cache";
+ case xdg_directory::runtime_dir:
+ return std::filesystem::path{"/run/user"} / std::to_string(::getuid());
+ }
+
+ return "";
+ }
+
+} // namespace wanda::system \ No newline at end of file