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 ++++++++++++++++++++++++++++++++------ src/til_settings.h | 2 ++ 2 files changed, 34 insertions(+), 6 deletions(-) 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. diff --git a/src/til_settings.h b/src/til_settings.h index 0a72931..cbfc599 100644 --- a/src/til_settings.h +++ b/src/til_settings.h @@ -52,7 +52,9 @@ unsigned til_settings_get_count(const til_settings_t *settings); const til_settings_t * til_settings_get_parent(const til_settings_t *settings); int til_settings_set_label(til_settings_t *settings, const char *label); const char * til_settings_get_label(const til_settings_t *settings); +til_setting_t * til_settings_get_setting_by_key(const til_settings_t *settings, const char *key, til_setting_t **res_setting); const char * til_settings_get_value_by_key(const til_settings_t *settings, const char *key, til_setting_t **res_setting); +til_setting_t * til_settings_get_setting_by_idx(const til_settings_t *settings, unsigned idx, til_setting_t **res_setting); const char * til_settings_get_value_by_idx(const til_settings_t *settings, unsigned idx, til_setting_t **res_setting); til_setting_t * til_settings_add_value(til_settings_t *settings, const char *key, const char *value); void til_settings_reset_descs(til_settings_t *settings); -- cgit v1.2.3