SDL
116 строк · 3.0 Кб
1/*
2Simple DirectMedia Layer
3Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any damages
7arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose,
10including commercial applications, and to alter it and redistribute it
11freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must not
14claim that you wrote the original software. If you use this software
15in a product, an acknowledgment in the product documentation would be
16appreciated but is not required.
172. Altered source versions must be plainly marked as such, and must not be
18misrepresented as being the original software.
193. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"22
23#ifdef SDL_FILESYSTEM_EMSCRIPTEN24
25/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26// System dependent filesystem routines
27
28#include "../SDL_sysfilesystem.h"29
30#include <errno.h>31#include <sys/stat.h>32
33#include <emscripten/emscripten.h>34
35char *SDL_SYS_GetBasePath(void)36{
37return SDL_strdup("/");38}
39
40char *SDL_SYS_GetPrefPath(const char *org, const char *app)41{
42const char *append = "/libsdl/";43char *result;44char *ptr = NULL;45size_t len = 0;46
47if (!app) {48SDL_InvalidParamError("app");49return NULL;50}51if (!org) {52org = "";53}54
55len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;56result = (char *)SDL_malloc(len);57if (!result) {58return NULL;59}60
61if (*org) {62SDL_snprintf(result, len, "%s%s/%s/", append, org, app);63} else {64SDL_snprintf(result, len, "%s%s/", append, app);65}66
67for (ptr = result + 1; *ptr; ptr++) {68if (*ptr == '/') {69*ptr = '\0';70if (mkdir(result, 0700) != 0 && errno != EEXIST) {71goto error;72}73*ptr = '/';74}75}76
77if (mkdir(result, 0700) != 0 && errno != EEXIST) {78error:79SDL_SetError("Couldn't create directory '%s': '%s'", result, strerror(errno));80SDL_free(result);81return NULL;82}83
84return result;85}
86
87char *SDL_SYS_GetUserFolder(SDL_Folder folder)88{
89const char *home = NULL;90
91if (folder != SDL_FOLDER_HOME) {92SDL_SetError("Emscripten only supports the home folder");93return NULL;94}95
96home = SDL_getenv("HOME");97if (!home) {98SDL_SetError("No $HOME environment variable available");99return NULL;100}101
102char *result = SDL_malloc(SDL_strlen(home) + 2);103if (!result) {104return NULL;105}106
107if (SDL_snprintf(result, SDL_strlen(home) + 2, "%s/", home) < 0) {108SDL_SetError("Couldn't snprintf home path for Emscripten: %s", home);109SDL_free(result);110return NULL;111}112
113return result;114}
115
116#endif // SDL_FILESYSTEM_EMSCRIPTEN117