瀏覽代碼

filesystem: SDL_GetPrefPath() now follows the SDL_GetStringRule.

Reference Issue #10229.
Ryan C. Gordon 9 月之前
父節點
當前提交
52bf7ff42d

+ 3 - 4
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.

+ 1 - 1
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)

+ 9 - 0
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)
 {

+ 1 - 0
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);

+ 1 - 1
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) {

+ 1 - 1
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;

+ 1 - 1
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;

+ 1 - 1
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;

+ 1 - 2
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 };

+ 1 - 1
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");

+ 1 - 1
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) {

+ 1 - 1
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;

+ 1 - 1
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;

+ 1 - 1
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;

+ 1 - 1
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.

+ 1 - 1
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;

+ 1 - 1
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,

+ 1 - 1
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

+ 6 - 3
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;
 }

+ 1 - 3
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) {