diff options
| author | 2022-04-26 00:27:04 +0300 | |
|---|---|---|
| committer | 2022-04-26 00:27:04 +0300 | |
| commit | 0d9f7828de76bbae685be30b8f75a5aefad408f3 (patch) | |
| tree | ff6dcfc908f1632dbfcaf41f180d60dc4a4555db /xdg.zig | |
| download | zig-xdg-0.1.0.tar.gz zig-xdg-0.1.0.tar.xz zig-xdg-0.1.0.zip | |
Initial commit0.1.0
Diffstat (limited to 'xdg.zig')
| -rw-r--r-- | xdg.zig | 36 |
1 files changed, 36 insertions, 0 deletions
| @@ -0,0 +1,36 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | |||
| 3 | const Allocator = std.mem.Allocator; | ||
| 4 | const Dir = std.fs.Dir; | ||
| 5 | |||
| 6 | pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 { | ||
| 7 | if (std.os.getenv("XDG_DATA_HOME")) |data_home| { | ||
| 8 | return std.fs.path.join(allocator, &.{ data_home, app_name }); | ||
| 9 | } | ||
| 10 | |||
| 11 | if (std.os.getenv("HOME")) |home| { | ||
| 12 | return std.fs.path.join(allocator, &.{ home, ".local", "share", app_name }); | ||
| 13 | } | ||
| 14 | |||
| 15 | return error.HomeNotFound; | ||
| 16 | } | ||
| 17 | |||
| 18 | pub fn getBinHome(allocator: Allocator) ![]u8 { | ||
| 19 | if (std.os.getenv("HOME")) |home| { | ||
| 20 | return std.fs.path.join(allocator, &.{ home, ".local", "bin" }); | ||
| 21 | } | ||
| 22 | |||
| 23 | return error.HomeNotFound; | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn openDataHome(allocator: Allocator, app_name: []const u8) !Dir { | ||
| 27 | var data_home = try getDataHome(allocator, app_name); | ||
| 28 | defer allocator.free(data_home); | ||
| 29 | return try std.fs.cwd().makeOpenPath(data_home, .{}); | ||
| 30 | } | ||
| 31 | |||
| 32 | pub fn openBinHome(allocator: Allocator) !Dir { | ||
| 33 | var bin_home = try getBinHome(allocator); | ||
| 34 | defer allocator.free(bin_home); | ||
| 35 | return try std.fs.cwd().makeOpenPath(bin_home, .{}); | ||
| 36 | } | ||