mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
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:
+30
-18
@@ -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,26 +79,38 @@ read_entire_file :: proc(name: string, allocator := context.allocator) -> (data:
|
|||||||
}
|
}
|
||||||
defer close(fd)
|
defer close(fd)
|
||||||
|
|
||||||
length: i64
|
return read_entire_file_from_handle(fd, allocator)
|
||||||
if length, err = file_size(fd); err != 0 {
|
}
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
if length <= 0 {
|
read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator) -> (data: []byte, success: bool) {
|
||||||
return nil, true
|
context.allocator = allocator
|
||||||
}
|
|
||||||
|
|
||||||
data = make([]byte, int(length))
|
length: i64
|
||||||
if data == nil {
|
err: Errno
|
||||||
return nil, false
|
if length, err = file_size(fd); err != 0 {
|
||||||
}
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
bytes_read, read_err := read(fd, data)
|
if length <= 0 {
|
||||||
if read_err != ERROR_NONE {
|
return nil, true
|
||||||
delete(data)
|
}
|
||||||
return nil, false
|
|
||||||
}
|
data = make([]byte, int(length), allocator)
|
||||||
return data[:bytes_read], true
|
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) {
|
write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
|
||||||
|
|||||||
Reference in New Issue
Block a user