diff options
author | thiagoftsm <thiagoftsm@gmail.com> | 2020-04-03 10:26:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-03 10:26:51 +0000 |
commit | 56ac19d8afd49f200fb2bd954d2f5bcd56c920cf (patch) | |
tree | 0e7e537097826a232aaa9562e01d69c8804078e1 /libnetdata/config | |
parent | f1177cc4f678e3ee0fe2e92dc199122d4c8348f2 (diff) |
cove355076: Config per section (#8588)
This commit brings the possibility to search an option directly when we
already have a section
Diffstat (limited to 'libnetdata/config')
-rw-r--r-- | libnetdata/config/appconfig.c | 38 | ||||
-rw-r--r-- | libnetdata/config/appconfig.h | 2 |
2 files changed, 31 insertions, 9 deletions
diff --git a/libnetdata/config/appconfig.c b/libnetdata/config/appconfig.c index 2fb21f1837..6c008234ad 100644 --- a/libnetdata/config/appconfig.c +++ b/libnetdata/config/appconfig.c @@ -285,16 +285,10 @@ cleanup: return ret; } - -char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value) +char *appconfig_get_by_section(struct section *co, const char *name, const char *default_value) { struct config_option *cv; - debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value); - - struct section *co = appconfig_section_find(root, section); - if(!co) co = appconfig_section_create(root, section); - cv = appconfig_option_index_find(co, name, 0); if(!cv) { cv = appconfig_value_create(co, name, default_value); @@ -314,6 +308,16 @@ char *appconfig_get(struct config *root, const char *section, const char *name, return(cv->value); } +char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value) +{ + debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value); + + struct section *co = appconfig_section_find(root, section); + if(!co) co = appconfig_section_create(root, section); + + return appconfig_get_by_section(co, name, default_value); +} + long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value) { char buffer[100], *s; @@ -336,6 +340,23 @@ LONG_DOUBLE appconfig_get_float(struct config *root, const char *section, const return str2ld(s, NULL); } +static inline int appconfig_test_boolean_value(char *s) { + if(!strcasecmp(s, "yes") || !strcasecmp(s, "true") || !strcasecmp(s, "on") + || !strcasecmp(s, "auto") || !strcasecmp(s, "on demand")) + return 1; + + return 0; +} + +int appconfig_get_boolean_by_section(struct section *co, const char *name, int value) { + char *s; + + s = appconfig_get_by_section(co, name, (!value)?"no":"yes"); + if(!s) return value; + + return appconfig_test_boolean_value(s); +} + int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value) { char *s; @@ -345,8 +366,7 @@ int appconfig_get_boolean(struct config *root, const char *section, const char * s = appconfig_get(root, section, name, s); if(!s) return value; - if(!strcasecmp(s, "yes") || !strcasecmp(s, "true") || !strcasecmp(s, "on") || !strcasecmp(s, "auto") || !strcasecmp(s, "on demand")) return 1; - return 0; + return appconfig_test_boolean_value(s); } int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value) diff --git a/libnetdata/config/appconfig.h b/libnetdata/config/appconfig.h index 0dea5ddf17..a0a3bd6329 100644 --- a/libnetdata/config/appconfig.h +++ b/libnetdata/config/appconfig.h @@ -159,9 +159,11 @@ extern int appconfig_load(struct config *root, char *filename, int overwrite_use extern void config_section_wrlock(struct section *co); extern void config_section_unlock(struct section *co); +extern char *appconfig_get_by_section(struct section *co, const char *name, const char *default_value); extern char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value); extern long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value); extern LONG_DOUBLE appconfig_get_float(struct config *root, const char *section, const char *name, LONG_DOUBLE value); +extern int appconfig_get_boolean_by_section(struct section *co, const char *name, int value); extern int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value); extern int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value); extern int appconfig_get_duration(struct config *root, const char *section, const char *name, const char *value); |