cove355076: Config per section (#8588)

This commit brings the possibility to search an option directly when we
already have a section
This commit is contained in:
thiagoftsm 2020-04-03 10:26:51 +00:00 committed by GitHub
parent f1177cc4f6
commit 56ac19d8af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 10 deletions

View File

@ -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)

View File

@ -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);

View File

@ -174,7 +174,7 @@ int configured_as_master() {
uuid_t uuid;
if (uuid_parse(section->name, uuid) != -1 &&
appconfig_get_boolean(&stream_config, section->name, "enabled", 0)) {
appconfig_get_boolean_by_section(section, "enabled", 0)) {
is_master = 1;
break;
}