summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/til_settings.c34
-rw-r--r--src/til_settings.h1
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
© All Rights Reserved