Skip to content

Commit

Permalink
Minor cleanups to config_file.c
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jul 17, 2023
1 parent 6651fcb commit aeff636
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 77 deletions.
2 changes: 1 addition & 1 deletion frontend/drivers/platform_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
8 changes: 3 additions & 5 deletions input/input_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion input/input_remapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
101 changes: 49 additions & 52 deletions libretro-common/file/config_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
Expand Down
27 changes: 10 additions & 17 deletions libretro-common/include/file/config_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tasks/task_overlay.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit aeff636

Please sign in to comment.