mirror of https://github.com/mgba-emu/mgba.git
VFS: VFileOpen can now have a swappable backend
This commit is contained in:
parent
2bb16fd0a8
commit
43f9f2dfd3
|
@ -5,6 +5,36 @@
|
|||
* 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) {
|
||||
|
|
|
@ -53,6 +53,8 @@ struct VDir {
|
|||
};
|
||||
|
||||
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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "util/memory.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct VFileFILE {
|
||||
|
@ -28,6 +29,9 @@ struct VFile* VFileFOpen(const char* path, const char* 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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue