summaryrefslogtreecommitdiff
path: root/day-2/util/file.zig
diff options
context:
space:
mode:
authorChristian Segundo2022-12-02 09:20:45 +0100
committerChristian Segundo2022-12-02 09:57:49 +0100
commit6990eb44ae6111ac18667e416dc4dc2883c50f25 (patch)
tree61798deb9ee52c47215ebe9addb7ad225316e28a /day-2/util/file.zig
parent77623e0a5bdbfb271bc02f26cf2b377ec16b55ec (diff)
downloadadvent-of-zig-2022-6990eb44ae6111ac18667e416dc4dc2883c50f25.tar.gz
add day 2
Diffstat (limited to 'day-2/util/file.zig')
-rw-r--r--day-2/util/file.zig15
1 files changed, 15 insertions, 0 deletions
diff --git a/day-2/util/file.zig b/day-2/util/file.zig
new file mode 100644
index 0000000..90849e9
--- /dev/null
+++ b/day-2/util/file.zig
@@ -0,0 +1,15 @@
+const std = @import("std");
+
+/// Reads an entire file into memory, caller owns the returned slice.
+pub fn slurp(allocator: std.mem.Allocator, file_path: []const u8) ![]u8 {
+ var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
+ const path = try std.fs.realpath(file_path, &path_buffer);
+
+ const file = try std.fs.openFileAbsolute(path, .{});
+ defer file.close();
+
+ return try file.readToEndAlloc(
+ allocator,
+ (try file.stat()).size,
+ );
+}