From c632125d82166cbf6ec433986c0570566cf0b0b9 Mon Sep 17 00:00:00 2001 From: zhibog Date: Sat, 16 Oct 2021 18:45:25 +0200 Subject: [PATCH] Added a read_entire_file proc that only takes a handle and turned it into a proc group with the one, that takes a path --- core/os/os.odin | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/core/os/os.odin b/core/os/os.odin index 7f938206c..be1f5ad4e 100644 --- a/core/os/os.odin +++ b/core/os/os.odin @@ -70,7 +70,7 @@ file_size_from_path :: proc(path: string) -> i64 { return length } -read_entire_file :: proc(name: string, allocator := context.allocator) -> (data: []byte, success: bool) { +read_entire_file_from_filename :: proc(name: string, allocator := context.allocator) -> (data: []byte, success: bool) { context.allocator = allocator fd, err := open(name, O_RDONLY, 0) @@ -79,26 +79,38 @@ read_entire_file :: proc(name: string, allocator := context.allocator) -> (data: } defer close(fd) - length: i64 - if length, err = file_size(fd); err != 0 { - return nil, false - } + return read_entire_file_from_handle(fd, allocator) +} - if length <= 0 { - return nil, true - } +read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator) -> (data: []byte, success: bool) { + context.allocator = allocator - data = make([]byte, int(length)) - if data == nil { - return nil, false - } + length: i64 + err: Errno + if length, err = file_size(fd); err != 0 { + return nil, false + } - bytes_read, read_err := read(fd, data) - if read_err != ERROR_NONE { - delete(data) - return nil, false - } - return data[:bytes_read], true + if length <= 0 { + return nil, true + } + + data = make([]byte, int(length), allocator) + if data == nil { + return nil, false + } + + bytes_read, read_err := read(fd, data) + if read_err != ERROR_NONE { + delete(data) + return nil, false + } + return data[:bytes_read], true +} + +read_entire_file :: proc { + read_entire_file_from_filename, + read_entire_file_from_handle, } write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {