From 0d9f7828de76bbae685be30b8f75a5aefad408f3 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Tue, 26 Apr 2022 00:27:04 +0300 Subject: Initial commit --- xdg.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 xdg.zig diff --git a/xdg.zig b/xdg.zig new file mode 100644 index 0000000..8466066 --- /dev/null +++ b/xdg.zig @@ -0,0 +1,36 @@ +const std = @import("std"); + +const Allocator = std.mem.Allocator; +const Dir = std.fs.Dir; + +pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 { + if (std.os.getenv("XDG_DATA_HOME")) |data_home| { + return std.fs.path.join(allocator, &.{ data_home, app_name }); + } + + if (std.os.getenv("HOME")) |home| { + return std.fs.path.join(allocator, &.{ home, ".local", "share", app_name }); + } + + return error.HomeNotFound; +} + +pub fn getBinHome(allocator: Allocator) ![]u8 { + if (std.os.getenv("HOME")) |home| { + return std.fs.path.join(allocator, &.{ home, ".local", "bin" }); + } + + return error.HomeNotFound; +} + +pub fn openDataHome(allocator: Allocator, app_name: []const u8) !Dir { + var data_home = try getDataHome(allocator, app_name); + defer allocator.free(data_home); + return try std.fs.cwd().makeOpenPath(data_home, .{}); +} + +pub fn openBinHome(allocator: Allocator) !Dir { + var bin_home = try getBinHome(allocator); + defer allocator.free(bin_home); + return try std.fs.cwd().makeOpenPath(bin_home, .{}); +} -- cgit v1.2.3