libmpm

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

commit 7a592241995aa1de6cfaeee9aa55fc9d1d73c1b3
parent bed63db2a0c4a3625ab5329f1479b2dea57db707
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 30 May 2017 18:57:17 +0200

Add(Errors): Add the possibility to set error strings

Diffstat:
Minc/config.h | 2--
Minc/error.h | 15+++++++++++++++
Msrc/error.c | 14++++++++++++++
Mtests/test_error.c | 8++++++++
4 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/inc/config.h b/inc/config.h @@ -80,8 +80,6 @@ # define CONFIG_BOOT_GRUB_DIR "grub-dir" # define CONFIG_BOOT_GRUB_DIR_DEF "/boot/grub" -# define ERROR_MAX_LEN 250 - typedef struct config_s { cfg_t *ptr; char *fn; diff --git a/inc/error.h b/inc/error.h @@ -19,6 +19,7 @@ # include <morphux.h> +# define ERROR_MAX_LEN 250 # define SET_ERR_STR(num, str) [num] = str typedef enum u32_t { @@ -79,4 +80,18 @@ mpm_error_t get_mpm_error(void); */ const char *mpm_strerror(mpm_error_t err_num); +/*! + * \brief Set an error string + * + * \param str String format + * + * \note Support printf format + */ +void mpm_set_str_error(const char *str, ...); + +/*! + * \brief Get an error string, previously set + */ +char *mpm_get_str_error(void); + #endif diff --git a/src/error.c b/src/error.c @@ -16,6 +16,7 @@ #include <error.h> + static u32_t g_error = ERR_NO_ERROR; static const char *g_str_errors[] = { SET_ERR_STR(ERR_NO_ERROR, "No error"), @@ -33,6 +34,7 @@ static const char *g_str_errors[] = { SET_ERR_STR(ERR_BAD_JSON, "JSON file wrongly formatted"), SET_ERR_STR(ERR_BAD_JSON_TYPE, "Unexecpted JSON type") }; +static char g_str_error[ERROR_MAX_LEN] = ""; void set_mpm_error(mpm_error_t err_num) { g_error = err_num; @@ -50,3 +52,15 @@ const char *mpm_strerror(mpm_error_t err_num) { g_error = ERR_NO_ERROR; return ret; } + +void mpm_set_str_error(const char *str, ...) { + va_list ap; + + va_start(ap, str); + vsnprintf(g_str_error, ERROR_MAX_LEN, str, ap); + va_end(ap); +} + +char *mpm_get_str_error(void) { + return g_str_error; +} diff --git a/tests/test_error.c b/tests/test_error.c @@ -14,7 +14,15 @@ TEST(get_mpm_error) { return TEST_SUCCESS; } +TEST(mpm_str_error) { + mpm_set_str_error("My name is %s, and i'm %d years old !", "Tux", 18888); + TEST_ASSERT(strcmp(mpm_get_str_error(), "My name is Tux, and i'm 18888 years old !") == 0, + "String content is wrong"); + return TEST_SUCCESS; +} + void register_test_error(void) { reg_test("error", set_mpm_error); reg_test("error", get_mpm_error); + reg_test("error", mpm_str_error); }