all repos — mgba @ 3bb8075fe7be1c6b2759113544ba32b9bd5f3b19

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}
47
48ssize_t VFileWrite32LE(struct VFile* vf, int32_t word) {
49	uint32_t leword;
50	STORE_32LE(word, 0, &leword);
51	return vf->write(vf, &leword, 4);
52}
53
54ssize_t VFileWrite16LE(struct VFile* vf, int16_t hword) {
55	uint16_t lehword;
56	STORE_16LE(hword, 0, &lehword);
57	return vf->write(vf, &lehword, 2);
58}
59
60ssize_t VFileRead32LE(struct VFile* vf, void* word) {
61	uint32_t leword;
62	ssize_t r = vf->read(vf, &leword, 4);
63	if (r == 4) {
64		STORE_32LE(leword, 0, word);
65	}
66	return r;
67}
68
69ssize_t VFileRead16LE(struct VFile* vf, void* hword) {
70	uint16_t lehword;
71	ssize_t r = vf->read(vf, &lehword, 2);
72	if (r == 2) {
73		STORE_16LE(lehword, 0, hword);
74	}
75	return r;
76}