From 4177224c95e9922d764562abe25d9bcd2b097c61 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 15 Nov 2019 14:24:54 -0800 Subject: settings: add setting_desc_check() helper Rudimentary setting value checking against the description. For now it just enforces the multiple-choice stuff, I'm undecided on the regex support for now. It'd just be nice to throw some more informative errors when cli arguments are incorrect for things like fullscreen=yes when it only knows fullscreen=on/off. --- src/settings.c | 20 ++++++++++++++++++++ src/settings.h | 1 + 2 files changed, 21 insertions(+) (limited to 'src') diff --git a/src/settings.c b/src/settings.c index c24c280..c7419e1 100644 --- a/src/settings.c +++ b/src/settings.c @@ -289,6 +289,26 @@ void setting_desc_free(setting_desc_t *desc) } +int setting_desc_check(const setting_desc_t *desc, const char *value) +{ + assert(desc); + + if (desc->values) { + + for (int i = 0; desc->values[i]; i++) { + if (!strcasecmp(desc->values[i], value)) + return 0; + } + + return -EINVAL; + } + + /* TODO: apply regex check */ + + return 0; +} + + /* wrapper around sprintf for convenient buffer size computation */ /* supply NULL buf when computing size, size and offset are ignored. * supply non-NULL for actual writing into buf of size bytes @ offset. diff --git a/src/settings.h b/src/settings.h index 26baca7..198f8b6 100644 --- a/src/settings.h +++ b/src/settings.h @@ -32,6 +32,7 @@ int settings_apply_desc_generators(const settings_t *settings, const setting_des int setting_desc_clone(const setting_desc_t *desc, setting_desc_t **res_desc); void setting_desc_free(setting_desc_t *desc); +int setting_desc_check(const setting_desc_t *desc, const char *value); #ifndef SETTINGS_STR #define _SETTINGS_STR(s) #s -- cgit v1.2.3