diff options
Diffstat (limited to 'FileSystem.c')
-rwxr-xr-x | FileSystem.c | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/FileSystem.c b/FileSystem.c index b691746..cb64fe5 100755 --- a/FileSystem.c +++ b/FileSystem.c @@ -1,13 +1,17 @@ #include "FileSystem.h" -FILE *FileOpen(const char *path) +#include <string.h> + +#include <sys/stat.h> + +FILE *FileOpen(const char *path, const bool write) { if (!path) { return NULL; } - return fopen(path, "rb"); + return fopen(path, write ? "wb" : "rb"); } bool FileClose(FILE *file) @@ -30,6 +34,16 @@ bool FileRead(FILE *file, void *dst, const size_t size) return fread(dst, 1, size, file) == size; } +bool FileWrite(FILE *file, const void *src, const size_t size) +{ + if (!file || !src || size == 0) + { + return false; + } + + return fwrite(src, 1, size, file) == size; +} + bool FileSeek(FILE *file, const size_t offset) { if (!file) @@ -39,3 +53,40 @@ bool FileSeek(FILE *file, const size_t offset) return fseek(file, offset, SEEK_SET) == 0; } + +size_t FileSize(const char *path) +{ + if (!path) + { + return 0; + } + + struct stat st; + if (stat(path, &st) == -1) + { + return 0; + } + + return st.st_size; +} + +const char *PathRelativeToBase(const char *full, const char *base) +{ + if (!full || !base) + { + return NULL; + } + + if (strstr(full, base) != &full[0]) + { + return NULL; + } + + full += strlen(base); + if (full[0] == '/') + { + ++full; + } + + return full; +} |