From b951a010fdd918681f147d6e13935f62aa27a611 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Mon, 1 May 2023 19:03:11 +0200 Subject: [PATCH] Move strftime_am_pm to libretro-common and get rid of duplicated function --- libretro-common/file/file_path.c | 26 +++++++++ libretro-common/include/file/file_path.h | 5 ++ menu/menu_driver.c | 29 +--------- runtime_file.c | 67 ++++++------------------ 4 files changed, 49 insertions(+), 78 deletions(-) diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 19b649849e..7e8b075218 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -68,6 +69,31 @@ #endif +/* Time format strings with AM-PM designation require special + * handling due to platform dependence */ +void strftime_am_pm(char *s, size_t len, const char* format, + const void *ptr) +{ + char *local = NULL; + const struct tm *timeptr = (const struct tm*)ptr; + + /* Ensure correct locale is set + * > Required for localised AM/PM strings */ + setlocale(LC_TIME, ""); + + strftime(s, len, format, timeptr); +#if !(defined(__linux__) && !defined(ANDROID)) + if ((local = local_to_utf8_string_alloc(s))) + { + if (!string_is_empty(local)) + strlcpy(s, local, len); + + free(local); + local = NULL; + } +#endif +} + /** * Create a new linked list with one node in it * The path on this node will be set to NULL diff --git a/libretro-common/include/file/file_path.h b/libretro-common/include/file/file_path.h index 5a81aa2365..3aa81f5701 100644 --- a/libretro-common/include/file/file_path.h +++ b/libretro-common/include/file/file_path.h @@ -664,6 +664,11 @@ bool path_mkdir(const char *dir); */ bool path_is_directory(const char *path); +/* Time format strings with AM-PM designation require special + * handling due to platform dependence */ +void strftime_am_pm(char *s, size_t len, const char* format, + const void* timeptr); + bool path_is_character_special(const char *path); int path_stat(const char *path); diff --git a/menu/menu_driver.c b/menu/menu_driver.c index 87047a82ca..3416f6b17d 100644 --- a/menu/menu_driver.c +++ b/menu/menu_driver.c @@ -20,9 +20,8 @@ #include "../config.h" #endif -#include - #include +#include #include #include #include @@ -676,32 +675,6 @@ bool menu_entries_list_search(const char *needle, size_t *idx) return match_found; } -/* Time format strings with AM-PM designation require special - * handling due to platform dependence */ -static void strftime_am_pm(char *s, size_t len, const char* format, - const struct tm* timeptr) -{ - char *local = NULL; - - /* Ensure correct locale is set - * > Required for localised AM/PM strings */ - setlocale(LC_TIME, ""); - - strftime(s, len, format, timeptr); -#if !(defined(__linux__) && !defined(ANDROID)) - local = local_to_utf8_string_alloc(s); - if (local) - { - if (!string_is_empty(local)) - strlcpy(s, local, len); - - free(local); - local = NULL; - } -#endif -} - - /* Display the date and time - time_mode will influence how * the time representation will look like. * */ diff --git a/runtime_file.c b/runtime_file.c index 05b80ec29d..ef38e21c52 100644 --- a/runtime_file.c +++ b/runtime_file.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -62,11 +61,9 @@ static bool RtlJSONObjectMemberHandler(void *ctx, const char *s, size_t len) { RtlJSONContext *p_ctx = (RtlJSONContext*)ctx; + /* Something went wrong */ if (p_ctx->current_entry_val) - { - /* something went wrong */ return false; - } if (len) { @@ -74,7 +71,7 @@ static bool RtlJSONObjectMemberHandler(void *ctx, const char *s, size_t len) p_ctx->current_entry_val = &p_ctx->runtime_string; else if (string_is_equal(s, "last_played")) p_ctx->current_entry_val = &p_ctx->last_played_string; - /* ignore unknown members */ + /* Ignore unknown members */ } return true; @@ -91,8 +88,8 @@ static bool RtlJSONStringHandler(void *ctx, const char *s, size_t len) *p_ctx->current_entry_val = strdup(s); } - /* ignore unknown members */ + /* Ignore unknown members */ p_ctx->current_entry_val = NULL; return true; @@ -104,6 +101,7 @@ static bool RtlJSONStringHandler(void *ctx, const char *s, size_t len) * Does nothing if log file does not exist. */ static void runtime_log_read_file(runtime_log_t *runtime_log) { + rjson_t* parser; unsigned runtime_hours = 0; unsigned runtime_minutes = 0; unsigned runtime_seconds = 0; @@ -116,8 +114,6 @@ static void runtime_log_read_file(runtime_log_t *runtime_log) unsigned last_played_second = 0; RtlJSONContext context = {0}; - rjson_t* parser; - /* Attempt to open log file */ RFILE *file = filestream_open(runtime_log->path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); @@ -129,8 +125,7 @@ static void runtime_log_read_file(runtime_log_t *runtime_log) } /* Initialise JSON parser */ - parser = rjson_open_rfile(file); - if (!parser) + if (!(parser = rjson_open_rfile(file))) { RARCH_ERR("Failed to create JSON parser.\n"); goto end; @@ -212,7 +207,6 @@ static void runtime_log_read_file(runtime_log_t *runtime_log) runtime_log->last_played.second = last_played_second; end: - /* Clean up leftover strings */ if (context.runtime_string) free(context.runtime_string); @@ -247,17 +241,17 @@ runtime_log_t *runtime_log_init( content_name[0] = '\0'; core_name[0] = '\0'; - if ( string_is_empty(dir_runtime_log) && - string_is_empty(dir_playlist)) + if ( string_is_empty(dir_runtime_log) + && string_is_empty(dir_playlist)) { RARCH_ERR("Runtime log directory is undefined - cannot save" " runtime log files.\n"); return NULL; } - if ( string_is_empty(core_path) || - string_is_equal(core_path, "builtin") || - string_is_equal(core_path, "DETECT")) + if ( string_is_empty(core_path) + || string_is_equal(core_path, "builtin") + || string_is_equal(core_path, "DETECT")) return NULL; /* Get core info: @@ -375,9 +369,7 @@ runtime_log_t *runtime_log_init( /* Phew... If we get this far then all is well. * > Create 'runtime_log' object */ - runtime_log = (runtime_log_t*) - malloc(sizeof(*runtime_log)); - if (!runtime_log) + if (!(runtime_log = (runtime_log_t*)malloc(sizeof(*runtime_log)))) return NULL; /* > Populate default values */ @@ -441,13 +433,11 @@ void runtime_log_set_runtime_hms(runtime_log_t *runtime_log, void runtime_log_set_runtime_usec( runtime_log_t *runtime_log, retro_time_t usec) { - if (!runtime_log) - return; - - runtime_log_convert_usec2hms(usec, - &runtime_log->runtime.hours, - &runtime_log->runtime.minutes, - &runtime_log->runtime.seconds); + if (runtime_log) + runtime_log_convert_usec2hms(usec, + &runtime_log->runtime.hours, + &runtime_log->runtime.minutes, + &runtime_log->runtime.seconds); } /* Adds specified hours, minutes, seconds value to current runtime */ @@ -647,29 +637,6 @@ void runtime_log_get_last_played_time(runtime_log_t *runtime_log, mktime(time_info); } -static void runtime_last_played_strftime( - char *s, size_t len, const char *format, - const struct tm *timeptr) -{ - char *local = NULL; - - /* Ensure correct locale is set */ - setlocale(LC_TIME, ""); - - /* Generate string */ - strftime(s, len, format, timeptr); -#if !(defined(__linux__) && !defined(ANDROID)) - if ((local = local_to_utf8_string_alloc(s))) - { - if (!string_is_empty(local)) - strlcpy(s, local, len); - - free(local); - local = NULL; - } -#endif -} - static bool runtime_last_played_human(runtime_log_t *runtime_log, char *str, size_t len) { @@ -877,7 +844,7 @@ void runtime_log_get_last_played_str(runtime_log_t *runtime_log, /* Get time */ struct tm time_info; runtime_log_get_last_played_time(runtime_log, &time_info); - runtime_last_played_strftime(tmp, sizeof(tmp), format_str, &time_info); + strftime_am_pm(tmp, sizeof(tmp), format_str, &time_info); str[_len ] = ' '; str[_len+1] = '\0'; strlcat(str, tmp, len);