From 63ccb36ef3ecd53a503c0e9fea87f0784802af0f Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Sun, 17 May 2020 23:31:59 +0200 Subject: Codechange: Use std::string for most of the user-settable custom names. --- src/saveload/afterload.cpp | 12 ++++++------ src/saveload/company_sl.cpp | 6 +++--- src/saveload/depot_sl.cpp | 2 +- src/saveload/engine_sl.cpp | 4 ++-- src/saveload/group_sl.cpp | 2 +- src/saveload/saveload.cpp | 4 +++- src/saveload/saveload_internal.h | 2 +- src/saveload/signs_sl.cpp | 2 +- src/saveload/station_sl.cpp | 7 +++---- src/saveload/strings_sl.cpp | 22 ++++++++-------------- src/saveload/town_sl.cpp | 4 ++-- src/saveload/vehicle_sl.cpp | 2 +- src/saveload/waypoint_sl.cpp | 5 ++--- 13 files changed, 34 insertions(+), 40 deletions(-) (limited to 'src/saveload') diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index fa95303f6..cc2a671a2 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -645,20 +645,20 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_84)) { for (Company *c : Company::Iterate()) { c->name = CopyFromOldName(c->name_1); - if (c->name != nullptr) c->name_1 = STR_SV_UNNAMED; + if (!c->name.empty()) c->name_1 = STR_SV_UNNAMED; c->president_name = CopyFromOldName(c->president_name_1); - if (c->president_name != nullptr) c->president_name_1 = SPECSTR_PRESIDENT_NAME; + if (!c->president_name.empty()) c->president_name_1 = SPECSTR_PRESIDENT_NAME; } for (Station *st : Station::Iterate()) { st->name = CopyFromOldName(st->string_id); /* generating new name would be too much work for little effect, use the station name fallback */ - if (st->name != nullptr) st->string_id = STR_SV_STNAME_FALLBACK; + if (!st->name.empty()) st->string_id = STR_SV_STNAME_FALLBACK; } for (Town *t : Town::Iterate()) { t->name = CopyFromOldName(t->townnametype); - if (t->name != nullptr) t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name; + if (!t->name.empty()) t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name; } } @@ -2502,11 +2502,11 @@ bool AfterLoadGame() * highest possible number to get them numbered in the * order they have in the pool. */ for (Waypoint *wp : Waypoint::Iterate()) { - if (wp->name != nullptr) wp->town_cn = UINT16_MAX; + if (!wp->name.empty()) wp->town_cn = UINT16_MAX; } for (Waypoint* wp : Waypoint::Iterate()) { - if (wp->name != nullptr) MakeDefaultName(wp); + if (!wp->name.empty()) MakeDefaultName(wp); } } diff --git a/src/saveload/company_sl.cpp b/src/saveload/company_sl.cpp index 5b9266c7b..6f98c7afa 100644 --- a/src/saveload/company_sl.cpp +++ b/src/saveload/company_sl.cpp @@ -242,11 +242,11 @@ void AfterLoadCompanyStats() static const SaveLoad _company_desc[] = { SLE_VAR(CompanyProperties, name_2, SLE_UINT32), SLE_VAR(CompanyProperties, name_1, SLE_STRINGID), - SLE_CONDSTR(CompanyProperties, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(CompanyProperties, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION), SLE_VAR(CompanyProperties, president_name_1, SLE_STRINGID), SLE_VAR(CompanyProperties, president_name_2, SLE_UINT32), - SLE_CONDSTR(CompanyProperties, president_name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(CompanyProperties, president_name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION), SLE_VAR(CompanyProperties, face, SLE_UINT32), @@ -519,7 +519,7 @@ static void Check_PLYR() } } - if (cprops->name == nullptr && !IsInsideMM(cprops->name_1, SPECSTR_COMPANY_NAME_START, SPECSTR_COMPANY_NAME_LAST + 1) && + if (cprops->name.empty() && !IsInsideMM(cprops->name_1, SPECSTR_COMPANY_NAME_START, SPECSTR_COMPANY_NAME_LAST + 1) && cprops->name_1 != STR_GAME_SAVELOAD_NOT_AVAILABLE && cprops->name_1 != STR_SV_UNNAMED && cprops->name_1 != SPECSTR_ANDCO_NAME && cprops->name_1 != SPECSTR_PRESIDENT_NAME && cprops->name_1 != SPECSTR_SILLY_NAME) { diff --git a/src/saveload/depot_sl.cpp b/src/saveload/depot_sl.cpp index eb4b9384b..bd84faefc 100644 --- a/src/saveload/depot_sl.cpp +++ b/src/saveload/depot_sl.cpp @@ -23,7 +23,7 @@ static const SaveLoad _depot_desc[] = { SLEG_CONDVAR(_town_index, SLE_UINT16, SL_MIN_VERSION, SLV_141), SLE_CONDREF(Depot, town, REF_TOWN, SLV_141, SL_MAX_VERSION), SLE_CONDVAR(Depot, town_cn, SLE_UINT16, SLV_141, SL_MAX_VERSION), - SLE_CONDSTR(Depot, name, SLE_STR, 0, SLV_141, SL_MAX_VERSION), + SLE_CONDSSTR(Depot, name, SLE_STR, SLV_141, SL_MAX_VERSION), SLE_CONDVAR(Depot, build_date, SLE_INT32, SLV_142, SL_MAX_VERSION), SLE_END() }; diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index 44ba6498e..863aabff0 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -39,7 +39,7 @@ static const SaveLoad _engine_desc[] = { SLE_CONDVAR(Engine, company_avail, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104), SLE_CONDVAR(Engine, company_avail, SLE_UINT16, SLV_104, SL_MAX_VERSION), SLE_CONDVAR(Engine, company_hidden, SLE_UINT16, SLV_193, SL_MAX_VERSION), - SLE_CONDSTR(Engine, name, SLE_STR, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(Engine, name, SLE_STR, SLV_84, SL_MAX_VERSION), SLE_CONDNULL(16, SLV_2, SLV_144), // old reserved space @@ -137,7 +137,7 @@ void CopyTempEngineData() e->preview_wait = se->preview_wait; e->company_avail = se->company_avail; e->company_hidden = se->company_hidden; - if (se->name != nullptr) e->name = stredup(se->name); + e->name = se->name; } ResetTempEngineData(); diff --git a/src/saveload/group_sl.cpp b/src/saveload/group_sl.cpp index 33c63ee44..cae313ff8 100644 --- a/src/saveload/group_sl.cpp +++ b/src/saveload/group_sl.cpp @@ -17,7 +17,7 @@ static const SaveLoad _group_desc[] = { SLE_CONDVAR(Group, name, SLE_NAME, SL_MIN_VERSION, SLV_84), - SLE_CONDSTR(Group, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(Group, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION), SLE_CONDNULL(2, SL_MIN_VERSION, SLV_164), // num_vehicle SLE_VAR(Group, owner, SLE_UINT8), SLE_VAR(Group, vehicle_type, SLE_UINT8), diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 7a41f4c40..4da89aa84 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -793,7 +793,7 @@ void WriteValue(void *ptr, VarType conv, int64 val) case SLE_VAR_U32: *(uint32*)ptr = val; break; case SLE_VAR_I64: *(int64 *)ptr = val; break; case SLE_VAR_U64: *(uint64*)ptr = val; break; - case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break; + case SLE_VAR_NAME: *reinterpret_cast(ptr) = CopyFromOldName(val); break; case SLE_VAR_NULL: break; default: NOT_REACHED(); } @@ -1515,6 +1515,8 @@ static bool IsVariableSizeRight(const SaveLoad *sld) case SLE_VAR_I64: case SLE_VAR_U64: return sld->size == sizeof(int64); + case SLE_VAR_NAME: + return sld->size == sizeof(std::string); default: return sld->size == sizeof(void *); } diff --git a/src/saveload/saveload_internal.h b/src/saveload/saveload_internal.h index 8a3f433c2..bca0e87cd 100644 --- a/src/saveload/saveload_internal.h +++ b/src/saveload/saveload_internal.h @@ -17,7 +17,7 @@ void InitializeOldNames(); StringID RemapOldStringID(StringID s); -char *CopyFromOldName(StringID id); +std::string CopyFromOldName(StringID id); void ResetOldNames(); void ResetOldWaypoints(); diff --git a/src/saveload/signs_sl.cpp b/src/saveload/signs_sl.cpp index 0be96e982..99e24435b 100644 --- a/src/saveload/signs_sl.cpp +++ b/src/saveload/signs_sl.cpp @@ -18,7 +18,7 @@ /** Description of a sign within the savegame. */ static const SaveLoad _sign_desc[] = { SLE_CONDVAR(Sign, name, SLE_NAME, SL_MIN_VERSION, SLV_84), - SLE_CONDSTR(Sign, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(Sign, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION), SLE_CONDVAR(Sign, x, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_5), SLE_CONDVAR(Sign, y, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_5), SLE_CONDVAR(Sign, x, SLE_INT32, SLV_5, SL_MAX_VERSION), diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index 1d3612ae3..c4d157c0f 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -62,8 +62,7 @@ void MoveBuoysToWaypoints() TileIndex xy = st->xy; Town *town = st->town; StringID string_id = st->string_id; - char *name = st->name; - st->name = nullptr; + std::string name = st->name; Date build_date = st->build_date; /* TTDPatch could use "buoys with rail station" for rail waypoints */ bool train = st->train_station.tile != INVALID_TILE; @@ -176,7 +175,7 @@ static const SaveLoad _old_station_desc[] = { SLE_CONDNULL(1, SL_MIN_VERSION, SLV_4), ///< alpha_order SLE_VAR(Station, string_id, SLE_STRINGID), - SLE_CONDSTR(Station, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(Station, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION), SLE_CONDVAR(Station, indtype, SLE_UINT8, SLV_103, SL_MAX_VERSION), SLE_CONDVAR(Station, had_vehicle_of_type, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_122), SLE_CONDVAR(Station, had_vehicle_of_type, SLE_UINT8, SLV_122, SL_MAX_VERSION), @@ -389,7 +388,7 @@ static const SaveLoad _base_station_desc[] = { SLE_VAR(BaseStation, xy, SLE_UINT32), SLE_REF(BaseStation, town, REF_TOWN), SLE_VAR(BaseStation, string_id, SLE_STRINGID), - SLE_STR(BaseStation, name, SLE_STR | SLF_ALLOW_CONTROL, 0), + SLE_SSTR(BaseStation, name, SLE_STR | SLF_ALLOW_CONTROL), SLE_VAR(BaseStation, delete_ctr, SLE_UINT8), SLE_VAR(BaseStation, owner, SLE_UINT8), SLE_VAR(BaseStation, facilities, SLE_UINT8), diff --git a/src/saveload/strings_sl.cpp b/src/saveload/strings_sl.cpp index dce8fdedb..dad7eb1ab 100644 --- a/src/saveload/strings_sl.cpp +++ b/src/saveload/strings_sl.cpp @@ -11,6 +11,7 @@ #include "../string_func.h" #include "../strings_func.h" #include "saveload_internal.h" +#include #include "table/strings.h" @@ -56,18 +57,17 @@ char *_old_name_array = nullptr; * @param id the StringID of the custom name to clone. * @return the clones custom name. */ -char *CopyFromOldName(StringID id) +std::string CopyFromOldName(StringID id) { /* Is this name an (old) custom name? */ - if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return nullptr; + if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return std::string(); if (IsSavegameVersionBefore(SLV_37)) { - /* Allow for expansion when converted to UTF-8. */ - char tmp[LEN_OLD_STRINGS * MAX_CHAR_LENGTH]; uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9); const char *strfrom = &_old_name_array[offs]; - char *strto = tmp; + std::ostringstream tmp; + std::ostreambuf_iterator strto(tmp); for (; *strfrom != '\0'; strfrom++) { WChar c = (byte)*strfrom; @@ -84,19 +84,13 @@ char *CopyFromOldName(StringID id) default: break; } - /* Check character will fit into our buffer. */ - if (strto + Utf8CharLen(c) > lastof(tmp)) break; - - strto += Utf8Encode(strto, c); + Utf8Encode(strto, c); } - /* Terminate the new string and copy it back to the name array */ - *strto = '\0'; - - return stredup(tmp); + return tmp.str(); } else { /* Name will already be in UTF-8. */ - return stredup(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]); + return std::string(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]); } } diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index 6fe1439b4..cbf7205d0 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -124,7 +124,7 @@ static const SaveLoad _town_desc[] = { SLE_CONDVAR(Town, townnamegrfid, SLE_UINT32, SLV_66, SL_MAX_VERSION), SLE_VAR(Town, townnametype, SLE_UINT16), SLE_VAR(Town, townnameparts, SLE_UINT32), - SLE_CONDSTR(Town, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(Town, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION), SLE_VAR(Town, flags, SLE_UINT8), SLE_CONDVAR(Town, statues, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104), @@ -167,7 +167,7 @@ static const SaveLoad _town_desc[] = { SLE_CONDARR(Town, goal, SLE_UINT32, NUM_TE, SLV_165, SL_MAX_VERSION), - SLE_CONDSTR(Town, text, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_168, SL_MAX_VERSION), + SLE_CONDSSTR(Town, text, SLE_STR | SLF_ALLOW_CONTROL, SLV_168, SL_MAX_VERSION), SLE_CONDVAR(Town, time_until_rebuild, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_54), SLE_CONDVAR(Town, grow_counter, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_54), diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 3569507c7..321ad53a6 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -585,7 +585,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt) SLE_REF(Vehicle, next, REF_VEHICLE_OLD), SLE_CONDVAR(Vehicle, name, SLE_NAME, SL_MIN_VERSION, SLV_84), - SLE_CONDSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_8), SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, SLV_8, SL_MAX_VERSION), SLE_VAR(Vehicle, owner, SLE_UINT8), diff --git a/src/saveload/waypoint_sl.cpp b/src/saveload/waypoint_sl.cpp index 5336d247b..4117a591e 100644 --- a/src/saveload/waypoint_sl.cpp +++ b/src/saveload/waypoint_sl.cpp @@ -29,7 +29,7 @@ struct OldWaypoint { Town *town; uint16 town_cn; StringID string_id; - char *name; + std::string name; uint8 delete_ctr; Date build_date; uint8 localidx; @@ -172,7 +172,7 @@ static const SaveLoad _old_waypoint_desc[] = { SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, SLV_12, SLV_89), SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, SLV_89, SL_MAX_VERSION), SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, SL_MIN_VERSION, SLV_84), - SLE_CONDSTR(OldWaypoint, name, SLE_STR, 0, SLV_84, SL_MAX_VERSION), + SLE_CONDSSTR(OldWaypoint, name, SLE_STR, SLV_84, SL_MAX_VERSION), SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8), SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, SLV_3, SLV_31), @@ -194,7 +194,6 @@ static void Load_WAYP() while ((index = SlIterateArray()) != -1) { /*C++17: OldWaypoint *wp = &*/ _old_waypoints.emplace_back(); OldWaypoint *wp = &_old_waypoints.back(); - memset(wp, 0, sizeof(*wp)); wp->index = index; SlObject(wp, _old_waypoint_desc); -- cgit v1.2.3-70-g09d2