authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 02:41:19 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 02:41:19 -07:00
loga74468be60283e66e9b20388372394b54219d6b0
tree7fca20dc3775358ddcc00df19ec803969b8e9b89
parentb47e61c4f05e895fec868252b3a23c5111b0cd10

add readFile


1 files changed, 9 insertions(+), 0 deletions(-)

src/lib.zig+9
...@@ -448,3 +448,12 @@ pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]...@@ -448,3 +448,12 @@ pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]
448 try reader.readNoEof(list.items[0..len]);448 try reader.readNoEof(list.items[0..len]);
449 return list.items;449 return list.items;
450}450}
451
452pub fn readFile(dir: std.fs.Dir, sub_path: string, alloc: std.mem.Allocator) !string {
453 var file = try dir.openFile(sub_path, .{});
454 defer file.close();
455 const stat = try file.stat();
456 var buffer = try std.ArrayList(u8).initCapacity(alloc, stat.size);
457 pipe(file.reader(), buffer.writer()) catch @panic("unreachable"); // did the filesystem lie?
458 return buffer.items;
459}