Move strftime_am_pm to libretro-common and get rid of duplicated

function
This commit is contained in:
libretroadmin 2023-05-01 19:03:11 +02:00
parent b2ff445531
commit b951a010fd
4 changed files with 49 additions and 78 deletions

View File

@ -24,6 +24,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>
#include <sys/stat.h>
@ -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

View File

@ -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);

View File

@ -20,9 +20,8 @@
#include "../config.h"
#endif
#include <locale.h>
#include <retro_timers.h>
#include <file/file_path.h>
#include <lists/dir_list.h>
#include <string/stdstring.h>
#include <compat/strcasestr.h>
@ -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.
* */

View File

@ -24,7 +24,6 @@
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <locale.h>
#include <file/file_path.h>
#include <retro_miscellaneous.h>
@ -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);