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