summaryrefslogtreecommitdiff
path: root/src/platform/file.c
diff options
context:
space:
mode:
authorOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 17:36:36 +1100
committerOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 17:36:36 +1100
commit06ed6edcedf30fd8f7e036fffe8f81e8ca89d4b0 (patch)
treef5ae75e431817a53ee5725b4c0d6f3e61b15a664 /src/platform/file.c
parent109b6dd1881d90915e972f0d263a032bd262adb5 (diff)
parentb5b7cea24c46d28a4b72bc18ca8ccb2d532007d3 (diff)
Merge branch 'master' of github.com:omnisci3nce/celeritas-core
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