summaryrefslogtreecommitdiff
path: root/util/file.zig
blob: c63422a45e7bebd418e18c3bc9cc4bb6fa99667b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const std = @import("std");

/// Reads an entire file into memory, caller owns the returned slice.
pub fn slurp(allocator: std.mem.Allocator, file_path: []const u8) ![]u8 {
    var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
    const path = try std.fs.realpath(file_path, &path_buffer);

    const file = try std.fs.openFileAbsolute(path, .{});
    defer file.close();

    var buf = try file.readToEndAlloc(
        allocator,
        (try file.stat()).size,
    );

    return allocator.resize(buf, buf.len-1).?;
}