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

This commit is contained in:
zhibog
2021-10-16 18:45:25 +02:00
parent f48ee00c2f
commit c632125d82
+14 -2
View File
@@ -70,7 +70,7 @@ file_size_from_path :: proc(path: string) -> i64 {
return length 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 context.allocator = allocator
fd, err := open(name, O_RDONLY, 0) fd, err := open(name, O_RDONLY, 0)
@@ -79,7 +79,14 @@ read_entire_file :: proc(name: string, allocator := context.allocator) -> (data:
} }
defer close(fd) defer close(fd)
return read_entire_file_from_handle(fd, allocator)
}
read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator) -> (data: []byte, success: bool) {
context.allocator = allocator
length: i64 length: i64
err: Errno
if length, err = file_size(fd); err != 0 { if length, err = file_size(fd); err != 0 {
return nil, false return nil, false
} }
@@ -88,7 +95,7 @@ read_entire_file :: proc(name: string, allocator := context.allocator) -> (data:
return nil, true return nil, true
} }
data = make([]byte, int(length)) data = make([]byte, int(length), allocator)
if data == nil { if data == nil {
return nil, false return nil, false
} }
@@ -101,6 +108,11 @@ read_entire_file :: proc(name: string, allocator := context.allocator) -> (data:
return data[:bytes_read], true 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) { write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
flags: int = O_WRONLY|O_CREATE flags: int = O_WRONLY|O_CREATE
if truncate { if truncate {