diff options
Diffstat (limited to 'src/files.zig')
| -rw-r--r-- | src/files.zig | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/files.zig b/src/files.zig new file mode 100644 index 0000000..ed9e628 --- /dev/null +++ b/src/files.zig | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | |||
| 3 | const Allocator = std.mem.Allocator; | ||
| 4 | |||
| 5 | pub fn resolvePath(allocator: Allocator, name: []const u8) ![]u8 { | ||
| 6 | return std.fs.cwd().realpathAlloc(allocator, name) catch |err| switch (err) { | ||
| 7 | error.FileNotFound => if (name.len == 0) { | ||
| 8 | return error.FileNotFound; | ||
| 9 | } else if (name[0] == std.fs.path.sep) { | ||
| 10 | // Already is an absolute path | ||
| 11 | return allocator.dupe(u8, name); | ||
| 12 | } else { | ||
| 13 | // TODO: if (name[0] == '~') | ||
| 14 | const cwd = try std.process.getCwdAlloc(allocator); | ||
| 15 | defer allocator.free(cwd); | ||
| 16 | |||
| 17 | return std.fmt.allocPrint(allocator, "{s}{s}{s}", .{cwd, std.fs.path.sep_str, name}); | ||
| 18 | }, | ||
| 19 | else => return err, | ||
| 20 | }; | ||
| 21 | } | ||