Allow passing fallback value for constant resolution
This commit is contained in:
parent
820480cc8a
commit
174d57666b
13
config.c
13
config.c
|
@ -195,21 +195,24 @@ reset_config(FullConfig *config)
|
|||
}
|
||||
|
||||
long long
|
||||
resolve_constant(const ConstantRegistry * registry, const config_setting_t * setting)
|
||||
resolve_constant_or(const ConstantRegistry * registry, const config_setting_t * setting, long long dflt)
|
||||
{
|
||||
if (!setting) {
|
||||
return 0;
|
||||
return dflt;
|
||||
}
|
||||
if (config_setting_type(setting) == CONFIG_TYPE_STRING) {
|
||||
if (!registry) {
|
||||
return 1;
|
||||
return dflt;
|
||||
}
|
||||
const char *name = config_setting_get_string(setting);
|
||||
HashTableIndex idx = hash_table_find(registry, hash_table_key_from_cstr(name));
|
||||
if (idx < 0) {
|
||||
return 0;
|
||||
return dflt;
|
||||
}
|
||||
return registry->value_array[idx];
|
||||
}
|
||||
return config_setting_get_int64(setting);
|
||||
if (config_setting_is_number(setting)) {
|
||||
return config_setting_get_int64(setting);
|
||||
}
|
||||
return dflt;
|
||||
}
|
||||
|
|
8
config.h
8
config.h
|
@ -38,6 +38,12 @@ typedef struct {
|
|||
|
||||
bool load_config(const config_setting_t *config_root, FullConfig *config);
|
||||
void reset_config(FullConfig *config);
|
||||
long long resolve_constant(const ConstantRegistry * registry, const config_setting_t * setting);
|
||||
long long resolve_constant_or(const ConstantRegistry * registry, const config_setting_t * setting, long long dflt);
|
||||
|
||||
__attribute__((unused)) inline static long long
|
||||
resolve_constant(const ConstantRegistry * registry, const config_setting_t * setting)
|
||||
{
|
||||
return resolve_constant_or(registry, setting, 0);
|
||||
}
|
||||
|
||||
#endif /* end of include guard: CONFIG_H_ */
|
||||
|
|
Loading…
Reference in New Issue