#include "SDL_rwopsromfs.h"
#include "SDL_error.h"
SDL_FORCE_INLINE SDL_bool IsReadMode(const char *mode);
SDL_FORCE_INLINE SDL_bool HasPrefix(const char *file, const char *prefix);
SDL_FORCE_INLINE FILE *TryOpenFile(const char *file, const char *mode);
SDL_FORCE_INLINE FILE *TryOpenInRomfs(const char *file, const char *mode);
FILE *
N3DS_FileOpen(const char *file, const char *mode)
{
if (!IsReadMode(mode)) {
return fopen(file, mode);
}
if (HasPrefix(file, "romfs:/") || HasPrefix(file, "sdmc:/")) {
return fopen(file, mode);
}
return TryOpenFile(file, mode);
}
SDL_FORCE_INLINE SDL_bool
IsReadMode(const char *mode)
{
return SDL_strchr(mode, 'r') != NULL;
}
SDL_FORCE_INLINE SDL_bool
HasPrefix(const char *file, const char *prefix)
{
return SDL_strncmp(prefix, file, SDL_strlen(prefix)) == 0;
}
SDL_FORCE_INLINE FILE *
TryOpenFile(const char *file, const char *mode)
{
FILE *fp = NULL;
fp = TryOpenInRomfs(file, mode);
if (fp == NULL) {
fp = fopen(file, mode);
}
return fp;
}
SDL_FORCE_INLINE FILE *
TryOpenInRomfs(const char *file, const char *mode)
{
FILE *fp = NULL;
char *prefixed_filepath = NULL;
if (SDL_asprintf(&prefixed_filepath, "romfs:/%s", file) < 0) {
SDL_OutOfMemory();
return NULL;
}
fp = fopen(prefixed_filepath, mode);
SDL_free(prefixed_filepath);
return fp;
}