Minor cleanups to config_file.c

This commit is contained in:
libretroadmin 2023-07-17 09:45:52 +02:00
parent 6651fcb92b
commit aeff636a0f
6 changed files with 65 additions and 77 deletions

View File

@ -269,7 +269,7 @@ static void gfx_set_dwm(void)
static void frontend_win32_get_os(char *s, size_t len, int *major, int *minor)
{
size_t _len;
size_t _len = 0;
char build_str[11] = {0};
bool server = false;
const char *arch = "";

View File

@ -2902,18 +2902,16 @@ static void input_poll_overlay(
*
* Returns: key identifier.
**/
enum retro_key input_config_translate_str_to_rk(const char *str)
enum retro_key input_config_translate_str_to_rk(const char *str, size_t len)
{
size_t i;
if (strlen(str) == 1 && ISALPHA((int)*str))
if (len == 1 && ISALPHA((int)*str))
return (enum retro_key)(RETROK_a + (TOLOWER((int)*str) - (int)'a'));
for (i = 0; input_config_key_map[i].str; i++)
{
if (string_is_equal_noncase(input_config_key_map[i].str, str))
return input_config_key_map[i].key;
}
RARCH_WARN("[Input]: Key name \"%s\" not found.\n", str);
return RETROK_UNKNOWN;
}
@ -4236,7 +4234,7 @@ void config_read_keybinds_conf(void *data)
entry = config_get_entry(conf, str);
if (entry && !string_is_empty(entry->value))
bind->key = input_config_translate_str_to_rk(
entry->value);
entry->value, strlen(entry->value));
/* Store new mapping bit and remember it for a while
* so that next clear leaves the new key alone */

View File

@ -148,7 +148,7 @@ void input_config_get_bind_string(void *settings_data,
*
* @return Key identifier.
**/
enum retro_key input_config_translate_str_to_rk(const char *str);
enum retro_key input_config_translate_str_to_rk(const char *str, size_t len);
/**
* Searches for a string among the "base" fields of the list of binds.

View File

@ -407,14 +407,16 @@ static void config_file_add_sub_conf(config_file_t *conf, char *path,
conf->path);
}
void config_file_add_reference(config_file_t *conf, char *path)
size_t config_file_add_reference(config_file_t *conf, char *path)
{
size_t len;
/* It is expected that the conf has it's path already set */
char short_path[PATH_MAX_LENGTH];
if (!conf->references)
conf->references = path_linked_list_new();
fill_pathname_abbreviated_or_relative(short_path, conf->path, path, sizeof(short_path));
len = fill_pathname_abbreviated_or_relative(short_path, conf->path, path, sizeof(short_path));
path_linked_list_add_path(conf->references, short_path);
return len;
}
static int config_file_load_internal(
@ -516,11 +518,9 @@ static bool config_file_parse_line(config_file_t *conf,
{
char *path = NULL;
bool include_found = string_starts_with_size(comment,
"include ",
STRLEN_CONST("include "));
"include ", STRLEN_CONST("include "));
bool reference_found = string_starts_with_size(comment,
"reference ",
STRLEN_CONST("reference "));
"reference ", STRLEN_CONST("reference "));
/* All comments except those starting with the include or
* reference directive are ignored */
@ -1331,69 +1331,66 @@ void config_set_path(config_file_t *conf, const char *entry, const char *val)
#endif
}
void config_set_double(config_file_t *conf, const char *key, double val)
size_t config_set_double(config_file_t *conf, const char *key, double val)
{
char buf[320];
#ifdef __cplusplus
snprintf(buf, sizeof(buf), "%f", (float)val);
size_t len = snprintf(buf, sizeof(buf), "%f", (float)val);
#elif defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
snprintf(buf, sizeof(buf), "%lf", val);
size_t len = snprintf(buf, sizeof(buf), "%lf", val);
#else
snprintf(buf, sizeof(buf), "%f", (float)val);
size_t len = snprintf(buf, sizeof(buf), "%f", (float)val);
#endif
config_set_string(conf, key, buf);
return len;
}
void config_set_float(config_file_t *conf, const char *key, float val)
size_t config_set_float(config_file_t *conf, const char *key, float val)
{
char buf[64];
snprintf(buf, sizeof(buf), "%f", val);
size_t len = snprintf(buf, sizeof(buf), "%f", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_int(config_file_t *conf, const char *key, int val)
size_t config_set_int(config_file_t *conf, const char *key, int val)
{
char buf[16];
snprintf(buf, sizeof(buf), "%d", val);
size_t len = snprintf(buf, sizeof(buf), "%d", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_uint(config_file_t *conf, const char *key, unsigned int val)
size_t config_set_uint(config_file_t *conf, const char *key, unsigned int val)
{
char buf[16];
snprintf(buf, sizeof(buf), "%u", val);
size_t len = snprintf(buf, sizeof(buf), "%u", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_hex(config_file_t *conf, const char *key, unsigned val)
size_t config_set_hex(config_file_t *conf, const char *key, unsigned val)
{
char buf[16];
snprintf(buf, sizeof(buf), "%x", val);
size_t len = snprintf(buf, sizeof(buf), "%x", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_uint64(config_file_t *conf, const char *key, uint64_t val)
size_t config_set_uint64(config_file_t *conf, const char *key, uint64_t val)
{
char buf[32];
snprintf(buf, sizeof(buf), "%" PRIu64, val);
size_t len = snprintf(buf, sizeof(buf), "%" PRIu64, val);
config_set_string(conf, key, buf);
return len;
}
void config_set_char(config_file_t *conf, const char *key, char val)
size_t config_set_char(config_file_t *conf, const char *key, char val)
{
char buf[2];
snprintf(buf, sizeof(buf), "%c", val);
size_t len = snprintf(buf, sizeof(buf), "%c", val);
config_set_string(conf, key, buf);
}
/**
* config_set_bool:
* TODO/FIXME - could be turned into a trivial macro or removed
**/
void config_set_bool(config_file_t *conf, const char *key, bool val)
{
config_set_string(conf, key, val ? "true" : "false");
return len;
}
/**
@ -1406,32 +1403,32 @@ bool config_file_write(config_file_t *conf, const char *path, bool sort)
if (!conf)
return false;
if (!conf->modified)
return true;
if (!string_is_empty(path))
if (conf->modified)
{
void* buf = NULL;
FILE *file = (FILE*)fopen_utf8(path, "wb");
if (!file)
return false;
if (string_is_empty(path))
config_file_dump(conf, stdout, sort);
else
{
void* buf = NULL;
FILE *file = (FILE*)fopen_utf8(path, "wb");
if (!file)
return false;
buf = calloc(1, 0x4000);
setvbuf(file, (char*)buf, _IOFBF, 0x4000);
buf = calloc(1, 0x4000);
setvbuf(file, (char*)buf, _IOFBF, 0x4000);
config_file_dump(conf, file, sort);
config_file_dump(conf, file, sort);
if (file != stdout)
fclose(file);
if (buf)
free(buf);
if (file != stdout)
fclose(file);
if (buf)
free(buf);
/* Only update modified flag if config file
* is actually written to disk */
conf->modified = false;
/* Only update modified flag if config file
* is actually written to disk */
conf->modified = false;
}
}
else
config_file_dump(conf, stdout, sort);
return true;
}

View File

@ -136,7 +136,7 @@ config_file_t *config_file_new_from_path_to_string(const char *path);
**/
void config_file_free(config_file_t *conf);
void config_file_add_reference(config_file_t *conf, char *path);
size_t config_file_add_reference(config_file_t *conf, char *path);
bool config_file_deinitialize(config_file_t *conf);
@ -291,24 +291,17 @@ bool config_get_bool(config_file_t *conf, const char *entry, bool *in);
/* Setters. Similar to the getters.
* Will not write to entry if the entry was obtained from an #include. */
void config_set_double(config_file_t *conf, const char *entry, double value);
void config_set_float(config_file_t *conf, const char *entry, float value);
void config_set_int(config_file_t *conf, const char *entry, int val);
void config_set_hex(config_file_t *conf, const char *entry, unsigned val);
void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
void config_set_char(config_file_t *conf, const char *entry, char val);
size_t config_set_double(config_file_t *conf, const char *entry, double value);
size_t config_set_float(config_file_t *conf, const char *entry, float value);
size_t config_set_int(config_file_t *conf, const char *entry, int val);
size_t config_set_hex(config_file_t *conf, const char *entry, unsigned val);
size_t config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
size_t config_set_char(config_file_t *conf, const char *entry, char val);
size_t config_set_uint(config_file_t *conf, const char *key, unsigned int val);
void config_set_path(config_file_t *conf, const char *entry, const char *val);
void config_set_string(config_file_t *conf, const char *entry, const char *val);
void config_unset(config_file_t *conf, const char *key);
void config_set_path(config_file_t *conf, const char *entry, const char *val);
/**
* config_set_bool:
* TODO/FIXME - could be turned into a trivial macro or removed
**/
void config_set_bool(config_file_t *conf, const char *entry, bool val);
void config_set_uint(config_file_t *conf, const char *key, unsigned int val);
/**
* config_file_write:

View File

@ -307,7 +307,7 @@ static bool task_overlay_load_desc(
else if (strstr(key, "retrok_") == key)
{
desc->type = OVERLAY_TYPE_KEYBOARD;
desc->retro_key_idx = input_config_translate_str_to_rk(key + 7);
desc->retro_key_idx = input_config_translate_str_to_rk(key + 7, strlen(key + 7));
}
else
{