From 8656b3d6f8752b33aac8cd11e37aaaf8ec5f79e5 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 11 Jul 2020 19:29:25 +0200 Subject: [PATCH] Create specialized optimized path_basedir_size --- disk_index_file.c | 2 +- libretro-common/file/file_path.c | 28 +++++++++++++++++++++++- libretro-common/formats/m3u/m3u_file.c | 2 +- libretro-common/include/file/file_path.h | 13 +++++++++++ menu/cbs/menu_cbs_ok.c | 4 ++-- retroarch.c | 8 +++---- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/disk_index_file.c b/disk_index_file.c index b65e8e75bc..dcd827f079 100644 --- a/disk_index_file.c +++ b/disk_index_file.c @@ -313,7 +313,7 @@ bool disk_index_file_init( { /* Use content directory */ strlcpy(disk_index_file_dir, content_path, sizeof(disk_index_file_dir)); - path_basedir(disk_index_file_dir); + path_basedir_size(disk_index_file_dir, STRLEN_CONST(disk_index_file_dir)); } /* > Create directory, if required */ diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 2f78be5fea..aca588935d 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -403,7 +403,7 @@ void fill_pathname_basedir(char *out_dir, { if (out_dir != in_path) strlcpy(out_dir, in_path, size); - path_basedir(out_dir); + path_basedir_size(out_dir, size); } void fill_pathname_basedir_noext(char *out_dir, @@ -554,6 +554,32 @@ void path_basedir(char *path) snprintf(path, 3, "." PATH_DEFAULT_SLASH()); } +/** + * path_basedir_size: + * @path : path + * @size : size of path + * + * Extracts base directory by mutating path. + * Keeps trailing '/'. + * + * Specialized function that avoids the implicit + * strlen call + **/ +void path_basedir_size(char *path, size_t size) +{ + char *last = NULL; + + if (size < 2) + return; + + last = find_last_slash(path); + + if (last) + last[1] = '\0'; + else + snprintf(path, 3, "." PATH_DEFAULT_SLASH()); +} + /** * path_parent_dir: * @path : path diff --git a/libretro-common/formats/m3u/m3u_file.c b/libretro-common/formats/m3u/m3u_file.c index 142a22695b..5b184fa092 100644 --- a/libretro-common/formats/m3u/m3u_file.c +++ b/libretro-common/formats/m3u/m3u_file.c @@ -497,7 +497,7 @@ bool m3u_file_save( if (find_last_slash(m3u_file->path)) { strlcpy(base_dir, m3u_file->path, sizeof(base_dir)); - path_basedir(base_dir); + path_basedir_size(base_dir, STRLEN_CONST(base_dir)); } /* Open file for writing */ diff --git a/libretro-common/include/file/file_path.h b/libretro-common/include/file/file_path.h index 6f1918d136..1f284d1a2c 100644 --- a/libretro-common/include/file/file_path.h +++ b/libretro-common/include/file/file_path.h @@ -135,6 +135,19 @@ const char *path_basename(const char *path); **/ void path_basedir(char *path); +/** + * path_basedir_size: + * @path : path + * @size : size of path + * + * Extracts base directory by mutating path. + * Keeps trailing '/'. + * + * Specialized function that avoids the implicit + * strlen call + **/ +void path_basedir_size(char *path, size_t size); + /** * path_parent_dir: * @path : path diff --git a/menu/cbs/menu_cbs_ok.c b/menu/cbs/menu_cbs_ok.c index 0a2f1db025..226d682779 100644 --- a/menu/cbs/menu_cbs_ok.c +++ b/menu/cbs/menu_cbs_ok.c @@ -865,7 +865,7 @@ int generic_action_ok_displaylist_push(const char *path, { filebrowser_clear_type(); strlcpy(tmp, path_get(RARCH_PATH_CONTENT), sizeof(tmp)); - path_basedir(tmp); + path_basedir_size(tmp, STRLEN_CONST(tmp)); info.type = type; info.directory_ptr = idx; @@ -881,7 +881,7 @@ int generic_action_ok_displaylist_push(const char *path, strlcpy(tmp, content_get_subsystem_rom(content_get_subsystem_rom_id() - 1), sizeof(tmp)); else strlcpy(tmp, path_get(RARCH_PATH_CONTENT), sizeof(tmp)); - path_basedir(tmp); + path_basedir_size(tmp, STRLEN_CONST(tmp)); if (content_get_subsystem() != type - MENU_SETTINGS_SUBSYSTEM_ADD) content_clear_subsystem(); diff --git a/retroarch.c b/retroarch.c index 2b6bedcbae..93e77d2a79 100644 --- a/retroarch.c +++ b/retroarch.c @@ -7900,7 +7900,7 @@ static bool menu_shader_manager_save_preset_internal( fullname, sizeof(buffer)); strlcpy(basedir, buffer, sizeof(basedir)); - path_basedir(basedir); + path_basedir_size(basedir, STRLEN_CONST(basedir)); if (!path_is_directory(basedir)) { @@ -8810,7 +8810,7 @@ static void discord_init( strlcpy(command, args, sizeof(command)); else { - path_basedir(full_path); + path_basedir_size(full_path, STRLEN_CONST(full_path)); snprintf(command, sizeof(command), "%s%s", full_path, args); } #else @@ -10876,7 +10876,7 @@ static void path_set_redirect(struct rarch_state *p_rarch) { strlcpy(new_savefile_dir, p_rarch->path_main_basename, path_size); - path_basedir(new_savefile_dir); + path_basedir_size(new_savefile_dir, STRLEN_CONST(new_savefile_dir)); } /* Set savestate directory if empty based on content directory */ @@ -10884,7 +10884,7 @@ static void path_set_redirect(struct rarch_state *p_rarch) { strlcpy(new_savestate_dir, p_rarch->path_main_basename, path_size); - path_basedir(new_savestate_dir); + path_basedir_size(new_savestate_dir, STRLEN_CONST(new_savestate_dir)); } if (global)