libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 8215c9f5b27d780c962fbab03dd24efec892c6b9
parent 46c681b425b960a6a05f3b7b56c01f67babca1ad
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 19 Jul 2017 20:10:25 +0200

Add(Config): Helper to get integer option, and helper to get option type

Diffstat:
Minc/config.h | 21+++++++++++++++++++++
Msrc/config.c | 28++++++++++++++++++++++++++++
2 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/inc/config.h b/inc/config.h @@ -121,4 +121,25 @@ void config_get_error_string(config_t *ptr); */ char *get_conf_str_from_name(config_t *ptr, const char *str); +/*! + * \brief Get an integer from the configuarion by its name + * + * \param ptr Configuration context + * \param str Name of the member (Ex: "download.something") + * \param ret Pointer to integer + * + * \return true on success, false on failure + */ +bool get_conf_int_from_name(config_t *ptr, const char *str, int *ret); + +/*! + * \brief Get option type from name + * + * \param ptr Configuration context + * \param str Name of the member (Ex: "download.something") + * + * \return Type on success, CFGT_NONE on error + */ +cfg_type_t get_type_from_name(config_t *ptr, const char *str); + #endif /* CONFIG_H */ diff --git a/src/config.c b/src/config.c @@ -139,6 +139,14 @@ void config_get_error_string(config_t *ptr) { strcpy(g_error, ""); } +/*! + * \brief Get an cfg_opt_t pointer from a common name + * + * \param ptr Configuration context + * \param str Name of the member (Ex: "download.something") + * + * \return A pointer on the option on success, NULL on failure + */ static cfg_opt_t *get_opt_from_name(config_t *ptr, const char *str) { cfg_t *sec; @@ -178,6 +186,13 @@ static cfg_opt_t *get_opt_from_name(config_t *ptr, const char *str) return opt; } +cfg_type_t get_type_from_name(config_t *ptr, const char *str) { + cfg_opt_t *opt = NULL; + + opt = get_opt_from_name(ptr, str); + return opt->type; +} + char *get_conf_str_from_name(config_t *ptr, const char *str) { cfg_opt_t *opt = NULL; @@ -189,3 +204,16 @@ char *get_conf_str_from_name(config_t *ptr, const char *str) { assert(opt->type == CFGT_STR); return cfg_opt_getnstr(opt, 0); } + +bool get_conf_int_from_name(config_t *ptr, const char *str, int *ret) { + cfg_opt_t *opt = NULL; + + opt = get_opt_from_name(ptr, str); + if (opt == NULL) + return false; + + /* Cannot not be a int */ + assert(opt->type == CFGT_INT); + *ret = cfg_opt_getnint(opt, 0); + return true; +}