From 9d09d66a273f68fae7efb71504bf40c664b91983 Mon Sep 17 00:00:00 2001 From: nasr Date: Mon, 13 Apr 2026 15:33:05 +0200 Subject: feature(main): init feature(main): init feature(main): init feature(main): init --- source/base/base_os.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 source/base/base_os.h (limited to 'source/base/base_os.h') diff --git a/source/base/base_os.h b/source/base/base_os.h new file mode 100644 index 0000000..388f665 --- /dev/null +++ b/source/base/base_os.h @@ -0,0 +1,67 @@ +#ifndef BASE_OS_H +#define BASE_OS_H + +internal string8 +load_file(mem_arena *arena, const char *path) +{ + string8 result = {0}; + struct stat sbuf = {0}; + + s32 file = open(path, O_RDONLY); + if(file == -1) + { + warn("fialed to open file. path could be invalid"); + return (string8){0}; + } + + if(fstat(file, &sbuf) == -1) + { + warn("error: fstat failed"); + close(file); + return (string8){0}; + } + + + result = PushString(arena, sbuf.st_size); + + result.size = (u64)sbuf.st_size; + if(result.size != 0) + { + result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0); + } + + close(file); + return result; +} + +internal string8 +write_file(const char *path, string8 data) +{ + + string8 result = {0}; + s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if(file == -1) + { + warn("failed to open file for writing. path could be invalid"); + return (string8){0}; + } + + u64 written = 0; + while(written < data.size) + { + s64 err = write(file, data.data + written, data.size - written); + if(err == -1) + { + warn("write syscall failed"); + close(file); + return (string8){0}; + } + written += err; + } + + close(file); + result = data; + return result; +} + +#endif /* BASE_OS_H */ -- cgit v1.3