summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/io/n3ds/SDL_iostreamromfs.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/io/n3ds/SDL_iostreamromfs.c
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/io/n3ds/SDL_iostreamromfs.c')
-rw-r--r--contrib/SDL-3.2.8/src/io/n3ds/SDL_iostreamromfs.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/io/n3ds/SDL_iostreamromfs.c b/contrib/SDL-3.2.8/src/io/n3ds/SDL_iostreamromfs.c
new file mode 100644
index 0000000..b42f539
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/io/n3ds/SDL_iostreamromfs.c
@@ -0,0 +1,88 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "SDL_iostreamromfs.h"
23
24// Checks if the mode is a kind of reading
25static bool IsReadMode(const char *mode);
26
27// Checks if the file starts with the given prefix
28static bool HasPrefix(const char *file, const char *prefix);
29
30static FILE *TryOpenFile(const char *file, const char *mode);
31static FILE *TryOpenInRomfs(const char *file, const char *mode);
32
33/* Nintendo 3DS applications may embed resources in the executable. The
34 resources are stored in a special read-only partition prefixed with
35 'romfs:/'. As such, when opening a file, we should first try the romfs
36 unless sdmc is specifically mentioned.
37*/
38FILE *N3DS_FileOpen(const char *file, const char *mode)
39{
40 // romfs are read-only
41 if (!IsReadMode(mode)) {
42 return fopen(file, mode);
43 }
44
45 // If the path has an explicit prefix, we skip the guess work
46 if (HasPrefix(file, "romfs:/") || HasPrefix(file, "sdmc:/")) {
47 return fopen(file, mode);
48 }
49
50 return TryOpenFile(file, mode);
51}
52
53static bool IsReadMode(const char *mode)
54{
55 return SDL_strchr(mode, 'r') != NULL;
56}
57
58static bool HasPrefix(const char *file, const char *prefix)
59{
60 return SDL_strncmp(prefix, file, SDL_strlen(prefix)) == 0;
61}
62
63static FILE *TryOpenFile(const char *file, const char *mode)
64{
65 FILE *fp = NULL;
66
67 fp = TryOpenInRomfs(file, mode);
68 if (!fp) {
69 fp = fopen(file, mode);
70 }
71
72 return fp;
73}
74
75FILE *TryOpenInRomfs(const char *file, const char *mode)
76{
77 FILE *fp = NULL;
78 char *prefixed_filepath = NULL;
79
80 if (SDL_asprintf(&prefixed_filepath, "romfs:/%s", file) < 0) {
81 return NULL;
82 }
83
84 fp = fopen(prefixed_filepath, mode);
85
86 SDL_free(prefixed_filepath);
87 return fp;
88}