mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
Better using; foreign system libraries; optional semicolons
This commit is contained in:
+44
-44
@@ -1,37 +1,37 @@
|
||||
#load "win32.odin"
|
||||
|
||||
File :: type struct {
|
||||
Handle :: type HANDLE;
|
||||
handle: Handle;
|
||||
Handle :: type HANDLE
|
||||
handle: Handle
|
||||
}
|
||||
|
||||
file_open :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte;
|
||||
_ = copy(buf[:], name as []byte);
|
||||
f := File{CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null)};
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle;
|
||||
buf: [300]byte
|
||||
_ = copy(buf[:], name as []byte)
|
||||
f := File{CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null)}
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
|
||||
|
||||
return f, success;
|
||||
return f, success
|
||||
}
|
||||
|
||||
file_create :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte;
|
||||
_ = copy(buf[:], name as []byte);
|
||||
buf: [300]byte
|
||||
_ = copy(buf[:], name as []byte)
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null),
|
||||
};
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle;
|
||||
return f, success;
|
||||
}
|
||||
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
|
||||
return f, success
|
||||
}
|
||||
|
||||
|
||||
file_close :: proc(f: ^File) {
|
||||
CloseHandle(f.handle);
|
||||
CloseHandle(f.handle)
|
||||
}
|
||||
|
||||
file_write :: proc(f: ^File, buf: []byte) -> bool {
|
||||
bytes_written: i32;
|
||||
return WriteFile(f.handle, ^buf[0], len(buf) as i32, ^bytes_written, null) != 0;
|
||||
bytes_written: i32
|
||||
return WriteFile(f.handle, ^buf[0], len(buf) as i32, ^bytes_written, null) != 0
|
||||
}
|
||||
|
||||
FileStandard :: type enum {
|
||||
@@ -47,60 +47,60 @@ __std_files: [FileStandard.COUNT as int]File;
|
||||
file_get_standard :: proc(std: FileStandard) -> ^File {
|
||||
// using FileStandard;
|
||||
if (!__std_file_set) {
|
||||
using FileStandard;
|
||||
__std_files[INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE);
|
||||
__std_files[OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
__std_files[ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
__std_file_set = true;
|
||||
using FileStandard
|
||||
__std_files[INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE)
|
||||
__std_files[OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||
__std_files[ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE)
|
||||
__std_file_set = true
|
||||
}
|
||||
return ^__std_files[std];
|
||||
return ^__std_files[std]
|
||||
}
|
||||
|
||||
|
||||
read_entire_file :: proc(name: string) -> (string, bool) {
|
||||
buf: [300]byte;
|
||||
_ = copy(buf[:], name as []byte);
|
||||
c_string := ^buf[0];
|
||||
buf: [300]byte
|
||||
_ = copy(buf[:], name as []byte)
|
||||
c_string := ^buf[0]
|
||||
|
||||
|
||||
f, file_ok := file_open(name);
|
||||
f, file_ok := file_open(name)
|
||||
if !file_ok {
|
||||
return "", false;
|
||||
return "", false
|
||||
}
|
||||
defer file_close(^f);
|
||||
defer file_close(^f)
|
||||
|
||||
length: i64;
|
||||
file_size_ok := GetFileSizeEx(f.handle as HANDLE, ^length) != 0;
|
||||
length: i64
|
||||
file_size_ok := GetFileSizeEx(f.handle as HANDLE, ^length) != 0
|
||||
if !file_size_ok {
|
||||
return "", false;
|
||||
return "", false
|
||||
}
|
||||
|
||||
data := new_slice(u8, length);
|
||||
data := new_slice(u8, length)
|
||||
if ^data[0] == null {
|
||||
return "", false;
|
||||
return "", false
|
||||
}
|
||||
|
||||
single_read_length: i32;
|
||||
total_read: i64;
|
||||
single_read_length: i32
|
||||
total_read: i64
|
||||
|
||||
for total_read < length {
|
||||
remaining := length - total_read;
|
||||
to_read: u32;
|
||||
MAX :: 0x7fffffff;
|
||||
remaining := length - total_read
|
||||
to_read: u32
|
||||
MAX :: 0x7fffffff
|
||||
if remaining <= MAX {
|
||||
to_read = remaining as u32;
|
||||
to_read = remaining as u32
|
||||
} else {
|
||||
to_read = MAX;
|
||||
to_read = MAX
|
||||
}
|
||||
|
||||
ReadFile(f.handle as HANDLE, ^data[total_read], to_read, ^single_read_length, null);
|
||||
ReadFile(f.handle as HANDLE, ^data[total_read], to_read, ^single_read_length, null)
|
||||
if single_read_length <= 0 {
|
||||
delete(data);
|
||||
return "", false;
|
||||
delete(data)
|
||||
return "", false
|
||||
}
|
||||
|
||||
total_read += single_read_length as i64;
|
||||
total_read += single_read_length as i64
|
||||
}
|
||||
|
||||
return data as string, true;
|
||||
return data as string, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user