|
@@ -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,50 +106,71 @@ 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) {
|
|
|
|
+ err: Error
|
|
|
|
+ data, err = read_entire_file_from_filename_or_err(name, allocator, loc)
|
|
|
|
+ success = err == nil
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+@(require_results)
|
|
|
|
+read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
|
|
|
|
+ err: Error
|
|
|
|
+ data, err = read_entire_file_from_handle_or_err(fd, allocator, loc)
|
|
|
|
+ success = err == nil
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+read_entire_file :: proc {
|
|
|
|
+ read_entire_file_from_filename,
|
|
|
|
+ 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
|
|
context.allocator = allocator
|
|
|
|
|
|
- fd, err := open(name, O_RDONLY, 0)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, false
|
|
|
|
- }
|
|
|
|
|
|
+ fd := open(name, O_RDONLY, 0) or_return
|
|
defer close(fd)
|
|
defer close(fd)
|
|
|
|
|
|
- return read_entire_file_from_handle(fd, allocator, loc)
|
|
|
|
|
|
+ return read_entire_file_from_handle_or_err(fd, allocator, loc)
|
|
}
|
|
}
|
|
|
|
|
|
-read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
|
|
|
|
|
|
+@(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
|
|
context.allocator = allocator
|
|
|
|
|
|
- length: i64
|
|
|
|
- err: Error
|
|
|
|
- if length, err = file_size(fd); err != nil {
|
|
|
|
- return nil, false
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ length := file_size(fd) or_return
|
|
if length <= 0 {
|
|
if length <= 0 {
|
|
- return nil, true
|
|
|
|
|
|
+ return nil, nil
|
|
}
|
|
}
|
|
|
|
|
|
- data, err = make([]byte, int(length), allocator, loc)
|
|
|
|
- if data == nil || err != nil {
|
|
|
|
- return nil, false
|
|
|
|
|
|
+ data = make([]byte, int(length), allocator, loc) or_return
|
|
|
|
+ if data == nil {
|
|
|
|
+ return nil, nil
|
|
}
|
|
}
|
|
-
|
|
|
|
- bytes_read, read_err := read_full(fd, data)
|
|
|
|
- if read_err != nil {
|
|
|
|
- delete(data)
|
|
|
|
- return nil, false
|
|
|
|
|
|
+ defer if err != nil {
|
|
|
|
+ delete(data, allocator)
|
|
}
|
|
}
|
|
- return data[:bytes_read], true
|
|
|
|
|
|
+
|
|
|
|
+ bytes_read := read_full(fd, data) or_return
|
|
|
|
+ data = data[:bytes_read]
|
|
|
|
+ return
|
|
}
|
|
}
|
|
|
|
|
|
-read_entire_file :: proc {
|
|
|
|
- read_entire_file_from_filename,
|
|
|
|
- read_entire_file_from_handle,
|
|
|
|
|
|
+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)
|
|
|
|
- if err != nil {
|
|
|
|
- return false
|
|
|
|
- }
|
|
|
|
|
|
+ fd := open(name, flags, mode) or_return
|
|
defer close(fd)
|
|
defer close(fd)
|
|
|
|
|
|
- _, write_err := write(fd, data)
|
|
|
|
- return write_err == nil
|
|
|
|
|
|
+ _ = write(fd, data) or_return
|
|
|
|
+ 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()
|
|
}
|
|
}
|