summaryrefslogtreecommitdiff
path: root/src/platform/file.c
diff options
context:
space:
mode:
authorOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 12:50:39 +1100
committerOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 12:50:39 +1100
commit80a734de03eaf3d534f95010653c47bf54389f48 (patch)
tree2b75a7516116e5e66ead39b48bd1ceb6b67a4af3 /src/platform/file.c
parent4b9ab2ec5acf2ecf2e96c38733a835b964664171 (diff)
new string function signatures just dropped
Diffstat (limited to 'src/platform/file.c')
-rw-r--r--src/platform/file.c31
1 files changed, 31 insertions, 0 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