Add format-style versions of SlError and SlErrorCorrupt.

This commit is contained in:
Jonathan G Rennison
2015-07-31 22:23:36 +01:00
parent 17e8693e62
commit 4508cfbf93
4 changed files with 53 additions and 13 deletions

View File

@@ -534,17 +534,22 @@ static void SlNullPointers()
* @note This function does never return as it throws an exception to
* break out of all the saveload code.
*/
void NORETURN SlError(StringID string, const char *extra_msg)
void NORETURN SlError(StringID string, const char *extra_msg, bool already_malloced)
{
char *str = NULL;
if (extra_msg != NULL) {
str = already_malloced ? const_cast<char *>(extra_msg) : stredup(extra_msg);
}
/* Distinguish between loading into _load_check_data vs. normal save/load. */
if (_sl.action == SLA_LOAD_CHECK) {
_load_check_data.error = string;
free(_load_check_data.error_data);
_load_check_data.error_data = (extra_msg == NULL) ? NULL : stredup(extra_msg);
_load_check_data.error_data = str;
} else {
_sl.error_str = string;
free(_sl.extra_msg);
_sl.extra_msg = (extra_msg == NULL) ? NULL : stredup(extra_msg);
_sl.extra_msg = str;
}
/* We have to NULL all pointers here; we might be in a state where
@@ -555,6 +560,18 @@ void NORETURN SlError(StringID string, const char *extra_msg)
throw std::exception();
}
/**
* As SlError, except that it takes a format string and additional parameters
*/
void CDECL NORETURN SlErrorFmt(StringID string, const char *msg, ...)
{
va_list va;
va_start(va, msg);
char *str = str_vfmt(msg, va);
va_end(va);
SlError(string, str, true);
}
/**
* Error handler for corrupt savegames. Sets everything up to show the
* error message and to clean up the mess of a partial savegame load.
@@ -562,9 +579,21 @@ void NORETURN SlError(StringID string, const char *extra_msg)
* @note This function does never return as it throws an exception to
* break out of all the saveload code.
*/
void NORETURN SlErrorCorrupt(const char *msg)
void NORETURN SlErrorCorrupt(const char *msg, bool already_malloced)
{
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg, already_malloced);
}
/**
* As SlErrorCorruptFmt, except that it takes a format string and additional parameters
*/
void CDECL NORETURN SlErrorCorruptFmt(const char *msg, ...)
{
va_list va;
va_start(va, msg);
char *str = str_vfmt(msg, va);
va_end(va);
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, str, true);
}