include/mgba-util/vfs.h (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef VFS_H
7#define VFS_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#ifdef _WIN32
14#include <io.h>
15#include <windows.h>
16#define PATH_SEP "/" // Windows can handle slashes, and backslashes confuse some libraries
17#else
18#define PATH_SEP "/"
19#endif
20
21#ifndef PATH_MAX
22#ifdef MAX_PATH
23#define PATH_MAX MAX_PATH
24#else
25#define PATH_MAX 128
26#endif
27#endif
28
29enum {
30 MAP_READ = 1,
31 MAP_WRITE = 2
32};
33
34enum VFSType {
35 VFS_UNKNOWN = 0,
36 VFS_FILE,
37 VFS_DIRECTORY
38};
39
40struct VFile {
41 bool (*close)(struct VFile* vf);
42 off_t (*seek)(struct VFile* vf, off_t offset, int whence);
43 ssize_t (*read)(struct VFile* vf, void* buffer, size_t size);
44 ssize_t (*readline)(struct VFile* vf, char* buffer, size_t size);
45 ssize_t (*write)(struct VFile* vf, const void* buffer, size_t size);
46 void* (*map)(struct VFile* vf, size_t size, int flags);
47 void (*unmap)(struct VFile* vf, void* memory, size_t size);
48 void (*truncate)(struct VFile* vf, size_t size);
49 ssize_t (*size)(struct VFile* vf);
50 bool (*sync)(struct VFile* vf, const void* buffer, size_t size);
51};
52
53struct VDirEntry {
54 const char* (*name)(struct VDirEntry* vde);
55 enum VFSType (*type)(struct VDirEntry* vde);
56};
57
58struct VDir {
59 bool (*close)(struct VDir* vd);
60 void (*rewind)(struct VDir* vd);
61 struct VDirEntry* (*listNext)(struct VDir* vd);
62 struct VFile* (*openFile)(struct VDir* vd, const char* name, int mode);
63 struct VDir* (*openDir)(struct VDir* vd, const char* name);
64 bool (*deleteFile)(struct VDir* vd, const char* name);
65};
66
67struct VFile* VFileOpen(const char* path, int flags);
68
69struct VFile* VFileOpenFD(const char* path, int flags);
70struct VFile* VFileFromFD(int fd);
71
72struct VFile* VFileFromMemory(void* mem, size_t size);
73struct VFile* VFileFromConstMemory(const void* mem, size_t size);
74struct VFile* VFileMemChunk(const void* mem, size_t size);
75
76struct VDir* VDirOpen(const char* path);
77struct VDir* VDirOpenArchive(const char* path);
78
79#if defined(USE_LIBZIP) || defined(USE_ZLIB)
80struct VDir* VDirOpenZip(const char* path, int flags);
81#endif
82
83#ifdef USE_LZMA
84struct VDir* VDirOpen7z(const char* path, int flags);
85#endif
86
87#if defined(__wii__) || defined(_3DS)
88struct VDir* VDeviceList(void);
89#endif
90
91#ifdef USE_VFS_FILE
92struct VFile* VFileFOpen(const char* path, const char* mode);
93struct VFile* VFileFromFILE(FILE* file);
94#endif
95
96void separatePath(const char* path, char* dirname, char* basename, char* extension);
97
98struct VFile* VDirFindFirst(struct VDir* dir, bool (*filter)(struct VFile*));
99struct VFile* VDirFindNextAvailable(struct VDir*, const char* basename, const char* infix, const char* suffix, int mode);
100
101ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size);
102
103ssize_t VFileWrite32LE(struct VFile* vf, int32_t word);
104ssize_t VFileWrite16LE(struct VFile* vf, int16_t hword);
105ssize_t VFileRead32LE(struct VFile* vf, void* word);
106ssize_t VFileRead16LE(struct VFile* vf, void* hword);
107
108CXX_GUARD_END
109
110#endif