all repos — mgba @ 24ff4e8a015c797d304b36be15df08868b811c63

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
 8ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) {
 9	size_t bytesRead = 0;
10	while (bytesRead < size - 1) {
11		size_t newRead = vf->read(vf, &buffer[bytesRead], 1);
12		bytesRead += newRead;
13		if (!newRead || buffer[bytesRead] == '\n') {
14			break;
15		}
16	}
17	return buffer[bytesRead] = '\0';
18}
19
20struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode) {
21	char path[PATH_MAX];
22	path[PATH_MAX - 1] = '\0';
23	struct VFile* vf;
24	if (!dir) {
25		if (!realPath) {
26			return 0;
27		}
28		char* dotPoint = strrchr(realPath, '.');
29		if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
30			return 0;
31		}
32		if (dotPoint > strrchr(realPath, '/')) {
33			int len = dotPoint - realPath;
34			strncpy(path, realPath, len);
35			path[len] = 0;
36			strncat(path + len, suffix, PATH_MAX - len - 1);
37		} else {
38			snprintf(path, PATH_MAX - 1, "%s%s", realPath, suffix);
39		}
40		vf = VFileOpen(path, mode);
41	} else {
42		snprintf(path, PATH_MAX - 1, "%s%s", prefix, suffix);
43		vf = dir->openFile(dir, path, mode);
44	}
45	return vf;
46}