aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/io.zig14
1 files changed, 14 insertions, 0 deletions
diff --git a/std/io.zig b/std/io.zig
index c86ebed326..87a46b05eb 100644
--- a/std/io.zig
+++ b/std/io.zig
@@ -493,6 +493,20 @@ pub fn writeFile(path: []const u8, data: []const u8, allocator: ?&mem.Allocator)
%return file.write(data);
}
+/// On success, caller owns returned buffer.
+pub fn readFileAlloc(path: []const u8, allocator: &mem.Allocator) -> %[]u8 {
+ var file = %return File.openRead(path, allocator);
+ defer file.close();
+
+ const size = %return file.getEndPos();
+ const buf = %return allocator.alloc(u8, size);
+ %defer allocator.free(buf);
+
+ var adapter = FileInStream.init(&file);
+ %return adapter.stream.readNoEof(buf);
+ return buf;
+}
+
pub const BufferedInStream = BufferedInStreamCustom(os.page_size);
pub fn BufferedInStreamCustom(comptime buffer_size: usize) -> type {