Add read_entire_file_or_err and write_entire_file_or_err

This commit is contained in:
gingerBill
2024-08-04 14:41:55 +01:00
parent e8d26c5797
commit fff1d509d5
+57 -37
View File
@@ -91,6 +91,7 @@ read_full :: proc(fd: Handle, buf: []byte) -> (n: int, err: Error) {
} }
@(require_results)
file_size_from_path :: proc(path: string) -> i64 { file_size_from_path :: proc(path: string) -> i64 {
fd, err := open(path, O_RDONLY, 0) fd, err := open(path, O_RDONLY, 0)
if err != nil { if err != nil {
@@ -105,42 +106,20 @@ file_size_from_path :: proc(path: string) -> i64 {
return length return length
} }
@(require_results)
read_entire_file_from_filename :: proc(name: string, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) { read_entire_file_from_filename :: proc(name: string, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
context.allocator = allocator err: Error
data, err = read_entire_file_from_filename_or_err(name, allocator, loc)
fd, err := open(name, O_RDONLY, 0) success = err == nil
if err != nil { return
return nil, false
}
defer close(fd)
return read_entire_file_from_handle(fd, allocator, loc)
} }
@(require_results)
read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) { read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
context.allocator = allocator
length: i64
err: Error err: Error
if length, err = file_size(fd); err != nil { data, err = read_entire_file_from_handle_or_err(fd, allocator, loc)
return nil, false success = err == nil
} return
if length <= 0 {
return nil, true
}
data, err = make([]byte, int(length), allocator, loc)
if data == nil || err != nil {
return nil, false
}
bytes_read, read_err := read_full(fd, data)
if read_err != nil {
delete(data)
return nil, false
}
return data[:bytes_read], true
} }
read_entire_file :: proc { read_entire_file :: proc {
@@ -148,7 +127,50 @@ read_entire_file :: proc {
read_entire_file_from_handle, read_entire_file_from_handle,
} }
@(require_results)
read_entire_file_from_filename_or_err :: proc(name: string, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Error) {
context.allocator = allocator
fd := open(name, O_RDONLY, 0) or_return
defer close(fd)
return read_entire_file_from_handle_or_err(fd, allocator, loc)
}
@(require_results)
read_entire_file_from_handle_or_err :: proc(fd: Handle, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Error) {
context.allocator = allocator
length := file_size(fd) or_return
if length <= 0 {
return nil, nil
}
data = make([]byte, int(length), allocator, loc) or_return
if data == nil {
return nil, nil
}
defer if err != nil {
delete(data, allocator)
}
bytes_read := read_full(fd, data) or_return
data = data[:bytes_read]
return
}
read_entire_file_or_err :: proc {
read_entire_file_from_filename_or_err,
read_entire_file_from_handle_or_err,
}
write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) { write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
return write_entire_file_or_err(name, data, truncate) == nil
}
@(require_results)
write_entire_file_or_err :: proc(name: string, data: []byte, truncate := true) -> Error {
flags: int = O_WRONLY|O_CREATE flags: int = O_WRONLY|O_CREATE
if truncate { if truncate {
flags |= O_TRUNC flags |= O_TRUNC
@@ -160,14 +182,11 @@ write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (succ
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
} }
fd, err := open(name, flags, mode) fd := open(name, flags, mode) or_return
if err != nil {
return false
}
defer close(fd) defer close(fd)
_, write_err := write(fd, data) _ = write(fd, data) or_return
return write_err == nil return nil
} }
write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Error) { write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Error) {
@@ -185,6 +204,7 @@ heap_alloc :: runtime.heap_alloc
heap_resize :: runtime.heap_resize heap_resize :: runtime.heap_resize
heap_free :: runtime.heap_free heap_free :: runtime.heap_free
@(require_results)
processor_core_count :: proc() -> int { processor_core_count :: proc() -> int {
return _processor_core_count() return _processor_core_count()
} }