parent
6d56ff2bb1
commit
636966616a
@ -0,0 +1,75 @@ |
||||
#include <assert.h> |
||||
#include <stdlib.h> |
||||
|
||||
#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; |
||||
} |
@ -0,0 +1,12 @@ |
||||
#ifndef file_io_H |
||||
#define file_io_H |
||||
|
||||
#include <stdbool.h> |
||||
#include <stdio.h> |
||||
|
||||
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 |
@ -0,0 +1,12 @@ |
||||
#ifndef num_types_H |
||||
#define num_types_H |
||||
|
||||
#include <stdint.h> |
||||
|
||||
typedef int32_t int32; |
||||
typedef int64_t int64; |
||||
typedef unsigned int uint; |
||||
typedef uint32_t uint32; |
||||
typedef uint64_t uint64; |
||||
|
||||
#endif |
Loading…
Reference in new issue