+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
This commit is contained in:
Mike Blumenkrantz 2010-07-25 06:25:06 +00:00
parent 846e224fd2
commit 37de5b807f
2 changed files with 41 additions and 1 deletions

View File

@ -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;

View File

@ -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.
*