all repos — mgba @ 101fa29ce9c34168e223371ae6b4c08eff858ba9

mGBA Game Boy Advance Emulator

src/util/vfs.c (view raw)

  1/* Copyright (c) 2013-2015 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#include "vfs.h"
  7
  8#include "util/string.h"
  9
 10#ifdef PSP2
 11#include "platform/psp2/sce-vfs.h"
 12#endif
 13#ifdef _3DS
 14#include "platform/3ds/3ds-vfs.h"
 15#endif
 16
 17struct VFile* VFileOpen(const char* path, int flags) {
 18#ifdef USE_VFS_FILE
 19	const char* chflags;
 20	switch (flags & O_ACCMODE) {
 21	case O_WRONLY:
 22		if (flags & O_APPEND) {
 23			chflags = "ab";
 24		} else {
 25			chflags = "wb";
 26		}
 27		break;
 28	case O_RDWR:
 29		if (flags & O_APPEND) {
 30			chflags = "a+b";
 31		} else if (flags & O_TRUNC) {
 32			chflags = "w+b";
 33		} else {
 34			chflags = "r+b";
 35		}
 36		break;
 37	case O_RDONLY:
 38		chflags = "rb";
 39		break;
 40	}
 41	return VFileFOpen(path, chflags);
 42#elif defined(PSP2)
 43	int sceFlags = PSP2_O_RDONLY;
 44	switch (flags & O_ACCMODE) {
 45	case O_WRONLY:
 46		sceFlags = PSP2_O_WRONLY;
 47		break;
 48	case O_RDWR:
 49		sceFlags = PSP2_O_RDWR;
 50		break;
 51	case O_RDONLY:
 52		sceFlags = PSP2_O_RDONLY;
 53		break;
 54	}
 55
 56	if (flags & O_APPEND) {
 57		sceFlags |= PSP2_O_APPEND;
 58	}
 59	if (flags & O_TRUNC) {
 60		sceFlags |= PSP2_O_TRUNC;
 61	}
 62	if (flags & O_CREAT) {
 63		sceFlags |= PSP2_O_CREAT;
 64	}
 65	return VFileOpenSce(path, sceFlags, 0666);
 66#elif defined(USE_VFS_3DS)
 67	int ctrFlags = FS_OPEN_READ;
 68	switch (flags & O_ACCMODE) {
 69	case O_WRONLY:
 70		ctrFlags = FS_OPEN_WRITE;
 71		break;
 72	case O_RDWR:
 73		ctrFlags = FS_OPEN_READ | FS_OPEN_WRITE;
 74		break;
 75	case O_RDONLY:
 76		ctrFlags = FS_OPEN_READ;
 77		break;
 78	}
 79
 80	if (flags & O_CREAT) {
 81		ctrFlags |= FS_OPEN_CREATE;
 82	}
 83	struct VFile* vf = VFileOpen3DS(&sdmcArchive, path, ctrFlags);
 84	if (!vf) {
 85		return 0;
 86	}
 87	if (flags & O_TRUNC) {
 88		vf->truncate(vf, 0);
 89	}
 90	if (flags & O_APPEND) {
 91		vf->seek(vf, vf->size(vf), SEEK_SET);
 92	}
 93	return vf;
 94#else
 95	return VFileOpenFD(path, flags);
 96#endif
 97}
 98
 99struct VDir* VDirOpenArchive(const char* path) {
100	struct VDir* dir = 0;
101#if USE_LIBZIP
102	if (!dir) {
103		dir = VDirOpenZip(path, 0);
104	}
105#endif
106#if USE_LZMA
107	if (!dir) {
108		dir = VDirOpen7z(path, 0);
109	}
110#endif
111	return dir;
112}
113
114ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) {
115	size_t bytesRead = 0;
116	while (bytesRead < size - 1) {
117		ssize_t newRead = vf->read(vf, &buffer[bytesRead], 1);
118		if (newRead <= 0) {
119			break;
120		}
121		bytesRead += newRead;
122		if (buffer[bytesRead] == '\n') {
123			break;
124		}
125	}
126	buffer[bytesRead] = '\0';
127	return bytesRead;
128}
129
130ssize_t VFileWrite32LE(struct VFile* vf, int32_t word) {
131	uint32_t leword;
132	STORE_32LE(word, 0, &leword);
133	return vf->write(vf, &leword, 4);
134}
135
136ssize_t VFileWrite16LE(struct VFile* vf, int16_t hword) {
137	uint16_t lehword;
138	STORE_16LE(hword, 0, &lehword);
139	return vf->write(vf, &lehword, 2);
140}
141
142ssize_t VFileRead32LE(struct VFile* vf, void* word) {
143	uint32_t leword;
144	ssize_t r = vf->read(vf, &leword, 4);
145	if (r == 4) {
146		STORE_32LE(leword, 0, word);
147	}
148	return r;
149}
150
151ssize_t VFileRead16LE(struct VFile* vf, void* hword) {
152	uint16_t lehword;
153	ssize_t r = vf->read(vf, &lehword, 2);
154	if (r == 2) {
155		STORE_16LE(lehword, 0, hword);
156	}
157	return r;
158}
159
160struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode) {
161	char path[PATH_MAX];
162	path[PATH_MAX - 1] = '\0';
163	struct VFile* vf;
164	if (!dir) {
165		if (!realPath) {
166			return 0;
167		}
168		char* dotPoint = strrchr(realPath, '.');
169		if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
170			return 0;
171		}
172		if (dotPoint > strrchr(realPath, '/')) {
173			int len = dotPoint - realPath;
174			strncpy(path, realPath, len);
175			path[len] = 0;
176			strncat(path + len, suffix, PATH_MAX - len - 1);
177		} else {
178			snprintf(path, PATH_MAX - 1, "%s%s", realPath, suffix);
179		}
180		vf = VFileOpen(path, mode);
181	} else {
182		snprintf(path, PATH_MAX - 1, "%s%s", prefix, suffix);
183		vf = dir->openFile(dir, path, mode);
184	}
185	return vf;
186}
187
188struct VFile* VDirOptionalOpenIncrementFile(struct VDir* dir, const char* realPath, const char* prefix, const char* infix, const char* suffix, int mode) {
189	char path[PATH_MAX];
190	path[PATH_MAX - 1] = '\0';
191	char realPrefix[PATH_MAX];
192	realPrefix[PATH_MAX - 1] = '\0';
193	if (!dir) {
194		if (!realPath) {
195			return 0;
196		}
197		const char* separatorPoint = strrchr(realPath, '/');
198		const char* dotPoint;
199		size_t len;
200		if (!separatorPoint) {
201			strcpy(path, "./");
202			separatorPoint = realPath;
203			dotPoint = strrchr(realPath, '.');
204		} else {
205			path[0] = '\0';
206			dotPoint = strrchr(separatorPoint, '.');
207
208			if (separatorPoint - realPath + 1 >= PATH_MAX - 1) {
209				return 0;
210			}
211
212			len = separatorPoint - realPath;
213			strncat(path, realPath, len);
214			path[len] = '\0';
215			++separatorPoint;
216		}
217
218		if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
219			return 0;
220		}
221
222		if (dotPoint >= separatorPoint) {
223			len = dotPoint - separatorPoint;
224		} else {
225			len = PATH_MAX - 1;
226		}
227
228		strncpy(realPrefix, separatorPoint, len);
229		realPrefix[len] = '\0';
230
231		prefix = realPrefix;
232		dir = VDirOpen(path);
233	}
234	if (!dir) {
235		// This shouldn't be possible
236		return 0;
237	}
238	dir->rewind(dir);
239	struct VDirEntry* dirent;
240	size_t prefixLen = strlen(prefix);
241	size_t infixLen = strlen(infix);
242	unsigned next = 0;
243	while ((dirent = dir->listNext(dir))) {
244		const char* filename = dirent->name(dirent);
245		char* dotPoint = strrchr(filename, '.');
246		size_t len = strlen(filename);
247		if (dotPoint) {
248			len = (dotPoint - filename);
249		}
250		const char* separator = strnrstr(filename, infix, len);
251		if (!separator) {
252			continue;
253		}
254		len = separator - filename;
255		if (len != prefixLen) {
256			continue;
257		}
258		if (strncmp(filename, prefix, prefixLen) == 0) {
259			int nlen;
260			separator += infixLen;
261			snprintf(path, PATH_MAX - 1, "%%u%s%%n", suffix);
262			unsigned increment;
263			if (sscanf(separator, path, &increment, &nlen) < 1) {
264				continue;
265			}
266			len = strlen(separator);
267			if (nlen < (ssize_t) len) {
268				continue;
269			}
270			if (next <= increment) {
271				next = increment + 1;
272			}
273		}
274	}
275	snprintf(path, PATH_MAX - 1, "%s%s%u%s", prefix, infix, next, suffix);
276	path[PATH_MAX - 1] = '\0';
277	return dir->openFile(dir, path, mode);
278}