Allow passing fallback value for constant resolution

This commit is contained in:
Vftdan 2024-08-16 11:56:04 +02:00
parent 820480cc8a
commit 174d57666b
2 changed files with 15 additions and 6 deletions

View File

@ -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];
}
if (config_setting_is_number(setting)) {
return config_setting_get_int64(setting);
}
return dflt;
}

View File

@ -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_ */