diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-25 12:50:39 +1100 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-25 12:50:39 +1100 |
commit | 80a734de03eaf3d534f95010653c47bf54389f48 (patch) | |
tree | 2b75a7516116e5e66ead39b48bd1ceb6b67a4af3 /src/platform | |
parent | 4b9ab2ec5acf2ecf2e96c38733a835b964664171 (diff) |
new string function signatures just dropped
Diffstat (limited to 'src/platform')
-rw-r--r-- | src/platform/file.c | 31 | ||||
-rw-r--r-- | src/platform/file.h | 10 |
2 files changed, 40 insertions, 1 deletions
diff --git a/src/platform/file.c b/src/platform/file.c index 45c5b30..44aa9d0 100644 --- a/src/platform/file.c +++ b/src/platform/file.c @@ -7,6 +7,8 @@ #include <stdlib.h> #include "log.h" +#include "mem.h" +#include "str.h" const char *string_from_file(const char *path) { FILE *f = fopen(path, "rb"); @@ -29,4 +31,33 @@ const char *string_from_file(const char *path) { string[fsize] = '\0'; return string; +} + +str8_opt str8_from_file(arena *a, str8 path) { + char *p = cstr(a, path); + str8_opt result = { .has_value = false }; + + FILE *f = fopen(p, "rb"); + if (f == NULL) { + ERROR("Error reading file: %s. errno: %d", path, errno); + return result; + } + if (ferror(f)) { + ERROR("Error reading file: %s. errno: %d", path, errno); + return result; + } + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + rewind(f); + + u8 *raw = arena_alloc(a, fsize + 1); + str8 contents = str8_create(raw, fsize); + contents.buf[contents.len] = '\0'; + + fread(raw, fsize, 1, f); + fclose(f); + result.contents = contents; + result.has_value = true; + + return result; }
\ No newline at end of file diff --git a/src/platform/file.h b/src/platform/file.h index b965ceb..fa09891 100644 --- a/src/platform/file.h +++ b/src/platform/file.h @@ -6,6 +6,14 @@ */ #pragma once -#include <stdbool.h> +#include "defines.h" +#include "str.h" + +typedef struct str8_opt { + str8 contents; + bool has_value; +} str8_opt; const char* string_from_file(const char* path); + +str8_opt str8_from_file(arena* a, str8 path);
\ No newline at end of file |