* 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)
{
size_t path_length;
char *last_slash;
const char *slash;
const char *backslash;
char tmp_buf[NAME_MAX_LENGTH];
const char *last_slash = NULL;
if (!path_data || string_is_empty(path_data->content_path))
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;
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)))
return false;

View File

@ -358,11 +358,7 @@ char *find_last_slash(const char *str)
{
const char *slash = strrchr(str, '/');
const char *backslash = strrchr(str, '\\');
if (!slash || (backslash > slash))
return (char*)backslash;
else
return (char*)slash;
return (!slash || (backslash > slash)) ? (char*)backslash : (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 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)
return strlcat(path, PATH_DEFAULT_SLASH(), size);
path_len = strlen(path);
@ -466,25 +464,32 @@ void fill_pathname_basedir(char *out_dir,
bool fill_pathname_parent_dir_name(char *out_dir,
const char *in_dir, size_t size)
{
char *temp = strdup(in_dir);
char *last = find_last_slash(temp);
char *tmp = strdup(in_dir);
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 = find_last_slash(temp);
*last_slash = '\0';
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,
leaving the directory name (or nested directory names) only. */
if (last)
*last = '\0';
if (last_slash)
*last_slash = '\0';
/* Point in_dir to the address of the last slash. */
/* If find_last_slash returns NULL, it means there was no slash in temp,
so use temp as-is. */
if (!(in_dir = find_last_slash(temp)))
in_dir = temp;
/* Point in_dir to the address of the last slash.
* If in_dir is NULL, it means there was no slash in tmp,
* so use tmp as-is. */
slash = strrchr(tmp, '/');
backslash = strrchr(tmp, '\\');
in_dir = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (!in_dir)
in_dir = tmp;
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);
else
strlcpy(out_dir, in_dir, size);
free(temp);
free(tmp);
return true;
}
free(temp);
free(tmp);
return false;
}
@ -595,17 +600,21 @@ size_t fill_str_dated_filename(char *out_filename,
**/
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')
return;
if ((last = find_last_slash(path)))
last[1] = '\0';
slash = strrchr(path, '/');
backslash = strrchr(path, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash)
last_slash[1] = '\0';
else
{
path[0] = '.';
path[1] = PATH_DEFAULT_SLASH_C();
path[2] = '\0';
path[0] = '.';
path[1] = PATH_DEFAULT_SLASH_C();
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]))
{
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.
* 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,
* 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))
|| (ptr = find_last_slash(path)))
|| (ptr = last_slash))
return ptr + 1;
return path;
}
@ -675,10 +694,10 @@ const char *path_basename(const char *path)
const char *path_basename_nocompression(const char *path)
{
/* We cut at the last slash */
const char *last = find_last_slash(path);
if (last)
return last + 1;
return path;
const char *slash = strrchr(path, '/');
const char *backslash = strrchr(path, '\\');
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
return (last_slash) ? (last_slash + 1) : path;
}
/**
@ -990,7 +1009,9 @@ size_t fill_pathname_join_special(char *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)
{
/* 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)
{
char *last = NULL;
const char *slash;
const char *backslash;
char *last_slash = NULL;
if (!path || path[0] == '\0' || path[1] == '\0')
return;
#ifdef HAVE_COMPRESSION
/* We want to find the directory with the archive in basedir. */
if ((last = (char*)path_get_archive_delim(path)))
*last = '\0';
if ((last_slash = (char*)path_get_archive_delim(path)))
*last_slash = '\0';
#endif
if ((last = find_last_slash(path)))
last[1] = '\0';
slash = strrchr(path, '/');
backslash = strrchr(path, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash)
last_slash[1] = '\0';
else
{
path[0] = '.';
path[1] = PATH_DEFAULT_SLASH_C();
path[2] = '\0';
path[0] = '.';
path[1] = PATH_DEFAULT_SLASH_C();
path[2] = '\0';
}
}

View File

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

View File

@ -420,8 +420,7 @@ static void explore_load_icons(explore_state_t *state)
if (string_is_empty(path))
return;
fill_pathname_slash(path, sizeof(path));
pathlen = strlen(path);
pathlen = fill_pathname_slash(path, sizeof(path));
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
{
size_t _len;
const char *slash = strrchr(path, '/');
const char *backslash = strrchr(path, '\\');
/* If path has no extension and contains no path
* delimiters, check if it is a core 'name', matching
* an existing file in the cores directory */
if (find_last_slash(path))
if (((!slash || (backslash > slash)) ? (char*)backslash : (char*)slash))
goto end;
/* First check for built-in cores */

View File

@ -326,7 +326,9 @@ runtime_log_t *runtime_log_init(
* content has the same name... */
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)
{
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))
{
/* 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'))
*last_slash = '\0';

View File

@ -1283,12 +1283,20 @@ static void task_database_handler(retro_task_t *task)
char *dirname = NULL;
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))
{
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;
char *dbname = NULL;
bool strmatch = false;
@ -1296,7 +1304,10 @@ static void task_database_handler(retro_task_t *task)
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;
free(dbpath);
@ -1304,8 +1315,7 @@ static void task_database_handler(retro_task_t *task)
if (strmatch)
{
struct string_list *single_list = string_list_new();
string_list_append(single_list,
data,
string_list_append(single_list, data,
dbstate->list->elems[i].attr);
dir_list_free(dbstate->list);
dbstate->list = single_list;

View File

@ -501,7 +501,9 @@ void rarch_log_file_init(
{
/* Get log directory */
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)
{