SDL

Форк
0
/
SDL_iostreamromfs.c 
88 строк · 2.6 Кб
1
/*
2
  Simple DirectMedia Layer
3
  Copyright (C) 1997-2024 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
25
static bool IsReadMode(const char *mode);
26

27
// Checks if the file starts with the given prefix
28
static bool HasPrefix(const char *file, const char *prefix);
29

30
static FILE *TryOpenFile(const char *file, const char *mode);
31
static 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
*/
38
FILE *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

53
static bool IsReadMode(const char *mode)
54
{
55
    return SDL_strchr(mode, 'r') != NULL;
56
}
57

58
static bool HasPrefix(const char *file, const char *prefix)
59
{
60
    return SDL_strncmp(prefix, file, SDL_strlen(prefix)) == 0;
61
}
62

63
static 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

75
FILE *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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.