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 * @note This function does never return as it throws an exception to
* break out of all the saveload code. * 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. */ /* Distinguish between loading into _load_check_data vs. normal save/load. */
if (_sl.action == SLA_LOAD_CHECK) { if (_sl.action == SLA_LOAD_CHECK) {
_load_check_data.error = string; _load_check_data.error = string;
free(_load_check_data.error_data); 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 { } else {
_sl.error_str = string; _sl.error_str = string;
free(_sl.extra_msg); 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 /* 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(); 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 handler for corrupt savegames. Sets everything up to show the
* error message and to clean up the mess of a partial savegame load. * 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 * @note This function does never return as it throws an exception to
* break out of all the saveload code. * 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);
} }

View File

@@ -16,6 +16,8 @@
#include "../strings_type.h" #include "../strings_type.h"
#include "extended_ver_sl.h" #include "extended_ver_sl.h"
#include <stdarg.h>
/** Save or load result codes. */ /** Save or load result codes. */
enum SaveOrLoadResult { enum SaveOrLoadResult {
SL_OK = 0, ///< completed successfully SL_OK = 0, ///< completed successfully
@@ -570,8 +572,10 @@ void SlGlobList(const SaveLoadGlobVarList *sldg);
void SlArray(void *array, size_t length, VarType conv); void SlArray(void *array, size_t length, VarType conv);
void SlObject(void *object, const SaveLoad *sld); void SlObject(void *object, const SaveLoad *sld);
bool SlObjectMember(void *object, const SaveLoad *sld); bool SlObjectMember(void *object, const SaveLoad *sld);
void NORETURN SlError(StringID string, const char *extra_msg = NULL); void NORETURN SlError(StringID string, const char *extra_msg = NULL, bool already_malloced = false);
void NORETURN SlErrorCorrupt(const char *msg); void NORETURN SlErrorCorrupt(const char *msg, bool already_malloced = false);
void CDECL NORETURN SlErrorFmt(StringID string, const char *msg, ...) WARN_FORMAT(2, 3);
void CDECL NORETURN SlErrorCorruptFmt(const char *msg, ...) WARN_FORMAT(1, 2);
bool SaveloadCrashWithMissingNewGRFs(); bool SaveloadCrashWithMissingNewGRFs();

View File

@@ -131,6 +131,16 @@ char *stredup(const char *s, const char *last)
return tmp; return tmp;
} }
char *str_vfmt(const char *str, va_list va)
{
char buf[4096];
int len = vseprintf(buf, lastof(buf), str, va);
char *p = MallocT<char>(len + 1);
memcpy(p, buf, len + 1);
return p;
}
/** /**
* Format, "printf", into a newly allocated string. * Format, "printf", into a newly allocated string.
* @param str The formatting string. * @param str The formatting string.
@@ -138,15 +148,11 @@ char *stredup(const char *s, const char *last)
*/ */
char *CDECL str_fmt(const char *str, ...) char *CDECL str_fmt(const char *str, ...)
{ {
char buf[4096];
va_list va; va_list va;
va_start(va, str); va_start(va, str);
int len = vseprintf(buf, lastof(buf), str, va); char *output = str_vfmt(str, va);
va_end(va); va_end(va);
char *p = MallocT<char>(len + 1); return output;
memcpy(p, buf, len + 1);
return p;
} }
/** /**

View File

@@ -39,6 +39,7 @@ int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FO
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap); int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap);
char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2); char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
char *str_vfmt(const char *str, va_list ap);
void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK); void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void ValidateString(const char *str); void ValidateString(const char *str);