summaryrefslogtreecommitdiff
path: root/src/platform
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform')
-rw-r--r--src/platform/file.c30
-rw-r--r--src/platform/file.h9
-rw-r--r--src/platform/path.c20
-rw-r--r--src/platform/path.h16
4 files changed, 74 insertions, 1 deletions
diff --git a/src/platform/file.c b/src/platform/file.c
index 44aa9d0..6030620 100644
--- a/src/platform/file.c
+++ b/src/platform/file.c
@@ -60,4 +60,34 @@ str8_opt str8_from_file(arena *a, str8 path) {
result.has_value = true;
return result;
+}
+
+FileData load_spv_file(const char *path) {
+ FILE *f = fopen(path, "rb");
+ if (f == NULL) {
+ perror("Error opening file");
+ return (FileData){ NULL, 0 };
+ }
+
+ fseek(f, 0, SEEK_END);
+ long fsize = ftell(f);
+ rewind(f);
+
+ char *data = (char *)malloc(fsize);
+ if (data == NULL) {
+ perror("Memory allocation failed");
+ fclose(f);
+ return (FileData){ NULL, 0 };
+ }
+
+ size_t bytesRead = fread(data, 1, fsize, f);
+ if (bytesRead < fsize) {
+ perror("Failed to read the entire file");
+ free(data);
+ fclose(f);
+ return (FileData){ NULL, 0 };
+ }
+
+ fclose(f);
+ return (FileData){ data, bytesRead };
} \ No newline at end of file
diff --git a/src/platform/file.h b/src/platform/file.h
index 8bb22c8..a8aa8ea 100644
--- a/src/platform/file.h
+++ b/src/platform/file.h
@@ -16,4 +16,11 @@ typedef struct 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
+str8_opt str8_from_file(arena* a, str8 path);
+
+typedef struct {
+ char* data;
+ size_t size;
+} FileData;
+
+FileData load_spv_file(const char* path); \ No newline at end of file
diff --git a/src/platform/path.c b/src/platform/path.c
new file mode 100644
index 0000000..9572941
--- /dev/null
+++ b/src/platform/path.c
@@ -0,0 +1,20 @@
+#include "path.h"
+
+#include <libgen.h>
+#include <stdlib.h>
+#include <string.h>
+#include "mem.h"
+#include "str.h"
+
+#if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC)
+path_opt path_parent(arena* a, const char* path) {
+ // Duplicate the string because dirname doesnt like const literals
+ char* path_copy = arena_alloc(a, strlen(path) + 1);
+ strcpy(path_copy, path);
+ char* path_dirname = dirname(path_copy);
+ return (path_opt){ .path = str8_cstr_view(path_dirname), .has_value = true };
+}
+#endif
+#ifdef CEL_PLATFORM_WINDOWS
+// TODO: path_opt path_parent(const char* path)
+#endif \ No newline at end of file
diff --git a/src/platform/path.h b/src/platform/path.h
new file mode 100644
index 0000000..73063ea
--- /dev/null
+++ b/src/platform/path.h
@@ -0,0 +1,16 @@
+/**
+ * @file path.h
+ * @brief
+ * @date 2024-03-11
+ * @copyright Copyright (c) 2024
+ */
+#pragma once
+
+#include "str.h"
+
+typedef struct path_opt {
+ str8 path;
+ bool has_value;
+} path_opt;
+
+path_opt path_parent(arena* a, const char* path); // TODO: convert to using str8 \ No newline at end of file