diff --git a/include/SDL3/SDL_filesystem.h b/include/SDL3/SDL_filesystem.h index dc083fc903..3520e0aeab 100644 --- a/include/SDL3/SDL_filesystem.h +++ b/include/SDL3/SDL_filesystem.h @@ -116,15 +116,14 @@ extern SDL_DECLSPEC const char *SDLCALL SDL_GetBasePath(void); * your applications that use this function. * - Always use a unique app string for each one, and make sure it never * changes for an app once you've decided on it. - * - Unicode characters are legal, as long as it's UTF-8 encoded, but... + * - Unicode characters are legal, as long as they are UTF-8 encoded, but... * - ...only use letters, numbers, and spaces. Avoid punctuation like "Game * Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient. * * The returned path is guaranteed to end with a path separator ('\\' on * Windows, '/' on most other platforms). * - * The pointer returned is owned by the caller. Please call SDL_free() on the - * pointer when done with it. + * The returned string follows the SDL_GetStringRule. * * \param org the name of your organization. * \param app the name of your application. @@ -136,7 +135,7 @@ extern SDL_DECLSPEC const char *SDLCALL SDL_GetBasePath(void); * * \sa SDL_GetBasePath */ -extern SDL_DECLSPEC char *SDLCALL SDL_GetPrefPath(const char *org, const char *app); +extern SDL_DECLSPEC const char *SDLCALL SDL_GetPrefPath(const char *org, const char *app); /** * The type of the OS-provided default folder for a specific purpose. diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 12c32e555c..345bb35d30 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -413,7 +413,7 @@ SDL_DYNAPI_PROC(const char*,SDL_GetPixelFormatName,(SDL_PixelFormat a),(a),retur SDL_DYNAPI_PROC(const char*,SDL_GetPlatform,(void),(),return) SDL_DYNAPI_PROC(void*,SDL_GetPointerProperty,(SDL_PropertiesID a, const char *b, void *c),(a,b,c),return) SDL_DYNAPI_PROC(SDL_PowerState,SDL_GetPowerInfo,(int *a, int *b),(a,b),return) -SDL_DYNAPI_PROC(char*,SDL_GetPrefPath,(const char *a, const char *b),(a,b),return) +SDL_DYNAPI_PROC(const char*,SDL_GetPrefPath,(const char *a, const char *b),(a,b),return) SDL_DYNAPI_PROC(SDL_Locale*,SDL_GetPreferredLocales,(void),(),return) SDL_DYNAPI_PROC(SDL_DisplayID,SDL_GetPrimaryDisplay,(void),(),return) SDL_DYNAPI_PROC(const char*,SDL_GetPrimarySelectionText,(void),(),return) diff --git a/src/filesystem/SDL_filesystem.c b/src/filesystem/SDL_filesystem.c index c714f084ca..324d31c0dd 100644 --- a/src/filesystem/SDL_filesystem.c +++ b/src/filesystem/SDL_filesystem.c @@ -429,6 +429,15 @@ const char *SDL_GetUserFolder(SDL_Folder folder) } +const char *SDL_GetPrefPath(const char *org, const char *app) +{ + char *path = SDL_SYS_GetPrefPath(org, app); + if (path) { + SDL_FreeLater(path); + } + return path; +} + void SDL_InitFilesystem(void) { diff --git a/src/filesystem/SDL_sysfilesystem.h b/src/filesystem/SDL_sysfilesystem.h index ca190daa2d..bd010cac88 100644 --- a/src/filesystem/SDL_sysfilesystem.h +++ b/src/filesystem/SDL_sysfilesystem.h @@ -24,6 +24,7 @@ // return a string that we can SDL_free(). It will be cached at the higher level. extern char *SDL_SYS_GetBasePath(void); +extern char *SDL_SYS_GetPrefPath(const char *org, const char *app); extern char *SDL_SYS_GetUserFolder(SDL_Folder folder); int SDL_SYS_EnumerateDirectory(const char *path, const char *dirname, SDL_EnumerateDirectoryCallback cb, void *userdata); diff --git a/src/filesystem/android/SDL_sysfilesystem.c b/src/filesystem/android/SDL_sysfilesystem.c index 95c58efc9a..d1189379b2 100644 --- a/src/filesystem/android/SDL_sysfilesystem.c +++ b/src/filesystem/android/SDL_sysfilesystem.c @@ -34,7 +34,7 @@ char *SDL_SYS_GetBasePath(void) return NULL; } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { const char *path = SDL_GetAndroidInternalStoragePath(); if (path) { diff --git a/src/filesystem/cocoa/SDL_sysfilesystem.m b/src/filesystem/cocoa/SDL_sysfilesystem.m index 500482c43f..56542f6627 100644 --- a/src/filesystem/cocoa/SDL_sysfilesystem.m +++ b/src/filesystem/cocoa/SDL_sysfilesystem.m @@ -61,7 +61,7 @@ char *SDL_SYS_GetBasePath(void) } } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { @autoreleasepool { char *retval = NULL; diff --git a/src/filesystem/dummy/SDL_sysfilesystem.c b/src/filesystem/dummy/SDL_sysfilesystem.c index 067bf15749..b870efd190 100644 --- a/src/filesystem/dummy/SDL_sysfilesystem.c +++ b/src/filesystem/dummy/SDL_sysfilesystem.c @@ -31,7 +31,7 @@ char *SDL_SYS_GetBasePath(void) return NULL; } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { SDL_Unsupported(); return NULL; diff --git a/src/filesystem/emscripten/SDL_sysfilesystem.c b/src/filesystem/emscripten/SDL_sysfilesystem.c index c00107f808..37e11c10d2 100644 --- a/src/filesystem/emscripten/SDL_sysfilesystem.c +++ b/src/filesystem/emscripten/SDL_sysfilesystem.c @@ -34,7 +34,7 @@ char *SDL_SYS_GetBasePath(void) return SDL_strdup("/"); } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { const char *append = "/libsdl/"; char *retval; diff --git a/src/filesystem/gdk/SDL_sysfilesystem.cpp b/src/filesystem/gdk/SDL_sysfilesystem.cpp index c1d9fa98f0..961901c72d 100644 --- a/src/filesystem/gdk/SDL_sysfilesystem.cpp +++ b/src/filesystem/gdk/SDL_sysfilesystem.cpp @@ -79,8 +79,7 @@ SDL_SYS_GetBasePath(void) return path; } -char * -SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { XUserHandle user = NULL; XAsyncBlock block = { 0 }; diff --git a/src/filesystem/haiku/SDL_sysfilesystem.cc b/src/filesystem/haiku/SDL_sysfilesystem.cc index af5de0c41c..3324627b36 100644 --- a/src/filesystem/haiku/SDL_sysfilesystem.cc +++ b/src/filesystem/haiku/SDL_sysfilesystem.cc @@ -65,7 +65,7 @@ char *SDL_SYS_GetBasePath(void) } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { // !!! FIXME: is there a better way to do this? const char *home = SDL_getenv("HOME"); diff --git a/src/filesystem/n3ds/SDL_sysfilesystem.c b/src/filesystem/n3ds/SDL_sysfilesystem.c index de9040fa05..e6ba5454cd 100644 --- a/src/filesystem/n3ds/SDL_sysfilesystem.c +++ b/src/filesystem/n3ds/SDL_sysfilesystem.c @@ -38,7 +38,7 @@ char *SDL_SYS_GetBasePath(void) return base_path; } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { char *pref_path = NULL; if (!app) { diff --git a/src/filesystem/ps2/SDL_sysfilesystem.c b/src/filesystem/ps2/SDL_sysfilesystem.c index f50056e309..1f9f3eb435 100644 --- a/src/filesystem/ps2/SDL_sysfilesystem.c +++ b/src/filesystem/ps2/SDL_sysfilesystem.c @@ -73,7 +73,7 @@ static void recursive_mkdir(const char *dir) mkdir(tmp, S_IRWXU); } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { char *retval = NULL; size_t len; diff --git a/src/filesystem/psp/SDL_sysfilesystem.c b/src/filesystem/psp/SDL_sysfilesystem.c index f903abd7f1..9019abbd59 100644 --- a/src/filesystem/psp/SDL_sysfilesystem.c +++ b/src/filesystem/psp/SDL_sysfilesystem.c @@ -44,7 +44,7 @@ char *SDL_SYS_GetBasePath(void) return retval; } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { char *retval = NULL; size_t len; diff --git a/src/filesystem/riscos/SDL_sysfilesystem.c b/src/filesystem/riscos/SDL_sysfilesystem.c index ebb603272b..6c13ea55c7 100644 --- a/src/filesystem/riscos/SDL_sysfilesystem.c +++ b/src/filesystem/riscos/SDL_sysfilesystem.c @@ -150,7 +150,7 @@ char *SDL_SYS_GetBasePath(void) return retval; } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { char *canon, *dir, *retval; size_t len; diff --git a/src/filesystem/unix/SDL_sysfilesystem.c b/src/filesystem/unix/SDL_sysfilesystem.c index 12b40641cb..5550b3201c 100644 --- a/src/filesystem/unix/SDL_sysfilesystem.c +++ b/src/filesystem/unix/SDL_sysfilesystem.c @@ -253,7 +253,7 @@ char *SDL_SYS_GetBasePath(void) return retval; } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { /* * We use XDG's base directory spec, even if you're not on Linux. diff --git a/src/filesystem/vita/SDL_sysfilesystem.c b/src/filesystem/vita/SDL_sysfilesystem.c index 4b56955d92..1bcde3a000 100644 --- a/src/filesystem/vita/SDL_sysfilesystem.c +++ b/src/filesystem/vita/SDL_sysfilesystem.c @@ -39,7 +39,7 @@ char *SDL_SYS_GetBasePath(void) return SDL_strdup("app0:/"); } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { const char *envr = "ux0:/data/"; char *retval = NULL; diff --git a/src/filesystem/windows/SDL_sysfilesystem.c b/src/filesystem/windows/SDL_sysfilesystem.c index 5608f0ca6e..5d18bf56c2 100644 --- a/src/filesystem/windows/SDL_sysfilesystem.c +++ b/src/filesystem/windows/SDL_sysfilesystem.c @@ -90,7 +90,7 @@ char *SDL_SYS_GetBasePath(void) return retval; } -char *SDL_GetPrefPath(const char *org, const char *app) +char *SDL_SYS_GetPrefPath(const char *org, const char *app) { /* * Vista and later has a new API for this, but SHGetFolderPath works there, diff --git a/src/filesystem/winrt/SDL_sysfilesystem.cpp b/src/filesystem/winrt/SDL_sysfilesystem.cpp index 0046899b5c..e63b55bc6d 100644 --- a/src/filesystem/winrt/SDL_sysfilesystem.cpp +++ b/src/filesystem/winrt/SDL_sysfilesystem.cpp @@ -137,7 +137,7 @@ extern "C" char *SDL_SYS_GetBasePath(void) return destPath; } -extern "C" char *SDL_GetPrefPath(const char *org, const char *app) +extern "C" char *SDL_SYS_GetPrefPath(const char *org, const char *app) { /* WinRT note: The 'SHGetFolderPath' API that is used in Windows 7 and * earlier is not available on WinRT or Windows Phone. WinRT provides diff --git a/src/storage/generic/SDL_genericstorage.c b/src/storage/generic/SDL_genericstorage.c index c6bfb74fe7..9f174238e1 100644 --- a/src/storage/generic/SDL_genericstorage.c +++ b/src/storage/generic/SDL_genericstorage.c @@ -225,15 +225,18 @@ static const SDL_StorageInterface GENERIC_user_iface = { static SDL_Storage *GENERIC_User_Create(const char *org, const char *app, SDL_PropertiesID props) { SDL_Storage *result; - - char *prefpath = SDL_GetPrefPath(org, app); + char *prefpath = NULL; + const char *sdlprefpath = SDL_GetPrefPath(org, app); + if (sdlprefpath) { + prefpath = SDL_strdup(sdlprefpath); + } if (prefpath == NULL) { return NULL; } result = SDL_OpenStorage(&GENERIC_user_iface, prefpath); if (result == NULL) { - SDL_free(prefpath); + SDL_free(prefpath); // otherwise CloseStorage will free it. } return result; } diff --git a/test/testfilesystem.c b/test/testfilesystem.c index 54214d71da..39c5177ff2 100644 --- a/test/testfilesystem.c +++ b/test/testfilesystem.c @@ -61,7 +61,7 @@ static int SDLCALL enum_callback(void *userdata, const char *origdir, const char int main(int argc, char *argv[]) { SDLTest_CommonState *state; - char *pref_path; + const char *pref_path; const char *base_path; /* Initialize test framework */ @@ -97,7 +97,6 @@ int main(int argc, char *argv[]) SDL_GetError()); } else { SDL_Log("pref path: '%s'\n", pref_path); - SDL_free(pref_path); } pref_path = SDL_GetPrefPath(NULL, "test_filesystem"); @@ -106,7 +105,6 @@ int main(int argc, char *argv[]) SDL_GetError()); } else { SDL_Log("pref path: '%s'\n", pref_path); - SDL_free(pref_path); } if (base_path) {