Skip to content

Commit

Permalink
Move strftime_am_pm to libretro-common and get rid of duplicated
Browse files Browse the repository at this point in the history
function
  • Loading branch information
LibretroAdmin committed May 1, 2023
1 parent b2ff445 commit b951a01
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 78 deletions.
26 changes: 26 additions & 0 deletions libretro-common/file/file_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>

#include <sys/stat.h>

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions libretro-common/include/file/file_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 1 addition & 28 deletions menu/menu_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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.
* */
Expand Down
67 changes: 17 additions & 50 deletions runtime_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -62,19 +61,17 @@ 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)
{
if (string_is_equal(s, "runtime"))
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b951a01

Please sign in to comment.