diff --git a/src/file_io.c b/src/file_io.c new file mode 100644 index 0000000..fd80f65 --- /dev/null +++ b/src/file_io.c @@ -0,0 +1,75 @@ +#include +#include + +#include "file_io.h" +#include "log.h" +#include "string_utils.h" + +static char* base_assets_path; + +void io_file_initialize(const char* assets_path) +{ + base_assets_path = str_new(assets_path); +} + +void io_file_cleanup(void) +{ + if(base_assets_path) free(base_assets_path); +} + +char* io_file_read(const char* path) +{ + /* Make path relative to base assets folder path */ + char* relative_path = str_new(base_assets_path); + relative_path = str_concat(relative_path, path); + + FILE* file = fopen(relative_path, "r"); + char* data = NULL; + if(file) + { + int rc = fseek(file, 0, SEEK_END); + if(rc == 0) + { + size_t size = (size_t)ftell(file); + rewind(file); + data = malloc(sizeof(char) * size + 1); + if(data) + { + if(fread(data, size, 1, file) > 0) + { + if(data[size] != '\0') data[size] = '\0'; + } + else + { + log_error("io:file_read", "fread failed"); + free(data); + } + } + else + { + log_error("io:file_read", "malloc failed"); + } + } + else + { + log_error("io:file_read", "fseek failed"); + } + fclose(file); + } + else + { + log_error("io:file_read", "File '%s' not found", relative_path); + } + + free(relative_path); + return data; +} + +FILE* io_file_open(const char* path, const char* mode) +{ + char* relative_path = str_new(base_assets_path); + relative_path = str_concat(relative_path, path); + FILE* file = fopen(relative_path, mode); + if(!file) log_error("io:file", "Fialed to open file '%s'", relative_path); + return file; +} diff --git a/src/file_io.h b/src/file_io.h new file mode 100644 index 0000000..5fe3203 --- /dev/null +++ b/src/file_io.h @@ -0,0 +1,12 @@ +#ifndef file_io_H +#define file_io_H + +#include +#include + +void io_file_initialize(const char* assets_path); +char* io_file_read(const char* path); +FILE* io_file_open(const char* path, const char* mode); +void io_file_cleanup(void); + +#endif diff --git a/src/num_types.h b/src/num_types.h new file mode 100644 index 0000000..2559886 --- /dev/null +++ b/src/num_types.h @@ -0,0 +1,12 @@ +#ifndef num_types_H +#define num_types_H + +#include + +typedef int32_t int32; +typedef int64_t int64; +typedef unsigned int uint; +typedef uint32_t uint32; +typedef uint64_t uint64; + +#endif