Codechange: use std::string instead of stredup for saveload error messages

This commit is contained in:
Rubidium 2023-04-30 21:46:49 +02:00 committed by rubidium42
parent 8665404fe0
commit 1f3b7e2efd
5 changed files with 13 additions and 16 deletions

View File

@ -32,7 +32,7 @@ typedef SmallMap<uint, CompanyProperties *> CompanyPropertiesMap;
struct LoadCheckData {
bool checkable; ///< True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
StringID error; ///< Error message from loading. INVALID_STRING_ID if no error.
char *error_data; ///< Data to pass to SetDParamStr when displaying #error.
std::string error_msg; ///< Data to pass to SetDParamStr when displaying #error.
uint32 map_size_x, map_size_y;
TimerGameCalendar::Date current_date;
@ -47,7 +47,7 @@ struct LoadCheckData {
struct LoggedAction *gamelog_action; ///< Gamelog actions
uint gamelog_actions; ///< Number of gamelog actions
LoadCheckData() : error_data(nullptr), grfconfig(nullptr),
LoadCheckData() : grfconfig(nullptr),
grf_compatibility(GLC_NOT_FOUND), gamelog_action(nullptr), gamelog_actions(0)
{
this->Clear();

View File

@ -49,8 +49,7 @@ void LoadCheckData::Clear()
{
this->checkable = false;
this->error = INVALID_STRING_ID;
free(this->error_data);
this->error_data = nullptr;
this->error_msg.clear();
this->map_size_x = this->map_size_y = 256; // Default for old savegames which do not store mapsize.
this->current_date = 0;
@ -500,7 +499,7 @@ public:
tr.top += FONT_HEIGHT_NORMAL;
} else if (_load_check_data.error != INVALID_STRING_ID) {
/* Incompatible / broken savegame */
SetDParamStr(0, _load_check_data.error_data);
SetDParamStr(0, _load_check_data.error_msg);
tr.top = DrawStringMultiLine(tr, _load_check_data.error, TC_RED);
} else {
/* Mapsize */

View File

@ -623,7 +623,7 @@ int openttd_main(int argc, char *argv[])
if (_load_check_data.HasErrors()) {
InitializeLanguagePacks(); // A language pack is needed for GetString()
char buf[256];
SetDParamStr(0, _load_check_data.error_data);
SetDParamStr(0, _load_check_data.error_msg);
GetString(buf, _load_check_data.error, lastof(buf));
fprintf(stderr, "%s\n", buf);
}

View File

@ -208,7 +208,7 @@ struct SaveLoadParams {
LoadFilter *lf; ///< Filter to read the savegame from.
StringID error_str; ///< the translatable error message to show
char *extra_msg; ///< the error message
std::string extra_msg; ///< the error message
bool saveinprogress; ///< Whether there is currently a save in progress.
};
@ -330,17 +330,15 @@ 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 std::string &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 == nullptr) ? nullptr : stredup(extra_msg);
_load_check_data.error_msg = extra_msg;
} else {
_sl.error_str = string;
free(_sl.extra_msg);
_sl.extra_msg = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
_sl.extra_msg = extra_msg;
}
/* We have to nullptr all pointers here; we might be in a state where
@ -362,7 +360,7 @@ 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 std::string &msg)
{
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
}

View File

@ -13,8 +13,8 @@
#include "../3rdparty/fmt/format.h"
#include "../strings_type.h"
void NORETURN SlError(StringID string, const char *extra_msg = nullptr);
void NORETURN SlErrorCorrupt(const char *msg);
void NORETURN SlError(StringID string, const std::string &extra_msg = {});
void NORETURN SlErrorCorrupt(const std::string &msg);
/**
* Issue an SlErrorCorrupt with a format string.
@ -28,7 +28,7 @@ void NORETURN SlErrorCorrupt(const char *msg);
template <typename T, typename ... Args>
static inline void NORETURN SlErrorCorruptFmt(const T &format, Args&&... fmt_args)
{
SlErrorCorrupt(fmt::format(format, fmt_args...).c_str());
SlErrorCorrupt(fmt::format(format, fmt_args...));
}
#endif /* SAVELOAD_ERROR_HPP */