diff options
| author | nasr <nsrddyn@gmail.com> | 2026-03-17 18:08:47 +0000 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-03-17 18:08:47 +0000 |
| commit | 3f6096c322192c7024d5eae8b21a3e5f24f6903e (patch) | |
| tree | 60c422407611dbfd328e0b0e1404de2c0c2e960c /source/base/base_os.h | |
| parent | e0722ef001c4cc41827dd83dad13d42d342d03ae (diff) | |
feature(base): file writing using string struct
not the ideal way i think. i thought handmade hero had a different way
but will take a look at that later
Diffstat (limited to 'source/base/base_os.h')
| -rw-r--r-- | source/base/base_os.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/source/base/base_os.h b/source/base/base_os.h index 9397fbb..388f665 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h | |||
| @@ -34,4 +34,34 @@ load_file(mem_arena *arena, const char *path) | |||
| 34 | return result; | 34 | return result; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | internal string8 | ||
| 38 | write_file(const char *path, string8 data) | ||
| 39 | { | ||
| 40 | |||
| 41 | string8 result = {0}; | ||
| 42 | s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
| 43 | if(file == -1) | ||
| 44 | { | ||
| 45 | warn("failed to open file for writing. path could be invalid"); | ||
| 46 | return (string8){0}; | ||
| 47 | } | ||
| 48 | |||
| 49 | u64 written = 0; | ||
| 50 | while(written < data.size) | ||
| 51 | { | ||
| 52 | s64 err = write(file, data.data + written, data.size - written); | ||
| 53 | if(err == -1) | ||
| 54 | { | ||
| 55 | warn("write syscall failed"); | ||
| 56 | close(file); | ||
| 57 | return (string8){0}; | ||
| 58 | } | ||
| 59 | written += err; | ||
| 60 | } | ||
| 61 | |||
| 62 | close(file); | ||
| 63 | result = data; | ||
| 64 | return result; | ||
| 65 | } | ||
| 66 | |||
| 37 | #endif /* BASE_OS_H */ | 67 | #endif /* BASE_OS_H */ |
