From 3c689de2d8245652b4592da9ea6a7eb883dff2c5 Mon Sep 17 00:00:00 2001 From: discomfitor Date: Sun, 25 Jul 2010 06:25:06 +0000 Subject: [PATCH] +eina_error_msg_modify to change the error string of a registered error; preserves allocation state of message creation (errors created with static_register() will remain statically allocated) git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@50484 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/include/eina_error.h | 2 +- src/lib/eina_error.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/include/eina_error.h b/src/include/eina_error.h index 7c119d4..47ce2fd 100644 --- a/src/include/eina_error.h +++ b/src/include/eina_error.h @@ -49,7 +49,7 @@ EAPI extern Eina_Error EINA_ERROR_OUT_OF_MEMORY; EAPI Eina_Error eina_error_msg_register(const char *msg) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; EAPI Eina_Error eina_error_msg_static_register(const char *msg) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; - +EAPI Eina_Bool eina_error_msg_modify(Eina_Error error, const char *msg) EINA_ARG_NONNULL(1, 2); EAPI Eina_Error eina_error_get(void); EAPI void eina_error_set(Eina_Error err); EAPI const char * eina_error_msg_get(Eina_Error error) EINA_PURE; diff --git a/src/lib/eina_error.c b/src/lib/eina_error.c index 219cbc0..3130816 100644 --- a/src/lib/eina_error.c +++ b/src/lib/eina_error.c @@ -365,6 +365,46 @@ eina_error_msg_static_register(const char *msg) } /** + * @brief Change the message of an already registered message + * + * @param error The Eina_Error to change the message of + * @param msg The description of the error. This string will be + * duplicated only if the error was registered with @ref eina_error_msg_register + * otherwise it must remain intact for the duration + * @return EINA_TRUE if successful, EINA_FALSE on error + * + * This function modifies the message associated with @p error and changes + * it to @p msg. If the error was previously registered by @ref eina_error_msg_static_register + * then the string will not be duplicated, otherwise the previous message + * will be freed and @p msg copied. + * + * @see eina_error_msg_register() + */ +EAPI Eina_Bool +eina_error_msg_modify(Eina_Error error, const char *msg) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(msg, EINA_FALSE); + if (error < 1) + return EINA_FALSE; + if ((size_t)error > _eina_errors_count) + return EINA_FALSE; + if (_eina_errors[error - 1].string_allocated) + { + const char *tmp; + + if (!(tmp = strdup(msg))) + return EINA_FALSE; + + free((void*)_eina_errors[error - 1].string); + _eina_errors[error - 1].string = tmp; + return EINA_TRUE; + } + + _eina_errors[error - 1].string = msg; + return EINA_TRUE; +} + +/** * @brief Return the description of the given an error number. * * @param error The error number. -- 2.7.4