From 1c38dc67c28c3537a1f841a127e3bfc2ff55ee09 Mon Sep 17 00:00:00 2001 From: twinaphex <libretro@gmail.com> Date: Thu, 25 Jun 2020 03:03:42 +0200 Subject: [PATCH] Optimize config_get_bool --- libretro-common/file/config_file.c | 33 ++++++++++++++-------- libretro-common/include/file/config_file.h | 10 ++++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/libretro-common/file/config_file.c b/libretro-common/file/config_file.c index 95d3a73acd..d70400055f 100644 --- a/libretro-common/file/config_file.c +++ b/libretro-common/file/config_file.c @@ -415,6 +415,23 @@ static bool parse_line(config_file_t *conf, return false; } + if ( + string_is_equal(list->value, "true") + || string_is_equal(list->value, "1") + ) + { + list->type = CONFIG_FILE_ENTRY_TYPE_BOOL; + list->value_bool = true; + } + else if ( + string_is_equal(list->value, "false") + || string_is_equal(list->value, "0") + ) + { + list->type = CONFIG_FILE_ENTRY_TYPE_BOOL; + list->value_bool = false; + } + return true; } @@ -878,21 +895,13 @@ bool config_get_bool(config_file_t *conf, const char *key, bool *in) { const struct config_entry_list *entry = config_get_entry(conf, key, NULL); - if (entry) + if (entry && entry->type == CONFIG_FILE_ENTRY_TYPE_BOOL) { - if (string_is_equal(entry->value, "true")) - *in = true; - else if (string_is_equal(entry->value, "1")) - *in = true; - else if (string_is_equal(entry->value, "false")) - *in = false; - else if (string_is_equal(entry->value, "0")) - *in = false; - else - return false; + *in = entry->value_bool; + return true; } - return entry != NULL; + return false; } void config_set_string(config_file_t *conf, const char *key, const char *val) diff --git a/libretro-common/include/file/config_file.h b/libretro-common/include/file/config_file.h index c972f768a5..0a99996ba6 100644 --- a/libretro-common/include/file/config_file.h +++ b/libretro-common/include/file/config_file.h @@ -51,6 +51,13 @@ RETRO_BEGIN_DECLS base->var = tmp; \ } while(0) +enum config_file_entry_type +{ + CONFIG_FILE_ENTRY_TYPE_DONTCARE = 0, + CONFIG_FILE_ENTRY_TYPE_BOOL +}; + + struct config_file { char *path; @@ -120,10 +127,11 @@ struct config_entry_list char *key; char *value; + enum config_file_entry_type type; + bool value_bool; struct config_entry_list *next; }; - struct config_file_entry { const char *key;