diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-05 12:48:05 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-05 12:48:05 +1000 |
commit | dfb6adbcbcc7d50b770b6d5ea82efdd8f8c32e25 (patch) | |
tree | a470b91a90716d7ea46fde53ed395449c24583a2 /archive/src/platform | |
parent | 54354e32c6498cc7f8839ab4deb1208d37216cc5 (diff) |
delete documentation workflow
Diffstat (limited to 'archive/src/platform')
-rw-r--r-- | archive/src/platform/file.c | 93 | ||||
-rw-r--r-- | archive/src/platform/file.h | 26 | ||||
-rw-r--r-- | archive/src/platform/platform.h | 37 | ||||
-rw-r--r-- | archive/src/platform/platform_unix.c | 16 | ||||
-rw-r--r-- | archive/src/platform/platform_windows.c | 22 |
5 files changed, 194 insertions, 0 deletions
diff --git a/archive/src/platform/file.c b/archive/src/platform/file.c new file mode 100644 index 0000000..91daa4f --- /dev/null +++ b/archive/src/platform/file.c @@ -0,0 +1,93 @@ +#include "file.h" + +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdio.h> +#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"); + if (f == NULL) { + ERROR("Error reading file: %s. errno: %d", path, errno); + return NULL; + } + if (ferror(f)) { + ERROR("Error reading file: %s. errno: %d", path, errno); + return NULL; + } + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + rewind(f); + + char* string = malloc(fsize + 1); + fread(string, fsize, 1, f); + fclose(f); + + 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; +} + +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 }; +} diff --git a/archive/src/platform/file.h b/archive/src/platform/file.h new file mode 100644 index 0000000..5e5e1e1 --- /dev/null +++ b/archive/src/platform/file.h @@ -0,0 +1,26 @@ +/** + * @file file.h + * @brief File I/O utilities + * @date 2024-02-24 + * @copyright Copyright (c) 2024 + */ +#pragma once + +#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); + +typedef struct { + char* data; + size_t size; +} FileData; + +FileData load_spv_file(const char* path); diff --git a/archive/src/platform/platform.h b/archive/src/platform/platform.h new file mode 100644 index 0000000..c2be630 --- /dev/null +++ b/archive/src/platform/platform.h @@ -0,0 +1,37 @@ +#pragma once + +#include "defines.h" +#include "str.h" + +// -- Paths +typedef struct path_opt { + Str8 path; + bool has_value; +} path_opt; + +// TODO: convert to using str8 +// TODO: use uppercase code style +path_opt path_parent(arena* a, const char* path); + +// --- Threads +typedef struct CelThread CelThread; + +CelThread Thread_Create(); +void Thread_Destroy(CelThread* thread); + +// --- Mutexes +typedef struct CelMutex CelMutex; + +CelMutex Mutex_Create(); +void Mutex_Destroy(CelMutex* mutex); + +/** @brief Blocks until the mutex can be acquired. if returns false then an error occurred and can + * be checked (TODO) */ +bool Mutex_Lock(CelMutex* mutex); + +/** @brief Tries to acquire the mutex like `mutex_lock` but returns immediately if the mutex has + * already been locked */ +bool Mutex_TryLock(CelMutex* mutex); + +/** @brief Releases a mutex. If it is already unlocked then does nothing */ +void Mutex_Unlock(CelMutex* mutex); diff --git a/archive/src/platform/platform_unix.c b/archive/src/platform/platform_unix.c new file mode 100644 index 0000000..86ac170 --- /dev/null +++ b/archive/src/platform/platform_unix.c @@ -0,0 +1,16 @@ +#include "platform.h" + +#if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC) + +#include <libgen.h> +#include <string.h> + +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 diff --git a/archive/src/platform/platform_windows.c b/archive/src/platform/platform_windows.c new file mode 100644 index 0000000..21ef359 --- /dev/null +++ b/archive/src/platform/platform_windows.c @@ -0,0 +1,22 @@ +#include "platform.h" + +#if defined(CEL_PLATFORM_WINDOWS) + +#include <shlwapi.h> +#include <windows.h> +#pragma comment(lib, "Shlwapi.lib") + +path_opt path_parent(arena* a, const char* path) { + // Duplicate the string because PathRemoveFileSpec mutates in-place + size_t len = strlen(path) + 1; + char* path_copy = arena_alloc(a, len); + strcpy_s(path_copy, len, path); + + if (PathRemoveFileSpecA(path_copy)) { + return (path_opt){ .path = Str8_cstr_view(path_copy), .has_value = true }; + } else { + return (path_opt){ .has_value = false }; + } +} + +#endif |