mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Add os.read_at_least and os_read_full utility procedures.
This commit is contained in:
+20
-1
@@ -55,6 +55,25 @@ write_encoded_rune :: proc(fd: Handle, r: rune) {
|
|||||||
write_byte(fd, '\'')
|
write_byte(fd, '\'')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
read_at_least :: proc(fd: Handle, buf: []byte, min: int) -> (n: int, err: Errno) {
|
||||||
|
if len(buf) < min {
|
||||||
|
return 0, -1
|
||||||
|
}
|
||||||
|
for n < min && err == 0 {
|
||||||
|
nn: int
|
||||||
|
nn, err = read(fd, buf[n:])
|
||||||
|
n += nn
|
||||||
|
}
|
||||||
|
if n >= min {
|
||||||
|
err = 0
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
read_full :: proc(fd: Handle, buf: []byte) -> (n: int, err: Errno) {
|
||||||
|
return read_at_least(fd, buf, len(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
@@ -100,7 +119,7 @@ read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator)
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes_read, read_err := read(fd, data)
|
bytes_read, read_err := read_full(fd, data)
|
||||||
if read_err != ERROR_NONE {
|
if read_err != ERROR_NONE {
|
||||||
delete(data)
|
delete(data)
|
||||||
return nil, false
|
return nil, false
|
||||||
|
|||||||
Reference in New Issue
Block a user