all repos — mgba @ 1ef3c9e5b086fe62addf5657314becc0fdd92295

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
 99ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) {
100	size_t bytesRead = 0;
101	while (bytesRead < size - 1) {
102		ssize_t newRead = vf->read(vf, &buffer[bytesRead], 1);
103		if (newRead <= 0) {
104			break;
105		}
106		bytesRead += newRead;
107		if (buffer[bytesRead] == '\n') {
108			break;
109		}
110	}
111	buffer[bytesRead] = '\0';
112	return bytesRead;
113}
114
115ssize_t VFileWrite32LE(struct VFile* vf, int32_t word) {
116	uint32_t leword;
117	STORE_32LE(word, 0, &leword);
118	return vf->write(vf, &leword, 4);
119}
120
121ssize_t VFileWrite16LE(struct VFile* vf, int16_t hword) {
122	uint16_t lehword;
123	STORE_16LE(hword, 0, &lehword);
124	return vf->write(vf, &lehword, 2);
125}
126
127ssize_t VFileRead32LE(struct VFile* vf, void* word) {
128	uint32_t leword;
129	ssize_t r = vf->read(vf, &leword, 4);
130	if (r == 4) {
131		STORE_32LE(leword, 0, word);
132	}
133	return r;
134}
135
136ssize_t VFileRead16LE(struct VFile* vf, void* hword) {
137	uint16_t lehword;
138	ssize_t r = vf->read(vf, &lehword, 2);
139	if (r == 2) {
140		STORE_16LE(lehword, 0, hword);
141	}
142	return r;
143}
144
145struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode) {
146	char path[PATH_MAX];
147	path[PATH_MAX - 1] = '\0';
148	struct VFile* vf;
149	if (!dir) {
150		if (!realPath) {
151			return 0;
152		}
153		char* dotPoint = strrchr(realPath, '.');
154		if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
155			return 0;
156		}
157		if (dotPoint > strrchr(realPath, '/')) {
158			int len = dotPoint - realPath;
159			strncpy(path, realPath, len);
160			path[len] = 0;
161			strncat(path + len, suffix, PATH_MAX - len - 1);
162		} else {
163			snprintf(path, PATH_MAX - 1, "%s%s", realPath, suffix);
164		}
165		vf = VFileOpen(path, mode);
166	} else {
167		snprintf(path, PATH_MAX - 1, "%s%s", prefix, suffix);
168		vf = dir->openFile(dir, path, mode);
169	}
170	return vf;
171}
172
173struct VFile* VDirOptionalOpenIncrementFile(struct VDir* dir, const char* realPath, const char* prefix, const char* infix, const char* suffix, int mode) {
174	char path[PATH_MAX];
175	path[PATH_MAX - 1] = '\0';
176	char realPrefix[PATH_MAX];
177	realPrefix[PATH_MAX - 1] = '\0';
178	if (!dir) {
179		if (!realPath) {
180			return 0;
181		}
182		const char* separatorPoint = strrchr(realPath, '/');
183		const char* dotPoint;
184		size_t len;
185		if (!separatorPoint) {
186			strcpy(path, "./");
187			separatorPoint = realPath;
188			dotPoint = strrchr(realPath, '.');
189		} else {
190			path[0] = '\0';
191			dotPoint = strrchr(separatorPoint, '.');
192
193			if (separatorPoint - realPath + 1 >= PATH_MAX - 1) {
194				return 0;
195			}
196
197			len = separatorPoint - realPath;
198			strncat(path, realPath, len);
199			path[len] = '\0';
200			++separatorPoint;
201		}
202
203		if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
204			return 0;
205		}
206
207		if (dotPoint >= separatorPoint) {
208			len = dotPoint - separatorPoint;
209		} else {
210			len = PATH_MAX - 1;
211		}
212
213		strncpy(realPrefix, separatorPoint, len);
214		realPrefix[len] = '\0';
215
216		prefix = realPrefix;
217		dir = VDirOpen(path);
218	}
219	if (!dir) {
220		// This shouldn't be possible
221		return 0;
222	}
223	dir->rewind(dir);
224	struct VDirEntry* dirent;
225	size_t prefixLen = strlen(prefix);
226	size_t infixLen = strlen(infix);
227	unsigned next = 0;
228	while ((dirent = dir->listNext(dir))) {
229		const char* filename = dirent->name(dirent);
230		char* dotPoint = strrchr(filename, '.');
231		size_t len = strlen(filename);
232		if (dotPoint) {
233			len = (dotPoint - filename);
234		}
235		const char* separator = strnrstr(filename, infix, len);
236		if (!separator) {
237			continue;
238		}
239		len = separator - filename;
240		if (len != prefixLen) {
241			continue;
242		}
243		if (strncmp(filename, prefix, prefixLen) == 0) {
244			int nlen;
245			separator += infixLen;
246			snprintf(path, PATH_MAX - 1, "%%u%s%%n", suffix);
247			unsigned increment;
248			if (sscanf(separator, path, &increment, &nlen) < 1) {
249				continue;
250			}
251			len = strlen(separator);
252			if (nlen < (ssize_t) len) {
253				continue;
254			}
255			if (next <= increment) {
256				next = increment + 1;
257			}
258		}
259	}
260	snprintf(path, PATH_MAX - 1, "%s%s%u%s", prefix, infix, next, suffix);
261	path[PATH_MAX - 1] = '\0';
262	return dir->openFile(dir, path, mode);
263}