From 9400a8491265b984c14469e1d711f3737748637d Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 30 Aug 2023 18:51:57 -0700 Subject: til_settings: add get_setting_by_{idx,key}() variants Currently everything wanting to get a settings value goes through til_settings_get_value_by_{idx,key}() functions which return the value rather than the setting directly to the callers. This was a convenient thing initially, but it's becoming apparent that most of the setup_funcs using these actually need the til_setting_t* for improved error handling in the res_setup baking phase. So this commit basically just converts the existing functions into bare til_setting_t* returns, leaving the existing get_value variants as helper wrappers around them. Subsequent commits will rework the myriad setup_funcs to use the new variants, eventually letting setup front-ends like setup_interactively() to make use of the res_setting on res_setup baking failures too. For now both variants will coexist, during the reworking. The get_value variants may go away at some point if nothing is making use of them. --- src/til_settings.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'src/til_settings.c') diff --git a/src/til_settings.c b/src/til_settings.c index 4a19788..0e59651 100644 --- a/src/til_settings.c +++ b/src/til_settings.c @@ -271,8 +271,8 @@ const char * til_settings_get_label(const til_settings_t *settings) } -/* find key= in settings, return value NULL if missing, optionally store setting @res_setting if found */ -const char * til_settings_get_value_by_key(const til_settings_t *settings, const char *key, til_setting_t **res_setting) +/* return setting matching key in settings, NULL if missing, optionally store setting @res_setting if found */ +til_setting_t * til_settings_get_setting_by_key(const til_settings_t *settings, const char *key, til_setting_t **res_setting) { assert(settings); assert(key); @@ -285,7 +285,7 @@ const char * til_settings_get_value_by_key(const til_settings_t *settings, const if (res_setting) *res_setting = settings->entries[i]; - return settings->entries[i]->value; + return settings->entries[i]; } } @@ -293,8 +293,21 @@ const char * til_settings_get_value_by_key(const til_settings_t *settings, const } -/* return positional value from settings, NULL if missing, optionally store setting @res_setting if found */ -const char * til_settings_get_value_by_idx(const til_settings_t *settings, unsigned idx, til_setting_t **res_setting) +/* return value matching key in settings, NULL if missing, optionally store setting @res_setting if found */ +const char * til_settings_get_value_by_key(const til_settings_t *settings, const char *key, til_setting_t **res_setting) +{ + til_setting_t *s; + + s = til_settings_get_setting_by_key(settings, key, res_setting); + if (!s) + return NULL; + + return s->value; +} + + +/* return positional setting from settings, NULL if missing, optionally store setting @res_setting if found */ +til_setting_t * til_settings_get_setting_by_idx(const til_settings_t *settings, unsigned idx, til_setting_t **res_setting) { assert(settings); @@ -302,13 +315,26 @@ const char * til_settings_get_value_by_idx(const til_settings_t *settings, unsig if (res_setting) *res_setting = settings->entries[idx]; - return settings->entries[idx]->value; + return settings->entries[idx]; } return NULL; } +/* return positional value from settings, NULL if missing, optionally store setting @res_setting if found */ +const char * til_settings_get_value_by_idx(const til_settings_t *settings, unsigned idx, til_setting_t **res_setting) +{ + til_setting_t *s; + + s = til_settings_get_setting_by_idx(settings, idx, res_setting); + if (!s) + return NULL; + + return s->value; +} + + /* helper for the common setup case of describing a setting when absent or not yet described. * returns: * -1 on error, res_* will be untouched in this case. -- cgit v1.2.1