diff options
| author | 2022-04-28 15:24:04 +0300 | |
|---|---|---|
| committer | 2022-04-28 15:24:04 +0300 | |
| commit | 26639e50682ecb0f7373935de2dbc8d1d2319226 (patch) | |
| tree | cf0a907586868f0333f79a223a0bd23b06add2e2 /xdg.zig | |
| parent | Initial commit (diff) | |
| download | zig-xdg-26639e50682ecb0f7373935de2dbc8d1d2319226.tar.gz zig-xdg-26639e50682ecb0f7373935de2dbc8d1d2319226.tar.xz zig-xdg-26639e50682ecb0f7373935de2dbc8d1d2319226.zip | |
Add support for getting config directories0.2.0
Diffstat (limited to 'xdg.zig')
| -rw-r--r-- | xdg.zig | 43 |
1 files changed, 43 insertions, 0 deletions
| @@ -1,6 +1,7 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | 2 | ||
| 3 | const Allocator = std.mem.Allocator; | 3 | const Allocator = std.mem.Allocator; |
| 4 | const ArrayList = std.ArrayList; | ||
| 4 | const Dir = std.fs.Dir; | 5 | const Dir = std.fs.Dir; |
| 5 | 6 | ||
| 6 | pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 { | 7 | pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 { |
| @@ -15,6 +16,18 @@ pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 { | |||
| 15 | return error.HomeNotFound; | 16 | return error.HomeNotFound; |
| 16 | } | 17 | } |
| 17 | 18 | ||
| 19 | pub fn getConfigHome(allocator: Allocator, app_name: []const u8) ![]u8 { | ||
| 20 | if (std.os.getenv("XDG_CONFIG_HOME")) |config_home| { | ||
| 21 | return std.fs.path.join(allocator, &.{ config_home, app_name }); | ||
| 22 | } | ||
| 23 | |||
| 24 | if (std.os.getenv("HOME")) |home| { | ||
| 25 | return std.fs.path.join(allocator, &.{ home, ".config", app_name }); | ||
| 26 | } | ||
| 27 | |||
| 28 | return error.HomeNotFound; | ||
| 29 | } | ||
| 30 | |||
| 18 | pub fn getBinHome(allocator: Allocator) ![]u8 { | 31 | pub fn getBinHome(allocator: Allocator) ![]u8 { |
| 19 | if (std.os.getenv("HOME")) |home| { | 32 | if (std.os.getenv("HOME")) |home| { |
| 20 | return std.fs.path.join(allocator, &.{ home, ".local", "bin" }); | 33 | return std.fs.path.join(allocator, &.{ home, ".local", "bin" }); |
| @@ -23,6 +36,36 @@ pub fn getBinHome(allocator: Allocator) ![]u8 { | |||
| 23 | return error.HomeNotFound; | 36 | return error.HomeNotFound; |
| 24 | } | 37 | } |
| 25 | 38 | ||
| 39 | pub fn getConfigDirs(allocator: Allocator, app_name: []const u8) ![][]u8 { | ||
| 40 | var list = ArrayList([]u8).init(allocator); | ||
| 41 | errdefer for (list.items) |s| allocator.free(s); | ||
| 42 | defer list.deinit(); | ||
| 43 | |||
| 44 | if (std.os.getenv("XDG_CONFIG_DIRS")) |config_dirs| { | ||
| 45 | var it = std.mem.split(u8, config_dirs, ":"); | ||
| 46 | while (it.next()) |config_dir| { | ||
| 47 | try list.append(try std.fs.path.join(allocator, &.{ config_dir, app_name })); | ||
| 48 | } | ||
| 49 | } else { | ||
| 50 | try list.append(try std.fs.path.join(allocator, &.{ "/etc/xdg", app_name })); | ||
| 51 | } | ||
| 52 | |||
| 53 | return list.toOwnedSlice(); | ||
| 54 | } | ||
| 55 | |||
| 56 | pub fn getAllConfigDirs(allocator: Allocator, app_name: []const u8) ![][]u8 { | ||
| 57 | var list = ArrayList([]u8).init(allocator); | ||
| 58 | errdefer for (list.items) |s| allocator.free(s); | ||
| 59 | defer list.deinit(); | ||
| 60 | |||
| 61 | try list.append(try getConfigHome(allocator, app_name)); | ||
| 62 | const config_dirs = try getConfigDirs(allocator, app_name); | ||
| 63 | defer allocator.free(config_dirs); | ||
| 64 | try list.appendSlice(config_dirs); | ||
| 65 | |||
| 66 | return list.toOwnedSlice(); | ||
| 67 | } | ||
| 68 | |||
| 26 | pub fn openDataHome(allocator: Allocator, app_name: []const u8) !Dir { | 69 | pub fn openDataHome(allocator: Allocator, app_name: []const u8) !Dir { |
| 27 | var data_home = try getDataHome(allocator, app_name); | 70 | var data_home = try getDataHome(allocator, app_name); |
| 28 | defer allocator.free(data_home); | 71 | defer allocator.free(data_home); |