From 126cc23d18cff60c18722594feda68eb8678d6eb Mon Sep 17 00:00:00 2001 From: Benno Lorenz Date: Mon, 1 Dec 2025 23:39:14 +0100 Subject: [PATCH] Utils --- src/utils/loadpuzzle.zig | 28 ++++++++++++++++++++++++++++ test.txt | 1 + 2 files changed, 29 insertions(+) create mode 100644 src/utils/loadpuzzle.zig create mode 100644 test.txt diff --git a/src/utils/loadpuzzle.zig b/src/utils/loadpuzzle.zig new file mode 100644 index 0000000..930a629 --- /dev/null +++ b/src/utils/loadpuzzle.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +pub fn readFile(allocator: std.mem.Allocator, path: []const u8) ![]u8 { + var file = try std.fs.cwd().openFile(path, .{}); + defer file.close(); + + var fatfuckingstr = std.Io.Writer.Allocating.init(allocator); + defer fatfuckingstr.deinit(); + + var buffer: [1024]u8 = undefined; + + var reader = file.reader(&buffer); + + const actual_reader = &reader.interface; + + _ = try actual_reader.stream(&fatfuckingstr.writer, .unlimited); + + return fatfuckingstr.toOwnedSlice(); +} + +test "reader_test" { + const alloc = std.testing.allocator; + + const contents = try readFile("test.txt", alloc); + defer alloc.free(contents); + + try std.testing.expectEqualStrings("Hello World!", contents); +} diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file