diff options
| -rw-r--r-- | src/til_settings.c | 41 | ||||
| -rw-r--r-- | src/til_settings.h | 1 | 
2 files changed, 42 insertions, 0 deletions
| diff --git a/src/til_settings.c b/src/til_settings.c index 0e59651..fb21af7 100644 --- a/src/til_settings.c +++ b/src/til_settings.c @@ -338,6 +338,47 @@ const char * til_settings_get_value_by_idx(const til_settings_t *settings, unsig  /* 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. + * 0 when setting is present and described, res_setting and res_setting_bis(optional) will be populated w/non-NULL, and res_desc NULL in this case. + * 1 when setting is either present but undescribed, or absent (and undescribed), res_* will be populated but res_setting{,_bis} may be NULL if absent and simply described. + */ +int til_settings_get_and_describe_setting(const til_settings_t *settings, const til_setting_spec_t *spec, til_setting_t **res_setting, til_setting_t **res_setting_bis, const til_setting_desc_t **res_desc) +{ +	til_setting_t	*setting; + +	assert(settings); +	assert(spec); +	assert(res_setting); + +	setting = til_settings_get_setting_by_key(settings, spec->key, NULL); +	if (!setting || !setting->desc) { +		int	r; + +		assert(res_desc); + +		r = til_setting_desc_new(settings, spec, res_desc); +		if (r < 0) +			return r; + +		*res_setting = setting; +		if (res_setting_bis) +			*res_setting_bis = setting; + +		return 1; +	} + +	*res_setting = setting; +	if (res_setting_bis) +		*res_setting_bis = setting; +	if (res_desc) +		*res_desc = NULL; + +	return 0; +} + + +/* 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.   * 0 when setting is present and described, res_value and res_setting will be populated w/non-NULL, and res_desc NULL in this case.   * 1 when setting is either present but undescribed, or absent (and undescribed), res_* will be populated but res_{value,setting} may be NULL if absent and simply described.   */ diff --git a/src/til_settings.h b/src/til_settings.h index cbfc599..be01b5b 100644 --- a/src/til_settings.h +++ b/src/til_settings.h @@ -58,6 +58,7 @@ til_setting_t * til_settings_get_setting_by_idx(const til_settings_t *settings,  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); +int til_settings_get_and_describe_setting(const til_settings_t *settings, const til_setting_spec_t *spec, til_setting_t **res_setting, til_setting_t **res_setting_bis, const til_setting_desc_t **res_desc);  int til_settings_get_and_describe_value(const til_settings_t *settings, const til_setting_spec_t *spec, const char **res_value, til_setting_t **res_setting, const til_setting_desc_t **res_desc);  char * til_settings_as_arg(const til_settings_t *settings);  char * til_settings_as_arg_unfiltered(const til_settings_t *settings); | 
