Atlas - SDL_iostreamromfs.c

Home / ext / SDL / src / io / n3ds Lines: 1 | Size: 2640 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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} 89
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.