* Inline find_last_slash

* explore_load_icons - fill_pathname_slash is equivalent to usage
of strlen here, no need to call strlen
This commit is contained in:
libretroadmin 2024-09-10 16:02:06 +02:00
parent 046c2375e7
commit 29f011acef
9 changed files with 120 additions and 80 deletions

View File

@ -1025,16 +1025,21 @@ bool gfx_thumbnail_get_content_dir(
gfx_thumbnail_path_data_t *path_data, char *content_dir, size_t len) gfx_thumbnail_path_data_t *path_data, char *content_dir, size_t len)
{ {
size_t path_length; size_t path_length;
char *last_slash;
const char *slash;
const char *backslash;
char tmp_buf[NAME_MAX_LENGTH]; char tmp_buf[NAME_MAX_LENGTH];
const char *last_slash = NULL;
if (!path_data || string_is_empty(path_data->content_path)) if (!path_data || string_is_empty(path_data->content_path))
return false; return false;
if (!(last_slash = find_last_slash(path_data->content_path))) slash = strrchr(path_data->content_path, '/');
backslash = strrchr(path_data->content_path, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (!last_slash)
return false; return false;
path_length = last_slash + 1 - path_data->content_path; path_length = last_slash + 1 - path_data->content_path;
if (!((path_length > 1) && (path_length < PATH_MAX_LENGTH))) if (!((path_length > 1) && (path_length < PATH_MAX_LENGTH)))
return false; return false;

View File

@ -358,11 +358,7 @@ char *find_last_slash(const char *str)
{ {
const char *slash = strrchr(str, '/'); const char *slash = strrchr(str, '/');
const char *backslash = strrchr(str, '\\'); const char *backslash = strrchr(str, '\\');
return (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (!slash || (backslash > slash))
return (char*)backslash;
else
return (char*)slash;
} }
/** /**
@ -376,7 +372,9 @@ char *find_last_slash(const char *str)
size_t fill_pathname_slash(char *path, size_t size) size_t fill_pathname_slash(char *path, size_t size)
{ {
size_t path_len; size_t path_len;
const char *last_slash = find_last_slash(path); const char *slash = strrchr(path, '/');
const char *backslash = strrchr(path, '\\');
const char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (!last_slash) if (!last_slash)
return strlcat(path, PATH_DEFAULT_SLASH(), size); return strlcat(path, PATH_DEFAULT_SLASH(), size);
path_len = strlen(path); path_len = strlen(path);
@ -466,25 +464,32 @@ void fill_pathname_basedir(char *out_dir,
bool fill_pathname_parent_dir_name(char *out_dir, bool fill_pathname_parent_dir_name(char *out_dir,
const char *in_dir, size_t size) const char *in_dir, size_t size)
{ {
char *temp = strdup(in_dir); char *tmp = strdup(in_dir);
char *last = find_last_slash(temp); const char *slash = strrchr(tmp, '/');
const char *backslash = strrchr(tmp, '\\');
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last && last[1] == 0) if (last_slash && last_slash[1] == 0)
{ {
*last = '\0'; *last_slash = '\0';
last = find_last_slash(temp); slash = strrchr(tmp, '/');
backslash = strrchr(tmp, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
} }
/* Cut the last part of the string (the filename) after the slash, /* Cut the last part of the string (the filename) after the slash,
leaving the directory name (or nested directory names) only. */ leaving the directory name (or nested directory names) only. */
if (last) if (last_slash)
*last = '\0'; *last_slash = '\0';
/* Point in_dir to the address of the last slash. */ /* Point in_dir to the address of the last slash.
/* If find_last_slash returns NULL, it means there was no slash in temp, * If in_dir is NULL, it means there was no slash in tmp,
so use temp as-is. */ * so use tmp as-is. */
if (!(in_dir = find_last_slash(temp))) slash = strrchr(tmp, '/');
in_dir = temp; backslash = strrchr(tmp, '\\');
in_dir = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (!in_dir)
in_dir = tmp;
if (in_dir && in_dir[1]) if (in_dir && in_dir[1])
{ {
@ -493,11 +498,11 @@ bool fill_pathname_parent_dir_name(char *out_dir,
strlcpy(out_dir, in_dir + 1, size); strlcpy(out_dir, in_dir + 1, size);
else else
strlcpy(out_dir, in_dir, size); strlcpy(out_dir, in_dir, size);
free(temp); free(tmp);
return true; return true;
} }
free(temp); free(tmp);
return false; return false;
} }
@ -595,17 +600,21 @@ size_t fill_str_dated_filename(char *out_filename,
**/ **/
void path_basedir(char *path) void path_basedir(char *path)
{ {
char *last = NULL; const char *slash;
const char *backslash;
char *last_slash = NULL;
if (!path || path[0] == '\0' || path[1] == '\0') if (!path || path[0] == '\0' || path[1] == '\0')
return; return;
slash = strrchr(path, '/');
if ((last = find_last_slash(path))) backslash = strrchr(path, '\\');
last[1] = '\0'; last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash)
last_slash[1] = '\0';
else else
{ {
path[0] = '.'; path[0] = '.';
path[1] = PATH_DEFAULT_SLASH_C(); path[1] = PATH_DEFAULT_SLASH_C();
path[2] = '\0'; path[2] = '\0';
} }
} }
@ -625,11 +634,18 @@ void path_parent_dir(char *path, size_t len)
if (len && PATH_CHAR_IS_SLASH(path[len - 1])) if (len && PATH_CHAR_IS_SLASH(path[len - 1]))
{ {
bool path_was_absolute = path_is_absolute(path); char *last_slash;
const char *slash;
const char *backslash;
bool was_absolute = path_is_absolute(path);
path[len - 1] = '\0'; path[len - 1] = '\0';
if (path_was_absolute && !find_last_slash(path)) slash = strrchr(path, '/');
backslash = strrchr(path, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (was_absolute && !last_slash)
{ {
/* We removed the only slash from what used to be an absolute path. /* We removed the only slash from what used to be an absolute path.
* On Linux, this goes from "/" to an empty string and everything works fine, * On Linux, this goes from "/" to an empty string and everything works fine,
@ -655,9 +671,12 @@ const char *path_basename(const char *path)
{ {
/* We cut either at the first compression-related hash, /* We cut either at the first compression-related hash,
* or we cut at the last slash */ * or we cut at the last slash */
const char *ptr = NULL; const char *ptr = NULL;
const char *slash = strrchr(path, '/');
const char *backslash = strrchr(path, '\\');
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if ( (ptr = path_get_archive_delim(path)) if ( (ptr = path_get_archive_delim(path))
|| (ptr = find_last_slash(path))) || (ptr = last_slash))
return ptr + 1; return ptr + 1;
return path; return path;
} }
@ -675,10 +694,10 @@ const char *path_basename(const char *path)
const char *path_basename_nocompression(const char *path) const char *path_basename_nocompression(const char *path)
{ {
/* We cut at the last slash */ /* We cut at the last slash */
const char *last = find_last_slash(path); const char *slash = strrchr(path, '/');
if (last) const char *backslash = strrchr(path, '\\');
return last + 1; char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
return path; return (last_slash) ? (last_slash + 1) : path;
} }
/** /**
@ -990,7 +1009,9 @@ size_t fill_pathname_join_special(char *out_path,
if (*out_path) if (*out_path)
{ {
const char *last_slash = find_last_slash(out_path); const char *slash = strrchr(out_path, '/');
const char *backslash = strrchr(out_path, '\\');
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash) if (last_slash)
{ {
/* Try to preserve slash type. */ /* Try to preserve slash type. */
@ -1284,23 +1305,26 @@ size_t fill_pathname_abbreviated_or_relative(char *out_path,
**/ **/
void path_basedir_wrapper(char *path) void path_basedir_wrapper(char *path)
{ {
char *last = NULL; const char *slash;
const char *backslash;
char *last_slash = NULL;
if (!path || path[0] == '\0' || path[1] == '\0') if (!path || path[0] == '\0' || path[1] == '\0')
return; return;
#ifdef HAVE_COMPRESSION #ifdef HAVE_COMPRESSION
/* We want to find the directory with the archive in basedir. */ /* We want to find the directory with the archive in basedir. */
if ((last = (char*)path_get_archive_delim(path))) if ((last_slash = (char*)path_get_archive_delim(path)))
*last = '\0'; *last_slash = '\0';
#endif #endif
slash = strrchr(path, '/');
if ((last = find_last_slash(path))) backslash = strrchr(path, '\\');
last[1] = '\0'; last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash)
last_slash[1] = '\0';
else else
{ {
path[0] = '.'; path[0] = '.';
path[1] = PATH_DEFAULT_SLASH_C(); path[1] = PATH_DEFAULT_SLASH_C();
path[2] = '\0'; path[2] = '\0';
} }
} }

View File

@ -471,11 +471,12 @@ void m3u_file_clear(m3u_file_t *m3u_file)
bool m3u_file_save( bool m3u_file_save(
m3u_file_t *m3u_file, enum m3u_file_label_type label_type) m3u_file_t *m3u_file, enum m3u_file_label_type label_type)
{ {
RFILE *file = NULL;
size_t i; size_t i;
const char *slash;
const char *backslash;
char base_dir[DIR_MAX_LENGTH]; char base_dir[DIR_MAX_LENGTH];
char *last_slash = NULL;
base_dir[0] = '\0'; RFILE *file = NULL;
if (!m3u_file || !m3u_file->entries) if (!m3u_file || !m3u_file->entries)
return false; return false;
@ -484,20 +485,23 @@ bool m3u_file_save(
if (string_is_empty(m3u_file->path)) if (string_is_empty(m3u_file->path))
return false; return false;
slash = strrchr(m3u_file->path, '/');
backslash = strrchr(m3u_file->path, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
/* Get M3U file base directory */ /* Get M3U file base directory */
if (find_last_slash(m3u_file->path)) if (last_slash)
{ {
strlcpy(base_dir, m3u_file->path, sizeof(base_dir)); strlcpy(base_dir, m3u_file->path, sizeof(base_dir));
path_basedir(base_dir); path_basedir(base_dir);
} }
else
base_dir[0] = '\0';
/* Open file for writing */ /* Open file for writing */
file = filestream_open( if (!(file = filestream_open(m3u_file->path,
m3u_file->path,
RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_WRITE,
RETRO_VFS_FILE_ACCESS_HINT_NONE); RETRO_VFS_FILE_ACCESS_HINT_NONE)))
if (!file)
return false; return false;
/* Loop over entries */ /* Loop over entries */
@ -605,29 +609,19 @@ void m3u_file_qsort(m3u_file_t *m3u_file)
bool m3u_file_is_m3u(const char *path) bool m3u_file_is_m3u(const char *path)
{ {
const char *file_ext = NULL; const char *file_ext = NULL;
int32_t file_size;
if (string_is_empty(path)) if (string_is_empty(path))
return false; return false;
/* Check file extension */ /* Check file extension */
file_ext = path_get_extension(path); file_ext = path_get_extension(path);
if (string_is_empty(file_ext)) if (string_is_empty(file_ext))
return false; return false;
if (!string_is_equal_noncase(file_ext, M3U_FILE_EXT)) if (!string_is_equal_noncase(file_ext, M3U_FILE_EXT))
return false; return false;
/* Ensure file exists */ /* Ensure file exists */
if (!path_is_valid(path)) if (!path_is_valid(path))
return false; return false;
/* Ensure we have non-zero file size */ /* Ensure we have non-zero file size */
file_size = path_get_size(path); if (path_get_size(path) <= 0)
if (file_size <= 0)
return false; return false;
return true; return true;
} }

View File

@ -420,8 +420,7 @@ static void explore_load_icons(explore_state_t *state)
if (string_is_empty(path)) if (string_is_empty(path))
return; return;
fill_pathname_slash(path, sizeof(path)); pathlen = fill_pathname_slash(path, sizeof(path));
pathlen = strlen(path);
for (i = 0; i != system_count; i++) for (i = 0; i != system_count; i++)
{ {

View File

@ -6534,10 +6534,12 @@ static void retroarch_parse_input_libretro_path(const char *path, size_t path_le
else else
{ {
size_t _len; size_t _len;
const char *slash = strrchr(path, '/');
const char *backslash = strrchr(path, '\\');
/* If path has no extension and contains no path /* If path has no extension and contains no path
* delimiters, check if it is a core 'name', matching * delimiters, check if it is a core 'name', matching
* an existing file in the cores directory */ * an existing file in the cores directory */
if (find_last_slash(path)) if (((!slash || (backslash > slash)) ? (char*)backslash : (char*)slash))
goto end; goto end;
/* First check for built-in cores */ /* First check for built-in cores */

View File

@ -326,7 +326,9 @@ runtime_log_t *runtime_log_init(
* content has the same name... */ * content has the same name... */
else if (string_is_equal(core_name, "TyrQuake")) else if (string_is_equal(core_name, "TyrQuake"))
{ {
const char *last_slash = find_last_slash(content_path); const char *slash = strrchr(content_path, '/');
const char *backslash = strrchr(content_path, '\\');
const char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash) if (last_slash)
{ {
size_t path_length = last_slash + 1 - content_path; size_t path_length = last_slash + 1 - content_path;

View File

@ -602,7 +602,9 @@ static bool content_file_list_set_info(
if (!string_is_empty(dir)) if (!string_is_empty(dir))
{ {
/* Remove any trailing slash */ /* Remove any trailing slash */
char *last_slash = find_last_slash(dir); const char *slash = strrchr(dir, '/');
const char *backslash = strrchr(dir, '\\');
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash && (last_slash[1] == '\0')) if (last_slash && (last_slash[1] == '\0'))
*last_slash = '\0'; *last_slash = '\0';

View File

@ -1283,12 +1283,20 @@ static void task_database_handler(retro_task_t *task)
char *dirname = NULL; char *dirname = NULL;
if (!string_is_empty(db->fullpath)) if (!string_is_empty(db->fullpath))
dirname = find_last_slash(db->fullpath) + 1; {
const char *slash = strrchr(db->fullpath, '/');
const char *backslash = strrchr(db->fullpath, '\\');
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
dirname = last_slash + 1;
}
if (!string_is_empty(dirname)) if (!string_is_empty(dirname))
{ {
for (i = 0; i < dbstate->list->size; i++) for (i = 0; i < dbstate->list->size; i++)
{ {
char *last_slash;
const char *slash;
const char *backslash;
const char *data = dbstate->list->elems[i].data; const char *data = dbstate->list->elems[i].data;
char *dbname = NULL; char *dbname = NULL;
bool strmatch = false; bool strmatch = false;
@ -1296,7 +1304,10 @@ static void task_database_handler(retro_task_t *task)
path_remove_extension(dbpath); path_remove_extension(dbpath);
dbname = find_last_slash(dbpath) + 1; slash = strrchr(dbpath, '/');
backslash = strrchr(dbpath, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
dbname = last_slash + 1;
strmatch = strcasecmp(dbname, dirname) == 0; strmatch = strcasecmp(dbname, dirname) == 0;
free(dbpath); free(dbpath);
@ -1304,8 +1315,7 @@ static void task_database_handler(retro_task_t *task)
if (strmatch) if (strmatch)
{ {
struct string_list *single_list = string_list_new(); struct string_list *single_list = string_list_new();
string_list_append(single_list, string_list_append(single_list, data,
data,
dbstate->list->elems[i].attr); dbstate->list->elems[i].attr);
dir_list_free(dbstate->list); dir_list_free(dbstate->list);
dbstate->list = single_list; dbstate->list = single_list;

View File

@ -501,7 +501,9 @@ void rarch_log_file_init(
{ {
/* Get log directory */ /* Get log directory */
const char *override_path = g_verbosity->override_path; const char *override_path = g_verbosity->override_path;
const char *last_slash = find_last_slash(override_path); const char *slash = strrchr(override_path, '/');
const char *backslash = strrchr(override_path, '\\');
const char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash) if (last_slash)
{ {