aboutsummaryrefslogtreecommitdiff
path: root/std/io.zig
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-12-04 22:05:27 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-12-04 22:05:27 -0500
commit31d9dc35395d495fa896532bdffb4529c136ce41 (patch)
treebaa81daf9f57f075bd9b145b7c897168425cbd8a /std/io.zig
parent5ebed1c9eeff6a190ff3d603bc6c680d4b920bd8 (diff)
downloadzig-31d9dc35395d495fa896532bdffb4529c136ce41.tar.gz
zig-31d9dc35395d495fa896532bdffb4529c136ce41.zip
read a file
Diffstat (limited to 'std/io.zig')
-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 {