(playlists) Small cleanups:

- Don't need to NULL terminate strings passed to strlcpy
- Misc. code cleanups
This commit is contained in:
LibretroAdmin 2022-09-03 06:31:25 +02:00
parent 5d36d80318
commit 8b5c5358ad

View File

@ -290,10 +290,10 @@ static playlist_path_id_t *playlist_path_id_init(const char *path)
* extract the path of the parent archive */ * extract the path of the parent archive */
if (archive_delim) if (archive_delim)
{ {
char archive_path[PATH_MAX_LENGTH];
size_t len = (1 + archive_delim - real_path); size_t len = (1 + archive_delim - real_path);
char archive_path[PATH_MAX_LENGTH] = {0}; if (len >= PATH_MAX_LENGTH)
len = PATH_MAX_LENGTH;
len = (len < PATH_MAX_LENGTH) ? len : PATH_MAX_LENGTH;
strlcpy(archive_path, real_path, len * sizeof(char)); strlcpy(archive_path, real_path, len * sizeof(char));
path_id->archive_path = strdup(archive_path); path_id->archive_path = strdup(archive_path);
@ -380,7 +380,7 @@ static bool playlist_path_equal(const char *real_path,
if (delim) if (delim)
{ {
char compressed_path_b[PATH_MAX_LENGTH] = {0}; char compressed_path_b[PATH_MAX_LENGTH];
unsigned len = (unsigned)(1 + delim - full_path); unsigned len = (unsigned)(1 + delim - full_path);
strlcpy(compressed_path_b, full_path, strlcpy(compressed_path_b, full_path,
@ -685,8 +685,7 @@ void playlist_delete_by_path(playlist_t *playlist,
if (!playlist || string_is_empty(search_path)) if (!playlist || string_is_empty(search_path))
return; return;
path_id = playlist_path_id_init(search_path); if (!(path_id = playlist_path_id_init(search_path)))
if (!path_id)
return; return;
while (i < RBUF_LEN(playlist->entries)) while (i < RBUF_LEN(playlist->entries))
@ -718,8 +717,7 @@ void playlist_get_index_by_path(playlist_t *playlist,
if (!playlist || !entry || string_is_empty(search_path)) if (!playlist || !entry || string_is_empty(search_path))
return; return;
path_id = playlist_path_id_init(search_path); if (!(path_id = playlist_path_id_init(search_path)))
if (!path_id)
return; return;
for (i = 0, len = RBUF_LEN(playlist->entries); i < len; i++) for (i = 0, len = RBUF_LEN(playlist->entries); i < len; i++)
@ -744,8 +742,7 @@ bool playlist_entry_exists(playlist_t *playlist,
if (!playlist || string_is_empty(path)) if (!playlist || string_is_empty(path))
return false; return false;
path_id = playlist_path_id_init(path); if (!(path_id = playlist_path_id_init(path)))
if (!path_id)
return false; return false;
for (i = 0, len = RBUF_LEN(playlist->entries); i < len; i++) for (i = 0, len = RBUF_LEN(playlist->entries); i < len; i++)
@ -1152,8 +1149,8 @@ bool playlist_content_path_is_valid(const char *path)
* handling is required... */ * handling is required... */
if (path_contains_compressed_file(path)) if (path_contains_compressed_file(path))
{ {
char archive_path[PATH_MAX_LENGTH];
const char *delim = path_get_archive_delim(path); const char *delim = path_get_archive_delim(path);
char archive_path[PATH_MAX_LENGTH] = {0};
size_t len = 0; size_t len = 0;
struct string_list *archive_list = NULL; struct string_list *archive_list = NULL;
const char *content_file = NULL; const char *content_file = NULL;
@ -1172,9 +1169,7 @@ bool playlist_content_path_is_valid(const char *path)
return false; return false;
/* Check if file exists inside archive */ /* Check if file exists inside archive */
archive_list = file_archive_get_file_list(archive_path, NULL); if (!(archive_list = file_archive_get_file_list(archive_path, NULL)))
if (!archive_list)
return false; return false;
/* > Get playlist entry content file name /* > Get playlist entry content file name
@ -1209,7 +1204,6 @@ bool playlist_content_path_is_valid(const char *path)
} }
/* This is a 'normal' path - just check if /* This is a 'normal' path - just check if
* it's valid */ * it's valid */
else
return path_is_valid(path); return path_is_valid(path);
} }
@ -1490,10 +1484,8 @@ void playlist_write_runtime_file(playlist_t *playlist)
if (!playlist || !playlist->modified) if (!playlist || !playlist->modified)
return; return;
file = intfstream_open_file(playlist->config.path, if (!(file = intfstream_open_file(playlist->config.path,
RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE); RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE)))
if (!file)
{ {
RARCH_ERR("Failed to write to playlist file: %s\n", playlist->config.path); RARCH_ERR("Failed to write to playlist file: %s\n", playlist->config.path);
return; return;
@ -2440,7 +2432,7 @@ static bool JSONObjectMemberHandler(void *context, const char *pValue, size_t le
return true; return true;
} }
static void get_old_format_metadata_value( static void playlist_get_old_format_metadata_value(
char *metadata_line, char *value, size_t len) char *metadata_line, char *value, size_t len)
{ {
char *end = NULL; char *end = NULL;
@ -2464,7 +2456,6 @@ static bool playlist_read_file(playlist_t *playlist)
unsigned i; unsigned i;
int test_char; int test_char;
bool res = true; bool res = true;
#if defined(HAVE_ZLIB) #if defined(HAVE_ZLIB)
/* Always use RZIP interface when reading playlists /* Always use RZIP interface when reading playlists
* > this will automatically handle uncompressed * > this will automatically handle uncompressed
@ -2496,22 +2487,7 @@ static bool playlist_read_file(playlist_t *playlist)
goto end; goto end;
}while (!isgraph(test_char) || test_char > 0x7F); }while (!isgraph(test_char) || test_char > 0x7F);
if (test_char == '{') playlist->old_format = (test_char != '{');
{
/* New playlist format detected */
#if 0
RARCH_LOG("[Playlist]: New playlist format detected.\n");
#endif
playlist->old_format = false;
}
else
{
/* old playlist format detected */
#if 0
RARCH_LOG("[Playlist]: Old playlist format detected.\n");
#endif
playlist->old_format = true;
}
/* Reset file to start */ /* Reset file to start */
intfstream_rewind(file); intfstream_rewind(file);
@ -2522,8 +2498,7 @@ static bool playlist_read_file(playlist_t *playlist)
JSONContext context = {0}; JSONContext context = {0};
context.playlist = playlist; context.playlist = playlist;
parser = rjson_open_stream(file); if (!(parser = rjson_open_stream(file)))
if (!parser)
{ {
RARCH_ERR("Failed to create JSON parser\n"); RARCH_ERR("Failed to create JSON parser\n");
goto end; goto end;
@ -2586,18 +2561,15 @@ static bool playlist_read_file(playlist_t *playlist)
{ {
*line_buf[i] = '\0'; *line_buf[i] = '\0';
if (intfstream_gets(file, line_buf[i], sizeof(line_buf[i]))) if (!intfstream_gets(file, line_buf[i], sizeof(line_buf[i])))
{ break;
/* Ensure line is NUL terminated, regardless of /* Ensure line is NULL terminated, regardless of
* Windows or Unix line endings */ * Windows or Unix line endings */
string_replace_all_chars(line_buf[i], '\r', '\0'); string_replace_all_chars(line_buf[i], '\r', '\0');
string_replace_all_chars(line_buf[i], '\n', '\0'); string_replace_all_chars(line_buf[i], '\n', '\0');
lines_read++; lines_read++;
} }
else
break;
}
/* If a 'full set' of lines were read, then this /* If a 'full set' of lines were read, then this
* is a valid playlist entry */ * is a valid playlist entry */
@ -2656,7 +2628,7 @@ static bool playlist_read_file(playlist_t *playlist)
if (strncmp("default_core_path", if (strncmp("default_core_path",
line_buf[0], line_buf[0],
STRLEN_CONST("default_core_path")) == 0) STRLEN_CONST("default_core_path")) == 0)
get_old_format_metadata_value( playlist_get_old_format_metadata_value(
line_buf[0], default_core_path, sizeof(default_core_path)); line_buf[0], default_core_path, sizeof(default_core_path));
/* Get default_core_name */ /* Get default_core_name */
@ -2666,7 +2638,7 @@ static bool playlist_read_file(playlist_t *playlist)
if (strncmp("default_core_name", if (strncmp("default_core_name",
line_buf[1], line_buf[1],
STRLEN_CONST("default_core_name")) == 0) STRLEN_CONST("default_core_name")) == 0)
get_old_format_metadata_value( playlist_get_old_format_metadata_value(
line_buf[1], default_core_name, sizeof(default_core_name)); line_buf[1], default_core_name, sizeof(default_core_name));
/* > Populate default core path/name, if required /* > Populate default core path/name, if required
@ -2689,7 +2661,7 @@ static bool playlist_read_file(playlist_t *playlist)
unsigned display_mode; unsigned display_mode;
char display_mode_str[4] = {0}; char display_mode_str[4] = {0};
get_old_format_metadata_value( playlist_get_old_format_metadata_value(
line_buf[2], display_mode_str, sizeof(display_mode_str)); line_buf[2], display_mode_str, sizeof(display_mode_str));
display_mode = string_to_unsigned(display_mode_str); display_mode = string_to_unsigned(display_mode_str);
@ -2709,7 +2681,7 @@ static bool playlist_read_file(playlist_t *playlist)
char thumbnail_mode_str[8] = {0}; char thumbnail_mode_str[8] = {0};
struct string_list thumbnail_modes = {0}; struct string_list thumbnail_modes = {0};
get_old_format_metadata_value( playlist_get_old_format_metadata_value(
line_buf[3], thumbnail_mode_str, line_buf[3], thumbnail_mode_str,
sizeof(thumbnail_mode_str)); sizeof(thumbnail_mode_str));
string_list_initialize(&thumbnail_modes); string_list_initialize(&thumbnail_modes);
@ -2718,10 +2690,8 @@ static bool playlist_read_file(playlist_t *playlist)
{ {
if (thumbnail_modes.size == 2) if (thumbnail_modes.size == 2)
{ {
unsigned thumbnail_mode;
/* Right thumbnail mode */ /* Right thumbnail mode */
thumbnail_mode = string_to_unsigned( unsigned thumbnail_mode = string_to_unsigned(
thumbnail_modes.elems[0].data); thumbnail_modes.elems[0].data);
if (thumbnail_mode <= PLAYLIST_THUMBNAIL_MODE_BOXARTS) if (thumbnail_mode <= PLAYLIST_THUMBNAIL_MODE_BOXARTS)
playlist->right_thumbnail_mode = (enum playlist_thumbnail_mode)thumbnail_mode; playlist->right_thumbnail_mode = (enum playlist_thumbnail_mode)thumbnail_mode;
@ -2747,7 +2717,7 @@ static bool playlist_read_file(playlist_t *playlist)
unsigned sort_mode; unsigned sort_mode;
char sort_mode_str[4] = {0}; char sort_mode_str[4] = {0};
get_old_format_metadata_value( playlist_get_old_format_metadata_value(
line_buf[4], sort_mode_str, sizeof(sort_mode_str)); line_buf[4], sort_mode_str, sizeof(sort_mode_str));
sort_mode = string_to_unsigned(sort_mode_str); sort_mode = string_to_unsigned(sort_mode_str);
@ -3043,10 +3013,7 @@ void command_playlist_push_write(
playlist_t *playlist, playlist_t *playlist,
const struct playlist_entry *entry) const struct playlist_entry *entry)
{ {
if (!playlist) if (playlist && playlist_push(playlist, entry))
return;
if (playlist_push(playlist, entry))
playlist_write_file(playlist); playlist_write_file(playlist);
} }
@ -3224,14 +3191,13 @@ enum playlist_label_display_mode playlist_get_label_display_mode(playlist_t *pla
enum playlist_thumbnail_mode playlist_get_thumbnail_mode( enum playlist_thumbnail_mode playlist_get_thumbnail_mode(
playlist_t *playlist, enum playlist_thumbnail_id thumbnail_id) playlist_t *playlist, enum playlist_thumbnail_id thumbnail_id)
{ {
if (!playlist) if (playlist)
return PLAYLIST_THUMBNAIL_MODE_DEFAULT; {
if (thumbnail_id == PLAYLIST_THUMBNAIL_RIGHT) if (thumbnail_id == PLAYLIST_THUMBNAIL_RIGHT)
return playlist->right_thumbnail_mode; return playlist->right_thumbnail_mode;
else if (thumbnail_id == PLAYLIST_THUMBNAIL_LEFT) else if (thumbnail_id == PLAYLIST_THUMBNAIL_LEFT)
return playlist->left_thumbnail_mode; return playlist->left_thumbnail_mode;
}
/* Fallback */ /* Fallback */
return PLAYLIST_THUMBNAIL_MODE_DEFAULT; return PLAYLIST_THUMBNAIL_MODE_DEFAULT;
} }
@ -3336,10 +3302,7 @@ void playlist_set_default_core_name(
void playlist_set_label_display_mode(playlist_t *playlist, void playlist_set_label_display_mode(playlist_t *playlist,
enum playlist_label_display_mode label_display_mode) enum playlist_label_display_mode label_display_mode)
{ {
if (!playlist) if (playlist && playlist->label_display_mode != label_display_mode)
return;
if (playlist->label_display_mode != label_display_mode)
{ {
playlist->label_display_mode = label_display_mode; playlist->label_display_mode = label_display_mode;
playlist->modified = true; playlist->modified = true;
@ -3369,10 +3332,7 @@ void playlist_set_thumbnail_mode(
void playlist_set_sort_mode(playlist_t *playlist, void playlist_set_sort_mode(playlist_t *playlist,
enum playlist_sort_mode sort_mode) enum playlist_sort_mode sort_mode)
{ {
if (!playlist) if (playlist && playlist->sort_mode != sort_mode)
return;
if (playlist->sort_mode != sort_mode)
{ {
playlist->sort_mode = sort_mode; playlist->sort_mode = sort_mode;
playlist->modified = true; playlist->modified = true;
@ -3474,10 +3434,7 @@ void playlist_set_scan_dat_file_path(playlist_t *playlist, const char *dat_file_
void playlist_set_scan_search_recursively(playlist_t *playlist, bool search_recursively) void playlist_set_scan_search_recursively(playlist_t *playlist, bool search_recursively)
{ {
if (!playlist) if (playlist && playlist->scan_record.search_recursively != search_recursively)
return;
if (playlist->scan_record.search_recursively != search_recursively)
{ {
playlist->scan_record.search_recursively = search_recursively; playlist->scan_record.search_recursively = search_recursively;
playlist->modified = true; playlist->modified = true;
@ -3486,10 +3443,7 @@ void playlist_set_scan_search_recursively(playlist_t *playlist, bool search_recu
void playlist_set_scan_search_archives(playlist_t *playlist, bool search_archives) void playlist_set_scan_search_archives(playlist_t *playlist, bool search_archives)
{ {
if (!playlist) if (playlist && playlist->scan_record.search_archives != search_archives)
return;
if (playlist->scan_record.search_archives != search_archives)
{ {
playlist->scan_record.search_archives = search_archives; playlist->scan_record.search_archives = search_archives;
playlist->modified = true; playlist->modified = true;
@ -3498,10 +3452,7 @@ void playlist_set_scan_search_archives(playlist_t *playlist, bool search_archive
void playlist_set_scan_filter_dat_content(playlist_t *playlist, bool filter_dat_content) void playlist_set_scan_filter_dat_content(playlist_t *playlist, bool filter_dat_content)
{ {
if (!playlist) if (playlist && playlist->scan_record.filter_dat_content != filter_dat_content)
return;
if (playlist->scan_record.filter_dat_content != filter_dat_content)
{ {
playlist->scan_record.filter_dat_content = filter_dat_content; playlist->scan_record.filter_dat_content = filter_dat_content;
playlist->modified = true; playlist->modified = true;
@ -3529,16 +3480,14 @@ bool playlist_entry_has_core(const struct playlist_entry *entry)
* Returns NULL if entry does not have a valid * Returns NULL if entry does not have a valid
* core association */ * core association */
core_info_t *playlist_entry_get_core_info(const struct playlist_entry* entry) core_info_t *playlist_entry_get_core_info(const struct playlist_entry* entry)
{
if (playlist_entry_has_core(entry))
{ {
core_info_t *core_info = NULL; core_info_t *core_info = NULL;
if (!playlist_entry_has_core(entry))
return NULL;
/* Search for associated core */ /* Search for associated core */
if (core_info_find(entry->core_path, &core_info)) if (core_info_find(entry->core_path, &core_info))
return core_info; return core_info;
}
return NULL; return NULL;
} }
@ -3564,4 +3513,3 @@ core_info_t *playlist_get_default_core_info(playlist_t* playlist)
return NULL; return NULL;
} }