all repos — mgba @ 43f9f2dfd314841156f5b0f8fb6ed1478ff29dd9

mGBA Game Boy Advance Emulator

VFS: VFileOpen can now have a swappable backend
Jeffrey Pfau jeffrey@endrift.com
Sat, 20 Jun 2015 03:11:11 -0700
commit

43f9f2dfd314841156f5b0f8fb6ed1478ff29dd9

parent

2bb16fd0a8cc7536085bc9172c515a613c2333e1

4 files changed, 37 insertions(+), 1 deletions(-)

jump to
M src/util/vfs.csrc/util/vfs.c

@@ -5,6 +5,36 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this

* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "vfs.h" +struct VFile* VFileOpen(const char* path, int flags) { +#ifdef USE_VFS_FILE + const char* chflags; + switch (flags & O_ACCMODE) { + case O_WRONLY: + if (flags & O_APPEND) { + chflags = "ab"; + } else { + chflags = "wb"; + } + break; + case O_RDWR: + if (flags & O_APPEND) { + chflags = "a+b"; + } else if (flags & O_TRUNC) { + chflags = "w+b"; + } else { + chflags = "r+b"; + } + break; + case O_RDONLY: + chflags = "rb"; + break; + } + return VFileFOpen(path, chflags); +#else + return VFileOpenFD(path, flags); +#endif +} + ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) { ssize_t bytesRead = 0; while (bytesRead < size - 1) {
M src/util/vfs.hsrc/util/vfs.h

@@ -53,6 +53,8 @@ struct VFile* (*openFile)(struct VDir* vd, const char* name, int mode);

}; struct VFile* VFileOpen(const char* path, int flags); + +struct VFile* VFileOpenFD(const char* path, int flags); struct VFile* VFileFOpen(const char* path, const char* mode); struct VFile* VFileFromFD(int fd); struct VFile* VFileFromMemory(void* mem, size_t size);
M src/util/vfs/vfs-fd.csrc/util/vfs/vfs-fd.c

@@ -29,7 +29,7 @@ static void _vfdUnmap(struct VFile* vf, void* memory, size_t size);

static void _vfdTruncate(struct VFile* vf, size_t size); static ssize_t _vfdSize(struct VFile* vf); -struct VFile* VFileOpen(const char* path, int flags) { +struct VFile* VFileOpenFD(const char* path, int flags) { if (!path) { return 0; }
M src/util/vfs/vfs-file.csrc/util/vfs/vfs-file.c

@@ -7,6 +7,7 @@ #include "util/vfs.h"

#include "util/memory.h" +#include <errno.h> #include <stdio.h> struct VFileFILE {

@@ -28,6 +29,9 @@ if (!path && !mode) {

return 0; } FILE* file = fopen(path, mode); + if (!file && errno == ENOENT && strcmp(mode, "r+b") == 0) { + file = fopen(path, "w+b"); + } return VFileFromFILE(file); }