From 37de5b807f09115c1a6067a4e8fc03c180278359 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz 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) SVN revision: 50484 --- legacy/eina/src/include/eina_error.h | 2 +- legacy/eina/src/lib/eina_error.c | 40 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/legacy/eina/src/include/eina_error.h b/legacy/eina/src/include/eina_error.h index 7c119d4c08..47ce2fdf0f 100644 --- a/legacy/eina/src/include/eina_error.h +++ b/legacy/eina/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/legacy/eina/src/lib/eina_error.c b/legacy/eina/src/lib/eina_error.c index 219cbc0d10..313081602b 100644 --- a/legacy/eina/src/lib/eina_error.c +++ b/legacy/eina/src/lib/eina_error.c @@ -364,6 +364,46 @@ eina_error_msg_static_register(const char *msg) return _eina_errors_count; /* identifier = index + 1 (== _count). */ } +/** + * @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. *