From 63d9bb93b8ea9063798c546a9fc8b9f8d0d356e8 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Fri, 19 May 2023 14:35:53 +0200 Subject: [PATCH] Codechange: migrate from C-style GetString to C++-style GetString --- src/company_cmd.cpp | 20 +++++++------------- src/console_cmds.cpp | 6 ++---- src/gfx.cpp | 22 +++++----------------- src/industry_gui.cpp | 32 ++++++++++---------------------- src/script/api/script_text.cpp | 4 +--- src/strings.cpp | 4 ++-- src/viewport.cpp | 10 ++++------ 7 files changed, 31 insertions(+), 67 deletions(-) diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index b1bf880859..79bd6153e4 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -356,10 +356,6 @@ CommandCost CheckTileOwnership(TileIndex tile) */ static void GenerateCompanyName(Company *c) { - /* Reserve space for extra unicode character. We need to do this to be able - * to detect too long company name. */ - char buffer[(MAX_LENGTH_COMPANY_NAME_CHARS + 1) * MAX_CHAR_LENGTH]; - if (c->name_1 != STR_SV_UNNAMED) return; if (c->last_build_coordinate == 0) return; @@ -367,6 +363,7 @@ static void GenerateCompanyName(Company *c) StringID str; uint32 strp; + std::string name; if (t->name.empty() && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) { str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_COMPANY_NAME_START; strp = t->townnameparts; @@ -377,8 +374,8 @@ verify_name:; if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name; } - GetString(buffer, str, lastof(buffer)); - if (Utf8StringLength(buffer) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name; + name = GetString(str); + if (Utf8StringLength(name) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name; set_name:; c->name_1 = str; @@ -499,18 +496,15 @@ restart:; /* Reserve space for extra unicode character. We need to do this to be able * to detect too long president name. */ - char buffer[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH]; SetDParam(0, c->index); - GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer)); - if (Utf8StringLength(buffer) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue; + std::string name = GetString(STR_PRESIDENT_NAME); + if (Utf8StringLength(name) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue; for (const Company *cc : Company::Iterate()) { if (c != cc) { - /* Reserve extra space so even overlength president names can be compared. */ - char buffer2[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH]; SetDParam(0, cc->index); - GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2)); - if (strcmp(buffer2, buffer) == 0) goto restart; + std::string other_name = GetString(STR_PRESIDENT_NAME); + if (name == other_name) goto restart; } } return; diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 0980ffcfce..06777da6cc 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1717,9 +1717,8 @@ DEF_CONSOLE_CMD(ConCompanies) for (const Company *c : Company::Iterate()) { /* Grab the company name */ - char company_name[512]; SetDParam(0, c->index); - GetString(company_name, STR_COMPANY_NAME, lastof(company_name)); + std::string company_name = GetString(STR_COMPANY_NAME); const char *password_state = ""; if (c->is_ai) { @@ -1728,8 +1727,7 @@ DEF_CONSOLE_CMD(ConCompanies) password_state = _network_company_states[c->index].password.empty() ? "unprotected" : "protected"; } - char colour[512]; - GetString(colour, STR_COLOUR_DARK_BLUE + _company_colours[c->index], lastof(colour)); + std::string colour = GetString(STR_COLOUR_DARK_BLUE + _company_colours[c->index]); IConsolePrint(CC_INFO, "#:{}({}) Company Name: '{}' Year Founded: {} Money: {} Loan: {} Value: {} (T:{}, R:{}, P:{}, S:{}) {}", c->index + 1, colour, company_name, c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c), diff --git a/src/gfx.cpp b/src/gfx.cpp index d2f826366e..4d268e3dbe 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -682,9 +682,7 @@ int DrawString(int left, int right, int top, std::string_view str, TextColour co */ int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize) { - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, str, lastof(buffer)); - return DrawString(left, right, top, buffer, colour, align, underline, fontsize); + return DrawString(left, right, top, GetString(str), colour, align, underline, fontsize); } /** @@ -707,9 +705,7 @@ int GetStringHeight(std::string_view str, int maxw, FontSize fontsize) */ int GetStringHeight(StringID str, int maxw) { - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, str, lastof(buffer)); - return GetStringHeight(buffer, maxw); + return GetStringHeight(GetString(str), maxw); } /** @@ -720,10 +716,7 @@ int GetStringHeight(StringID str, int maxw) */ int GetStringLineCount(StringID str, int maxw) { - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, str, lastof(buffer)); - - Layouter layout(buffer, maxw); + Layouter layout(GetString(str), maxw); return (uint)layout.size(); } @@ -831,9 +824,7 @@ int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_vi */ int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize) { - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, str, lastof(buffer)); - return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize); + return DrawStringMultiLine(left, right, top, bottom, GetString(str), colour, align, underline, fontsize); } /** @@ -860,10 +851,7 @@ Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize) */ Dimension GetStringBoundingBox(StringID strid, FontSize start_fontsize) { - char buffer[DRAW_STRING_BUFFER]; - - GetString(buffer, strid, lastof(buffer)); - return GetStringBoundingBox(buffer, start_fontsize); + return GetStringBoundingBox(GetString(strid), start_fontsize); } /** diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 073ea88b14..52110634ab 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -72,7 +72,7 @@ enum CargoSuffixDisplay { /** Transfer storage of cargo suffix information. */ struct CargoSuffix { CargoSuffixDisplay display; ///< How to display the cargo and text. - char text[512]; ///< Cargo suffix text. + std::string text; ///< Cargo suffix text. }; extern void GenerateIndustries(); @@ -89,7 +89,7 @@ static void ShowIndustryCargoesWindow(IndustryType id); */ static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, CargoSuffix &suffix) { - suffix.text[0] = '\0'; + suffix.text.clear(); suffix.display = CSD_CARGO_AMOUNT; if (HasBit(indspec->callback_mask, CBM_IND_CARGO_SUFFIX)) { @@ -101,7 +101,7 @@ static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, if (GB(callback, 0, 8) == 0xFF) return; if (callback < 0x400) { StartTextRefStackUsage(indspec->grf_prop.grffile, 6); - GetString(suffix.text, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback), lastof(suffix.text)); + suffix.text = GetString(GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback)); StopTextRefStackUsage(); suffix.display = CSD_CARGO_AMOUNT_TEXT; return; @@ -117,14 +117,14 @@ static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, } if (callback < 0x400) { StartTextRefStackUsage(indspec->grf_prop.grffile, 6); - GetString(suffix.text, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback), lastof(suffix.text)); + suffix.text = GetString(GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback)); StopTextRefStackUsage(); suffix.display = CSD_CARGO_AMOUNT_TEXT; return; } if (callback >= 0x800 && callback < 0xC00) { StartTextRefStackUsage(indspec->grf_prop.grffile, 6); - GetString(suffix.text, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 - 0x800 + callback), lastof(suffix.text)); + suffix.text = GetString(GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 - 0x800 + callback)); StopTextRefStackUsage(); suffix.display = CSD_CARGO_TEXT; return; @@ -194,15 +194,7 @@ std::array _sorted_industry_types; ///< Industr /** Sort industry types by their name. */ static bool IndustryTypeNameSorter(const IndustryType &a, const IndustryType &b) { - static char industry_name[2][64]; - - const IndustrySpec *indsp1 = GetIndustrySpec(a); - GetString(industry_name[0], indsp1->name, lastof(industry_name[0])); - - const IndustrySpec *indsp2 = GetIndustrySpec(b); - GetString(industry_name[1], indsp2->name, lastof(industry_name[1])); - - int r = StrNaturalCompare(industry_name[0], industry_name[1]); // Sort by name (natural sorting). + int r = StrNaturalCompare(GetString(GetIndustrySpec(a)->name), GetString(GetIndustrySpec(b)->name)); // Sort by name (natural sorting). /* If the names are equal, sort by industry type. */ return (r != 0) ? r < 0 : (a < b); @@ -350,7 +342,6 @@ class BuildIndustryWindow : public Window { std::string MakeCargoListString(const CargoID *cargolist, const CargoSuffix *cargo_suffix, int cargolistlen, StringID prefixstr) const { std::string cargostring; - char buf[1024]; int numcargo = 0; int firstcargo = -1; @@ -363,20 +354,17 @@ class BuildIndustryWindow : public Window { } SetDParam(0, CargoSpec::Get(cargolist[j])->name); SetDParamStr(1, cargo_suffix[j].text); - GetString(buf, STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION, lastof(buf)); - cargostring += buf; + cargostring += GetString(STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION); } if (numcargo > 0) { SetDParam(0, CargoSpec::Get(cargolist[firstcargo])->name); SetDParamStr(1, cargo_suffix[firstcargo].text); - GetString(buf, prefixstr, lastof(buf)); - cargostring = std::string(buf) + cargostring; + cargostring = GetString(prefixstr) + cargostring; } else { SetDParam(0, STR_JUST_NOTHING); SetDParamStr(1, ""); - GetString(buf, prefixstr, lastof(buf)); - cargostring = std::string(buf); + cargostring = GetString(prefixstr); } return cargostring; @@ -1547,7 +1535,7 @@ protected: for (byte j = 0; j < lengthof(i->produced_cargo); j++) { if (i->produced_cargo[j] == CT_INVALID) continue; - cargos.push_back({ i->produced_cargo[j], i->last_month_production[j], cargo_suffix[j].text, ToPercent8(i->last_month_pct_transported[j]) }); + cargos.push_back({ i->produced_cargo[j], i->last_month_production[j], cargo_suffix[j].text.c_str(), ToPercent8(i->last_month_pct_transported[j]) }); } switch (static_cast(this->industries.SortType())) { diff --git a/src/script/api/script_text.cpp b/src/script/api/script_text.cpp index b6f46e9f59..3d2a95650c 100644 --- a/src/script/api/script_text.cpp +++ b/src/script/api/script_text.cpp @@ -250,8 +250,6 @@ const std::string Text::GetDecodedText() { const std::string &encoded_text = this->GetEncodedText(); - static char buf[1024]; ::SetDParamStr(0, encoded_text); - ::GetString(buf, STR_JUST_RAW_STRING, lastof(buf)); - return buf; + return ::GetString(STR_JUST_RAW_STRING); } diff --git a/src/strings.cpp b/src/strings.cpp index 0e13ef13bc..ed8d1bfc94 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -155,8 +155,8 @@ void CopyOutDParam(uint64 *dst, int offs, int num) */ void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num) { - char buf[DRAW_STRING_BUFFER]; - GetString(buf, string, lastof(buf)); + /* Just get the string to extract the type information. */ + GetString(string); MemCpyT(dst, _global_string_params.GetPointerToOffset(0), num); for (int i = 0; i < num; i++) { diff --git a/src/viewport.cpp b/src/viewport.cpp index 185f8bf082..8e5e7ead81 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -1449,17 +1449,15 @@ void ViewportSign::UpdatePosition(int center, int top, StringID str, StringID st this->top = top; - char buffer[DRAW_STRING_BUFFER]; - - GetString(buffer, str, lastof(buffer)); - this->width_normal = WidgetDimensions::scaled.fullbevel.left + Align(GetStringBoundingBox(buffer).width, 2) + WidgetDimensions::scaled.fullbevel.right; + std::string name = GetString(str); + this->width_normal = WidgetDimensions::scaled.fullbevel.left + Align(GetStringBoundingBox(name).width, 2) + WidgetDimensions::scaled.fullbevel.right; this->center = center; /* zoomed out version */ if (str_small != STR_NULL) { - GetString(buffer, str_small, lastof(buffer)); + name = GetString(str_small); } - this->width_small = WidgetDimensions::scaled.fullbevel.left + Align(GetStringBoundingBox(buffer, FS_SMALL).width, 2) + WidgetDimensions::scaled.fullbevel.right; + this->width_small = WidgetDimensions::scaled.fullbevel.left + Align(GetStringBoundingBox(name, FS_SMALL).width, 2) + WidgetDimensions::scaled.fullbevel.right; this->MarkDirty(); }