Reduce snprintf calls

This commit is contained in:
LibretroAdmin 2022-08-25 13:03:08 +02:00
parent 1b181185c2
commit 15fe258c1b
4 changed files with 159 additions and 72 deletions

View File

@ -672,9 +672,21 @@ static void menu_action_cpu_managed_freq_label(
};
if (freq == 1)
strlcpy(s, "Min.", len);
{
s[0] = 'M';
s[1] = 'i';
s[2] = 'n';
s[3] = '.';
s[4] = '\0';
}
else if (freq == ~0U)
strlcpy(s, "Max.", len);
{
s[0] = 'M';
s[1] = 'a';
s[2] = 'x';
s[3] = '.';
s[4] = '\0';
}
else
snprintf(s, len, "%u MHz", freq / 1000);
}
@ -817,12 +829,20 @@ static void menu_action_setting_disp_set_label_input_desc(
const char *descriptor = runloop_state_get_ptr()->system.input_desc_btn[mapped_port][remap_idx];
if (!string_is_empty(descriptor))
{
if (remap_idx < RARCH_FIRST_CUSTOM_BIND)
strlcpy(s, descriptor, len);
size_t _len = strlcpy(s, descriptor, len);
if (remap_idx < RARCH_FIRST_CUSTOM_BIND) { }
else if (remap_idx % 2 == 0)
snprintf(s, len, "%s %c", descriptor, '+');
{
s[_len ] = ' ';
s[_len+1] = '+';
s[_len+2] = '\0';
}
else
snprintf(s, len, "%s %c", descriptor, '-');
{
s[_len ] = ' ';
s[_len+1] = '-';
s[_len+2] = '\0';
}
return;
}
}
@ -842,11 +862,9 @@ static void menu_action_setting_disp_set_label_input_desc_kbd(
const char *path,
char *s2, size_t len2)
{
char desc[PATH_MAX_LENGTH];
unsigned key_id, btn_idx;
unsigned remap_id;
unsigned user_idx;
settings_t *settings = config_get_ptr();
if (!settings)
@ -864,8 +882,8 @@ static void menu_action_setting_disp_set_label_input_desc_kbd(
if (key_descriptors[key_id].key != RETROK_FIRST)
{
snprintf(desc, sizeof(desc), "Keyboard %s", key_descriptors[key_id].desc);
strlcpy(s, desc, len);
strlcpy(s, "Keyboard ", len);
strlcat(s, key_descriptors[key_id].desc, len);
}
else
{

View File

@ -69,6 +69,7 @@
static int menu_action_sublabel_file_browser_core(file_list_t *list, unsigned type, unsigned i, const char *label, const char *path, char *s, size_t len)
{
size_t _len;
core_info_t *core_info = NULL;
/* Search for specified core */
@ -80,17 +81,22 @@ static int menu_action_sublabel_file_browser_core(file_list_t *list, unsigned ty
/* Add license text */
string_list_join_concat(tmp, sizeof(tmp),
core_info->licenses_list, ", ");
snprintf(s, len, "%s: %s",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES),
tmp);
_len = strlcpy(s,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES), len);
s[_len ] = ':';
s[_len+1] = ' ';
s[_len+2] = '\0';
strlcat(s, tmp, len);
return 1;
}
/* No license found - set to N/A */
snprintf(s, len, "%s: %s",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES),
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE)
);
_len = strlcpy(s,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES), len);
s[_len ] = ':';
s[_len+1] = ' ';
s[_len+2] = '\0';
strlcat(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE), len);
return 1;
}
@ -1539,7 +1545,14 @@ static int action_bind_sublabel_netplay_room(file_list_t *list,
snprintf(buf, sizeof(buf), "(%08lX)",
(unsigned long)(unsigned)room->gamecrc);
else
snprintf(buf, sizeof(buf), "(%s)", room->subsystem_name);
{
size_t _len;
buf[0 ] = '(';
buf[1 ] = '\0';
_len = strlcat(buf, room->subsystem_name, sizeof(buf));
buf[_len ] = ')';
buf[_len+1] = '\0';
}
strlcat(s, buf, len);
@ -1579,8 +1592,14 @@ static int action_bind_sublabel_netplay_kick_client(file_list_t *list,
if (status)
{
snprintf(buf, sizeof(buf), "%s: %s\n",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_STATUS), status);
size_t _len = strlcpy(buf, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_STATUS),
sizeof(buf));
buf[_len ] = ':';
buf[_len+1] = ' ';
buf[_len+2] = '\0';
_len = strlcat(buf, status, sizeof(buf));
buf[_len ] = '\n';
buf[_len+1] = '\0';
strlcat(s, buf, len);
}
@ -1659,6 +1678,7 @@ static int action_bind_sublabel_playlist_entry(
const char *label, const char *path,
char *s, size_t len)
{
size_t _len;
size_t list_size = menu_entries_get_size();
playlist_t *playlist = NULL;
const struct playlist_entry *entry = NULL;
@ -1717,10 +1737,11 @@ static int action_bind_sublabel_playlist_entry(
return 0;
/* Add core name */
snprintf(s, len, "%s %s",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_CORE),
entry->core_name
);
_len = strlcpy(s,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_CORE), len);
s[_len ] = ' ';
s[_len+1] = '\0';
strlcat(s, entry->core_name, len);
/* Get runtime info *if* required runtime log is enabled
* *and* this is a valid playlist type */
@ -1842,6 +1863,7 @@ static int action_bind_sublabel_core_updater_entry(
const char *label, const char *path,
char *s, size_t len)
{
size_t _len;
core_updater_list_t *core_list = core_updater_list_get_cached();
const core_updater_list_entry_t *entry = NULL;
@ -1855,20 +1877,22 @@ static int action_bind_sublabel_core_updater_entry(
/* Add license text */
string_list_join_concat(tmp, sizeof(tmp),
entry->licenses_list, ", ");
snprintf(s, len,
"%s: %s",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES),
tmp
);
_len = strlcpy(s,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES), len);
s[_len ] = ':';
s[_len+1] = ' ';
s[_len+2] = '\0';
strlcat(s, tmp, len);
return 1;
}
/* No license found - set to N/A */
snprintf(s, len,
"%s: %s",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES),
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE)
);
_len = strlcpy(s,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFO_LICENSES), len);
s[_len ] = ':';
s[_len+1] = ' ';
s[_len+2] = '\0';
strlcat(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE), len);
return 1;
}
#endif
@ -1884,7 +1908,7 @@ static int action_bind_sublabel_core_backup_entry(
? list->list[i].alt
: list->list[i].path;
/* Set sublabel prefix */
size_t _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_BACKUP_CRC), len);
size_t _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_BACKUP_CRC), len);
/* Add crc string */
if (string_is_empty(crc))

View File

@ -68,7 +68,13 @@
{ \
const char *title = msg_hash_to_str(lbl); \
if (!string_is_empty(path) && !string_is_empty(title)) \
snprintf(s, len, "%s: %s", title, path); \
{ \
size_t _len = strlcpy(s, title, len); \
s[_len ] = ':'; \
s[_len+1] = ' '; \
s[_len+2] = '\0'; \
strlcat(s, path, len); \
} \
else if (!string_is_empty(title)) \
strlcpy(s, title, len); \
return 1; \
@ -101,12 +107,14 @@ static void action_get_title_fill_search_filter_default(
static void action_get_title_fill_path_search_filter_default(
const char *path, enum msg_hash_enums lbl, char *s, size_t len)
{
size_t _len = 0;
const char *title = msg_hash_to_str(lbl);
snprintf(s, len, "%s %s",
string_is_empty(title) ? "" : title,
string_is_empty(path) ? "" : path
);
if (!string_is_empty(title))
_len = strlcpy(s, title, len);
s[_len ] = ' ';
s[_len+1] = '\0';
if (!string_is_empty(path))
strlcat(s, path, len);
menu_entries_search_append_terms_string(s, len);
}
@ -411,26 +419,28 @@ static int action_get_title_deferred_playlist_list(const char *path, const char
static int action_get_title_deferred_core_backup_list(
const char *core_path, const char *prefix, char *s, size_t len)
{
size_t _len;
core_info_t *core_info = NULL;
if (string_is_empty(core_path) || string_is_empty(prefix))
return 0;
_len = strlcpy(s, prefix, len);
s[_len ] = ':';
s[_len+1] = ' ';
s[_len+2] = '\0';
/* Search for specified core
* > If core is found, add display name */
if (core_info_find(core_path, &core_info) &&
core_info->display_name)
snprintf(s, len, "%s: %s", prefix,
core_info->display_name);
strlcat(s, core_info->display_name, len);
else
{
/* > If not, use core file name */
const char *core_filename = path_basename_nocompression(core_path);
if (!string_is_empty(core_filename))
snprintf(s, len, "%s: %s", prefix,
core_filename);
else
snprintf(s, len, "%s: ", prefix);
strlcat(s, core_filename, len);
}
return 1;
@ -494,7 +504,12 @@ static int action_get_core_information_steam_list(
const char *path, const char *label, unsigned menu_type,
char *s, size_t len)
{
snprintf(s, len, "%s - %s", msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFORMATION), path);
size_t _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_CORE_INFORMATION), len);
s[_len ] = ' ';
s[_len+1] = '-';
s[_len+2] = ' ';
s[_len+3] = '\0';
strlcat(s, path, len);
return 1;
}
#endif
@ -838,22 +853,25 @@ DEFAULT_TITLE_GENERIC_MACRO(action_get_title_list_rdb_entry_database_info,MENU_E
static int action_get_sideload_core_list(const char *path, const char *label,
unsigned menu_type, char *s, size_t len)
{
snprintf(s, len,
"%s %s", msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SIDELOAD_CORE_LIST),
string_is_empty(path) ? "" : path
);
size_t _len = strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SIDELOAD_CORE_LIST), len);
s[_len ] = ' ';
s[_len+1] = '\0';
if (!string_is_empty(path))
strlcat(s, path, len);
return 0;
}
static int action_get_title_default(const char *path, const char *label,
unsigned menu_type, char *s, size_t len)
{
size_t _len = strlcpy(s,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SELECT_FILE), len);
if (!string_is_empty(path))
snprintf(s, len, "%s %s",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SELECT_FILE),
path);
else
strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_SELECT_FILE), len);
{
s[_len ] = ' ';
s[_len+1] = '\0';
strlcat(s, path, len);
}
menu_entries_search_append_terms_string(s, len);
@ -904,6 +922,7 @@ static int action_get_title_group_settings(const char *path, const char *label,
}
{
size_t _len;
char elem0[255];
char elem1[255];
struct string_list list_label = {0};
@ -918,10 +937,15 @@ static int action_get_title_group_settings(const char *path, const char *label,
}
string_list_deinitialize(&list_label);
_len = strlcpy(s, elem0, len);
if (!string_is_empty(elem1))
snprintf(s, len, "%s - %s", elem0, elem1);
else
strlcpy(s, elem0, len);
{
s[_len ] = ' ';
s[_len+1] = '-';
s[_len+2] = ' ';
s[_len+2] = '\0';
strlcat(s, elem1, len);
}
}
return 0;

View File

@ -575,14 +575,32 @@ void runtime_log_get_runtime_usec(
void runtime_log_get_runtime_str(runtime_log_t *runtime_log,
char *s, size_t len)
{
size_t _len = strlcpy(s,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_RUNTIME),
len);
s[_len ] = ' ';
s[_len+1] = '\0';
if (runtime_log)
snprintf(s, len, "%s %02u:%02u:%02u",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_RUNTIME),
{
char t[64];
t[0] = '\0';
snprintf(t, sizeof(t), "%02u:%02u:%02u",
runtime_log->runtime.hours, runtime_log->runtime.minutes,
runtime_log->runtime.seconds);
strlcat(s, t, len);
}
else
snprintf(s, len, "%s 00:00:00",
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_RUNTIME));
{
s[_len+1] = '0';
s[_len+2] = '0';
s[_len+3] = ':';
s[_len+4] = '0';
s[_len+5] = '0';
s[_len+6] = ':';
s[_len+7] = '0';
s[_len+8] = '0';
s[_len+9] = '\0';
}
}
/* Gets last played entry values */
@ -702,6 +720,7 @@ void runtime_log_get_last_played_str(runtime_log_t *runtime_log,
enum playlist_sublabel_last_played_style_type timedate_style,
enum playlist_sublabel_last_played_date_separator_type date_separator)
{
size_t _len;
char tmp[64];
bool has_am_pm = false;
const char *format_str = "";
@ -852,10 +871,11 @@ void runtime_log_get_last_played_str(runtime_log_t *runtime_log,
runtime_log_get_last_played_time(runtime_log, &time_info);
runtime_last_played_strftime(tmp, sizeof(tmp), format_str, &time_info);
}
snprintf(str, len, "%s%s",
msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_LAST_PLAYED),
tmp);
_len = strlcpy(str, msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_LAST_PLAYED), len);
str[_len ] = ' ';
str[_len+1] = '\0';
strlcat(str, tmp, len);
return;
}
@ -1138,10 +1158,11 @@ void runtime_log_get_last_played_str(runtime_log_t *runtime_log,
msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_PLAYLIST_INLINE_CORE_DISPLAY_NEVER),
sizeof(tmp));
snprintf(str, len, "%s %s",
msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_LAST_PLAYED),
tmp);
_len = strlcpy(str, msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_PLAYLIST_SUBLABEL_LAST_PLAYED), len);
str[_len ] = ' ';
str[_len+1] = '\0';
strlcat(str, tmp, len);
return;
case PLAYLIST_LAST_PLAYED_STYLE_YMD_HMS:
default: