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/til_settings.c | |
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/til_settings.c')
-rw-r--r-- | src/til_settings.c | 34 |
1 files changed, 34 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; +} |