From 6fffa5c11d006fbdd0154dc3b8c19045dcae916c Mon Sep 17 00:00:00 2001 From: Misa Date: Wed, 9 Mar 2022 11:55:38 -0800 Subject: [PATCH] Make `basePath` and `pathSep` global variables While reviewing #272, I noticed that the PR was passing these two arguments through a helper function, even though they really shouldn't ever change. To obviate the need to pass these through, I'm making them global variables. pathSep is just a string literal from PhysFS, while basePath is a whole complicated calculation from SDL and needs to be freed. It will be freed upon filesystem deinit (as is done with PhysFS and the STDIN buffer). Additionally the logic in FILESYSTEM_init is simplified by no longer needing to keep a retval variable or use gotos to free basePath in there. --- desktop_version/src/FileSystemUtils.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 6d2893b0..04428ab4 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -34,6 +34,8 @@ static int mkdir(char* path, int mode) #define MAX_PATH PATH_MAX #endif +static const char* pathSep = NULL; +static char* basePath = NULL; static char saveDir[MAX_PATH] = {'\0'}; static char levelDir[MAX_PATH] = {'\0'}; @@ -63,9 +65,8 @@ static const PHYSFS_Allocator allocator = { int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) { char output[MAX_PATH]; - int retval; - const char* pathSep = PHYSFS_getDirSeparator(); - char* basePath; + + pathSep = PHYSFS_getDirSeparator(); PHYSFS_setAllocator(&allocator); @@ -170,8 +171,7 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) "\nor get it from the free Make and Play Edition.", NULL ); - retval = 0; - goto end; + return 0; } SDL_snprintf(output, sizeof(output), "%s%s", basePath, "gamecontrollerdb.txt"); @@ -179,11 +179,7 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) { vlog_info("gamecontrollerdb.txt not found!"); } - retval = 1; - -end: - SDL_free(basePath); - return retval; + return 1; } static unsigned char* stdin_buffer = NULL; @@ -200,6 +196,8 @@ void FILESYSTEM_deinit(void) SDL_free(stdin_buffer); stdin_buffer = NULL; } + SDL_free(basePath); + basePath = NULL; } char *FILESYSTEM_getUserSaveDirectory(void)