diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2023-05-09 17:15:19 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2023-05-11 15:19:25 -0700 |
commit | 80905545cdd85536191486b8fe2095686d197f58 (patch) | |
tree | 586212fbbdceea65aa471b50dd7dc3fd48ad1346 /src | |
parent | 40feb616242c4e29395659ff1873c9fa35b31dcd (diff) |
til_settings: helper for labeling positional instances
When there's a bare-value setting turned into a nested settings
instance, there's no key onhand for labeling the instance.
But such nameless settings are basically array elements only
positionally accessed. This helper produces labels in that style
for such settings by taking the container settings label and
adding [$idx] to the end.
These labels aren't really used as more than a debugging aid at
the moment. But module contexts already have paths in main, and
it seems like these settings instance labels will likely become
the name components in constructing those paths.
Diffstat (limited to 'src')
-rw-r--r-- | src/til_settings.c | 34 | ||||
-rw-r--r-- | src/til_settings.h | 1 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/til_settings.c b/src/til_settings.c index 239e359..46844fd 100644 --- a/src/til_settings.c +++ b/src/til_settings.c @@ -537,3 +537,37 @@ char * til_settings_as_arg(const til_settings_t *settings) return outbuf; } + + +/* generate a positional label for a given setting, stored @ res_label. + * this is added specifically for labeling bare-value settings in an array subscript fashion... + */ +int til_settings_label_setting(const til_settings_t *settings, const til_setting_t *setting, char **res_label) +{ + char *label; + + assert(settings && settings->label); + assert(setting); + assert(res_label); + + /* Have to search for the setting, but shouldn't be perf-sensitive + * since we don't do stuff like this every frame or anything. + * I suppose til_setting_t could cache its position when added... TODO + */ + for (unsigned i = 0; i < settings->num; i++) { + if (settings->settings[i] == setting) { + size_t len = snprintf(NULL, 0, "%s[%u]", settings->label, i) + 1; + + label = calloc(1, len); + if (!label) + return -ENOMEM; + + snprintf(label, len, "%s[%u]", settings->label, i); + *res_label = label; + + return 0; + } + } + + return -ENOENT; +} diff --git a/src/til_settings.h b/src/til_settings.h index a2dacb8..0998f93 100644 --- a/src/til_settings.h +++ b/src/til_settings.h @@ -55,6 +55,7 @@ int til_settings_apply_desc_generators(const til_settings_t *settings, const til int til_setting_desc_new(const til_settings_t *settings, const til_setting_spec_t *spec, const til_setting_desc_t **res_desc); til_setting_desc_t * til_setting_desc_free(const til_setting_desc_t *desc); int til_setting_spec_check(const til_setting_spec_t *spec, const char *value); +int til_settings_label_setting(const til_settings_t *settings, const til_setting_t *setting, char **res_label); #ifndef TIL_SETTINGS_STR #define _TIL_SETTINGS_STR(s) #s |