libmpm

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

commit 1662006ee0d43712d19cd76c440e0c81e80865e9
parent 0f9a260645159c68ddad40edd8df7b3e68838140
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 18 Jul 2017 19:25:47 +0200

Add(Config): We now have a way to get a value from configuration token, only for string atm.

Diffstat:
Minc/config.h | 10++++++++++
Msrc/config.c | 29+++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/inc/config.h b/inc/config.h @@ -111,4 +111,14 @@ void config_free(config_t **ptr); */ void config_get_error_string(config_t *ptr); +/*! + * \brief Get a string of the configuration by its name + * + * \param ptr Configuration context + * \param str Name of the member (Ex: "download.something") + * + * \return The value on success, NULL on failure + */ +char *get_conf_str_from_name(config_t *ptr, const char *str); + #endif /* CONFIG_H */ diff --git a/src/config.c b/src/config.c @@ -138,3 +138,32 @@ void config_get_error_string(config_t *ptr) { asprintf(&ptr->err, "%s:%d: %s", ptr->fn, ptr->ptr->line, g_error); strcpy(g_error, ""); } + +char *get_conf_str_from_name(config_t *ptr, const char *str) { + size_t i; + char *section, *member, *dup, *ret; + cfg_t *sec; + + if (ptr == NULL || str == NULL ) + return NULL; + + dup = strdup(str); + + for (i = 0; dup[i] != '\0' && dup[i] != '.'; i++) + ; + + if (dup[i] == '\0') + { + free(dup); + return NULL; + } + + dup[i] = '\0'; + section = dup; + member = dup + i + 1; + sec = cfg_getsec(ptr->ptr, section); + ret = cfg_getstr(sec, member); + dup[i] = '.'; + free(dup); + return ret; +}