From cdef3ff9f7aedaac1845b5dd4272a2f4f27a7a36 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Mon, 23 Dec 2024 07:10:03 +0100 Subject: [PATCH] strftime - write directly into string instead of using intermediary copies --- libretro-common/file/file_path.c | 9 ++------- menu/menu_driver.c | 9 ++++++--- save.c | 10 ++-------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 8334075d44..b12180b73d 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -569,20 +569,15 @@ size_t fill_str_dated_filename(char *s, const char *in_str, const char *ext, size_t len) { struct tm tm_; - char format[NAME_MAX_LENGTH]; size_t _len = 0; time_t cur_time = time(NULL); rtime_localtime(&cur_time, &tm_); _len = strlcpy(s, in_str, len); if (string_is_empty(ext)) - { - strftime(format, sizeof(format), "-%y%m%d-%H%M%S", &tm_); - _len += strlcpy(s + _len, format, len - _len); - } + _len += strftime(s + _len, len - _len, "-%y%m%d-%H%M%S", &tm_); else { - strftime(format, sizeof(format), "-%y%m%d-%H%M%S.", &tm_); - _len += strlcpy(s + _len, format, len - _len); + _len += strftime(s + _len, len - _len, "-%y%m%d-%H%M%S.", &tm_); _len += strlcpy(s + _len, ext, len - _len); } return _len; diff --git a/menu/menu_driver.c b/menu/menu_driver.c index 759f4f1382..0e9027f08e 100644 --- a/menu/menu_driver.c +++ b/menu/menu_driver.c @@ -642,6 +642,9 @@ bool menu_entries_list_search(const char *needle, size_t *idx) * */ size_t menu_display_timedate(gfx_display_ctx_datetime_t *datetime) { + /* Storage container for current menu datetime + * representation string */ + static char datetime_cache[NAME_MAX_LENGTH]; struct menu_state *menu_st = &menu_driver_state; /* Trigger an update, if required */ @@ -999,16 +1002,16 @@ size_t menu_display_timedate(gfx_display_ctx_datetime_t *datetime) } if (has_am_pm) - strftime_am_pm(menu_st->datetime_cache, sizeof(menu_st->datetime_cache), + strftime_am_pm(datetime_cache, sizeof(datetime_cache), format_str, &tm_); else - strftime(menu_st->datetime_cache, sizeof(menu_st->datetime_cache), + strftime(datetime_cache, sizeof(datetime_cache), format_str, &tm_); } /* Copy cached datetime string to input * menu_display_ctx_datetime_t struct */ - return strlcpy(datetime->s, menu_st->datetime_cache, datetime->len); + return strlcpy(datetime->s, datetime_cache, datetime->len); } /* Display current (battery) power state */ diff --git a/save.c b/save.c index ac28e4a77f..d79a494ea5 100644 --- a/save.c +++ b/save.c @@ -418,18 +418,12 @@ static bool dump_to_file_desperate(const void *data, size_t _len; time_t time_; struct tm tm_; - char timebuf[256]; - timebuf [0] = '\0'; time(&time_); - rtime_localtime(&time_, &tm_); - - strftime(timebuf, 256 * sizeof(char), - "%Y-%m-%d-%H-%M-%S", &tm_); - _len = strlcat(path, "/RetroArch-recovery-", sizeof(path)); _len += snprintf(path + _len, sizeof(path) - _len, "%u", type); - strlcpy(path + _len, timebuf, sizeof(path) - _len); + strftime(path + _len, sizeof(path) - _len, + "%Y-%m-%d-%H-%M-%S", &tm_); /* Fallback (emergency) saves are always * uncompressed