From 5b1d93805d84250461d5058bdcf8ffbd6b28f010 Mon Sep 17 00:00:00 2001 From: Hielke Morsink Date: Sun, 29 Nov 2015 19:41:06 +0100 Subject: [PATCH] More consistent use between name and paths. Improved path_get_* functions. --- src/config.c | 17 +++++++---------- src/game.c | 2 +- src/util/util.c | 19 ++++++++++++++----- src/windows/install_track.c | 4 +--- src/windows/loadsave.c | 4 ++-- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/config.c b/src/config.c index e21031a26d..c43a5648e6 100644 --- a/src/config.c +++ b/src/config.c @@ -1440,16 +1440,13 @@ static void title_sequence_open(const char *path, const char *customName) gConfigTitleSequences.presets = realloc(gConfigTitleSequences.presets, sizeof(title_sequence) * (size_t)gConfigTitleSequences.num_presets); if (customName == NULL) { - char nameBuffer[MAX_PATH], *name; + char nameBuffer[MAX_PATH]; safe_strncpy(nameBuffer, path, MAX_PATH); - name = nameBuffer + strlen(nameBuffer) - 1; - while (*name == '\\' || *name == '/') { - *name = 0; - name--; - } - while (*(name - 1) != '\\' && *(name - 1) != '/') { - name--; - } + // Get folder name + // First strip off the last folder separator + *strrchr(nameBuffer, platform_get_path_separator()) = '\0'; + // Then find the name of the folder + char *name = strrchr(nameBuffer, platform_get_path_separator()) + 1; safe_strncpy(gConfigTitleSequences.presets[preset].name, name, TITLE_SEQUENCE_NAME_SIZE); gConfigTitleSequences.presets[preset].path[0] = 0; } @@ -1548,7 +1545,7 @@ void title_sequence_save_preset_script(int preset) platform_get_user_directory(path, "title sequences"); - strcat(path, path_get_filename(gConfigTitleSequences.presets[preset].name)); + strcat(path, gConfigTitleSequences.presets[preset].name); strncat(path, &separator, 1); strcat(path, "script.txt"); diff --git a/src/game.c b/src/game.c index a53f67f12b..2f8d571e68 100644 --- a/src/game.c +++ b/src/game.c @@ -1050,7 +1050,7 @@ void save_game() } void save_game_as() { - window_loadsave_open(LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME, gScenarioSavePath); + window_loadsave_open(LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME, path_get_filename(gScenarioSavePath)); } diff --git a/src/util/util.c b/src/util/util.c index f437e486b3..9bc17d235f 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -58,7 +58,13 @@ const char *path_get_filename(const utf8 *path) // Find last slash or backslash in the path char *filename = strrchr(path, platform_get_path_separator()); - assert(filename != NULL); + // Checks if the path is valid (e.g. not just a file name) + if (filename == NULL) + { + log_warning("Invalid path given: %s", path); + // Return the input string to keep things working + return path; + } // Increase pointer by one, to get rid of the slashes filename++; @@ -68,12 +74,15 @@ const char *path_get_filename(const utf8 *path) const char *path_get_extension(const utf8 *path) { - // Try to find the most-right dot in the path - char *extension = strrchr(path, '.'); + // Get the filename from the path + char *filename = path_get_filename(path); - // When NULL was returned, return a pointer to the null-terminator + // Try to find the most-right dot in the filename + char *extension = strrchr(filename, '.'); + + // When no dot was found, return a pointer to the null-terminator if (extension == NULL) - extension = strrchr(path, '\0'); + extension = strrchr(filename, '\0'); return extension; } diff --git a/src/windows/install_track.c b/src/windows/install_track.c index 1b631782ae..d91d13e389 100644 --- a/src/windows/install_track.c +++ b/src/windows/install_track.c @@ -137,9 +137,7 @@ void window_install_track_open(const char* path) strncpy(track_path, path, MAX_PATH); track_path[MAX_PATH - 1] = '\0'; - char* track_name_pointer = track_path; - while (*track_name_pointer++ != '\0'); - while (*--track_name_pointer != '\\'); + char* track_name_pointer = strrchr(track_path, platform_get_path_separator()); track_name_pointer++; strncpy(track_dest_name, track_name_pointer, MAX_PATH); diff --git a/src/windows/loadsave.c b/src/windows/loadsave.c index d9d9b5a3f4..87ae4eb597 100644 --- a/src/windows/loadsave.c +++ b/src/windows/loadsave.c @@ -159,7 +159,7 @@ rct_window *window_loadsave_open(int type, char *defaultName) _defaultName[0] = 0; if (!str_is_null_or_empty(defaultName)) { - safe_strncpy(_defaultName, path_get_filename(defaultName), sizeof(_defaultName)); + safe_strncpy(_defaultName, defaultName, sizeof(_defaultName)); path_remove_extension(_defaultName); } @@ -745,7 +745,7 @@ static void window_loadsave_select(rct_window *w, const char *path) case (LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME) : if (gLoadSaveTitleSequenceSave) { utf8 newName[MAX_PATH]; - char *extension = (char*)path_get_extension(path_get_filename(path)); + char *extension = (char*)path_get_extension(path); safe_strncpy(newName, path_get_filename(path), MAX_PATH); if (_stricmp(extension, ".sv6") != 0 && _stricmp(extension, ".sc6") != 0) strcat(newName, ".sv6");