From c2b81881ae574aabc59a6b50da7e29d6de12cb78 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Sat, 2 Sep 2023 16:42:49 +0200 Subject: [PATCH] Cleanups --- core_backup.c | 156 +++++++++++++--------------------------- core_backup.h | 15 ---- menu/menu_displaylist.c | 28 +++++++- 3 files changed, 77 insertions(+), 122 deletions(-) diff --git a/core_backup.c b/core_backup.c index 0d377eece9..ea67079e57 100644 --- a/core_backup.c +++ b/core_backup.c @@ -244,11 +244,11 @@ error: * Returns true if successful */ bool core_backup_get_backup_crc(char *backup_path, uint32_t *crc) { - struct string_list *metadata_list = NULL; enum core_backup_type backup_type; + struct string_list *metadata_list = NULL; if (string_is_empty(backup_path) || !crc) - goto error; + return false; /* Get backup type */ backup_type = core_backup_get_backup_type(backup_path); @@ -340,69 +340,64 @@ enum core_backup_type core_backup_get_core_path( char *core_path, size_t len) { const char *backup_filename = NULL; - char *core_filename = NULL; - enum core_backup_type backup_type = CORE_BACKUP_TYPE_INVALID; if (string_is_empty(backup_path) || string_is_empty(dir_libretro)) - return backup_type; + return CORE_BACKUP_TYPE_INVALID; backup_filename = path_basename(backup_path); - if (string_is_empty(backup_filename)) - return backup_type; - - /* Check backup type */ - switch (core_backup_get_backup_type(backup_path)) + if (!string_is_empty(backup_filename)) { - case CORE_BACKUP_TYPE_ARCHIVE: - { - char *period = NULL; + /* Check backup type */ + switch (core_backup_get_backup_type(backup_path)) + { + case CORE_BACKUP_TYPE_ARCHIVE: + { + /* This is an archived backup with timestamp/crc + * metadata in the filename */ + char *core_filename = strdup(backup_filename); + /* Find the location of the second period */ + char *period = strchr(core_filename, '.'); + if (!period || (*(++period) == '\0')) + { + free(core_filename); + break; + } - /* This is an archived backup with timestamp/crc - * metadata in the filename */ - core_filename = strdup(backup_filename); + if (!(period = strchr(period, '.'))) + { + free(core_filename); + break; + } - /* Find the location of the second period */ - period = strchr(core_filename, '.'); - if (!period || (*(++period) == '\0')) - break; + /* Trim everything after (and including) the + * second period */ + *period = '\0'; - period = strchr(period, '.'); - if (!period) - break; + if (string_is_empty(core_filename)) + { + free(core_filename); + break; + } - /* Trim everything after (and including) the - * second period */ - *period = '\0'; - - if (string_is_empty(core_filename)) - break; - - /* All good - build core path */ + /* All good - build core path */ + fill_pathname_join_special(core_path, dir_libretro, + core_filename, len); + free(core_filename); + } + return CORE_BACKUP_TYPE_ARCHIVE; + case CORE_BACKUP_TYPE_LIB: + /* This is a plain dynamic library file */ fill_pathname_join_special(core_path, dir_libretro, - core_filename, len); - - backup_type = CORE_BACKUP_TYPE_ARCHIVE; - } - break; - case CORE_BACKUP_TYPE_LIB: - /* This is a plain dynamic library file */ - fill_pathname_join_special(core_path, dir_libretro, - backup_filename, len); - backup_type = CORE_BACKUP_TYPE_LIB; - break; - default: - /* Backup is invalid */ - break; + backup_filename, len); + return CORE_BACKUP_TYPE_LIB; + default: + /* Backup is invalid */ + break; + } } - if (core_filename) - { - free(core_filename); - core_filename = NULL; - } - - return backup_type; + return CORE_BACKUP_TYPE_INVALID; } /*************************/ @@ -426,7 +421,7 @@ static bool core_backup_add_entry(core_backup_list_t *backup_list, || string_is_empty(core_filename) || string_is_empty(backup_path) || (backup_list->size >= backup_list->capacity)) - goto error; + return false; backup_filename = strdup(path_basename(backup_path)); @@ -529,9 +524,7 @@ core_backup_list_t *core_backup_list_init( dir_list_sort(dir_list, true); /* Create core backup list */ - backup_list = (core_backup_list_t*)malloc(sizeof(*backup_list)); - - if (!backup_list) + if (!(backup_list = (core_backup_list_t*)malloc(sizeof(*backup_list)))) goto error; backup_list->entries = NULL; @@ -542,10 +535,8 @@ core_backup_list_t *core_backup_list_init( * (Note: Set this to the full size of the directory * list - this may be larger than we need, but saves * many inefficiencies later) */ - entries = (core_backup_list_entry_t*) - calloc(dir_list->size, sizeof(*entries)); - - if (!entries) + if (!(entries = (core_backup_list_entry_t*) + calloc(dir_list->size, sizeof(*entries)))) goto error; backup_list->entries = entries; @@ -695,48 +686,3 @@ bool core_backup_list_get_crc( return false; } - -/* Fetches a string representation of a backup - * list entry timestamp. - * Returns false in the event of an error */ -bool core_backup_list_get_entry_timestamp_str( - const core_backup_list_entry_t *entry, - enum core_backup_date_separator_type date_separator, - char *timestamp, size_t len) -{ - const char *format_str = "%04u-%02u-%02u %02u:%02u:%02u"; - - if (!entry || (len < 20)) - return false; - - /* Get time format string */ - if (date_separator == CORE_BACKUP_DATE_SEPARATOR_SLASH) - format_str = "%04u/%02u/%02u %02u:%02u:%02u"; - else if (date_separator == CORE_BACKUP_DATE_SEPARATOR_PERIOD) - format_str = "%04u.%02u.%02u %02u:%02u:%02u"; - - snprintf(timestamp, len, - format_str, - entry->date.year, - entry->date.month, - entry->date.day, - entry->date.hour, - entry->date.minute, - entry->date.second); - - return true; -} - -/* Fetches a string representation of a backup - * list entry crc value. - * Returns false in the event of an error */ -bool core_backup_list_get_entry_crc_str( - const core_backup_list_entry_t *entry, - char *crc, size_t len) -{ - if (!entry || (len < 9)) - return false; - - snprintf(crc, len, "%08lx", (unsigned long)entry->crc); - return true; -} diff --git a/core_backup.h b/core_backup.h index 6b4feb8550..ed71ee4e2e 100644 --- a/core_backup.h +++ b/core_backup.h @@ -157,21 +157,6 @@ bool core_backup_list_get_crc( uint32_t crc, enum core_backup_mode backup_mode, const core_backup_list_entry_t **entry); -/* Fetches a string representation of a backup - * list entry timestamp. - * Returns false in the event of an error */ -bool core_backup_list_get_entry_timestamp_str( - const core_backup_list_entry_t *entry, - enum core_backup_date_separator_type date_separator, - char *timestamp, size_t len); - -/* Fetches a string representation of a backup - * list entry crc value. - * Returns false in the event of an error */ -bool core_backup_list_get_entry_crc_str( - const core_backup_list_entry_t *entry, - char *crc, size_t len); - RETRO_END_DECLS #endif diff --git a/menu/menu_displaylist.c b/menu/menu_displaylist.c index 8021f5ed27..e69956b479 100644 --- a/menu/menu_displaylist.c +++ b/menu/menu_displaylist.c @@ -885,6 +885,31 @@ end: return count; } +/* Fetches a string representation of a backup + * list entry timestamp. + * Returns false in the event of an error */ +static size_t core_backup_list_get_entry_timestamp_str( + const core_backup_list_entry_t *entry, + enum core_backup_date_separator_type date_separator, + char *timestamp, size_t len) +{ + const char *format_str = "%04u-%02u-%02u %02u:%02u:%02u"; + /* Get time format string */ + if (date_separator == CORE_BACKUP_DATE_SEPARATOR_SLASH) + format_str = "%04u/%02u/%02u %02u:%02u:%02u"; + else if (date_separator == CORE_BACKUP_DATE_SEPARATOR_PERIOD) + format_str = "%04u.%02u.%02u %02u:%02u:%02u"; + + return snprintf(timestamp, len, + format_str, + entry->date.year, + entry->date.month, + entry->date.day, + entry->date.hour, + entry->date.minute, + entry->date.second); +} + static unsigned menu_displaylist_parse_core_backup_list( file_list_t *list, const char *core_path, settings_t *settings, bool restore) @@ -936,8 +961,7 @@ static unsigned menu_displaylist_parse_core_backup_list( /* Get timestamp and crc strings */ core_backup_list_get_entry_timestamp_str( entry, date_separator, timestamp, sizeof(timestamp)); - core_backup_list_get_entry_crc_str( - entry, crc, sizeof(crc)); + snprintf(crc, sizeof(crc), "%08lx", (unsigned long)entry->crc); /* Append 'auto backup' tag to timestamp, if required */ if (entry->backup_mode == CORE_BACKUP_MODE_AUTO)