Codechange: migrate from C-style GetString to C++-style GetString

This commit is contained in:
Rubidium 2023-05-19 14:35:53 +02:00 committed by rubidium42
parent 1a179cb297
commit 63d9bb93b8
7 changed files with 31 additions and 67 deletions

View File

@ -356,10 +356,6 @@ CommandCost CheckTileOwnership(TileIndex tile)
*/ */
static void GenerateCompanyName(Company *c) 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->name_1 != STR_SV_UNNAMED) return;
if (c->last_build_coordinate == 0) return; if (c->last_build_coordinate == 0) return;
@ -367,6 +363,7 @@ static void GenerateCompanyName(Company *c)
StringID str; StringID str;
uint32 strp; uint32 strp;
std::string name;
if (t->name.empty() && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) { if (t->name.empty() && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_COMPANY_NAME_START; str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_COMPANY_NAME_START;
strp = t->townnameparts; strp = t->townnameparts;
@ -377,8 +374,8 @@ verify_name:;
if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name; if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
} }
GetString(buffer, str, lastof(buffer)); name = GetString(str);
if (Utf8StringLength(buffer) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name; if (Utf8StringLength(name) >= MAX_LENGTH_COMPANY_NAME_CHARS) goto bad_town_name;
set_name:; set_name:;
c->name_1 = str; c->name_1 = str;
@ -499,18 +496,15 @@ restart:;
/* Reserve space for extra unicode character. We need to do this to be able /* Reserve space for extra unicode character. We need to do this to be able
* to detect too long president name. */ * to detect too long president name. */
char buffer[(MAX_LENGTH_PRESIDENT_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
SetDParam(0, c->index); SetDParam(0, c->index);
GetString(buffer, STR_PRESIDENT_NAME, lastof(buffer)); std::string name = GetString(STR_PRESIDENT_NAME);
if (Utf8StringLength(buffer) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue; if (Utf8StringLength(name) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) continue;
for (const Company *cc : Company::Iterate()) { for (const Company *cc : Company::Iterate()) {
if (c != cc) { 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); SetDParam(0, cc->index);
GetString(buffer2, STR_PRESIDENT_NAME, lastof(buffer2)); std::string other_name = GetString(STR_PRESIDENT_NAME);
if (strcmp(buffer2, buffer) == 0) goto restart; if (name == other_name) goto restart;
} }
} }
return; return;

View File

@ -1717,9 +1717,8 @@ DEF_CONSOLE_CMD(ConCompanies)
for (const Company *c : Company::Iterate()) { for (const Company *c : Company::Iterate()) {
/* Grab the company name */ /* Grab the company name */
char company_name[512];
SetDParam(0, c->index); 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 = ""; const char *password_state = "";
if (c->is_ai) { if (c->is_ai) {
@ -1728,8 +1727,7 @@ DEF_CONSOLE_CMD(ConCompanies)
password_state = _network_company_states[c->index].password.empty() ? "unprotected" : "protected"; password_state = _network_company_states[c->index].password.empty() ? "unprotected" : "protected";
} }
char colour[512]; std::string colour = GetString(STR_COLOUR_DARK_BLUE + _company_colours[c->index]);
GetString(colour, STR_COLOUR_DARK_BLUE + _company_colours[c->index], lastof(colour));
IConsolePrint(CC_INFO, "#:{}({}) Company Name: '{}' Year Founded: {} Money: {} Loan: {} Value: {} (T:{}, R:{}, P:{}, S:{}) {}", IConsolePrint(CC_INFO, "#:{}({}) Company Name: '{}' Year Founded: {} Money: {} Loan: {} Value: {} (T:{}, R:{}, P:{}, S:{}) {}",
c->index + 1, colour, company_name, c->index + 1, colour, company_name,
c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c), c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c),

View File

@ -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) int DrawString(int left, int right, int top, StringID str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
{ {
char buffer[DRAW_STRING_BUFFER]; return DrawString(left, right, top, GetString(str), colour, align, underline, fontsize);
GetString(buffer, str, lastof(buffer));
return DrawString(left, right, top, buffer, colour, align, underline, fontsize);
} }
/** /**
@ -707,9 +705,7 @@ int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
*/ */
int GetStringHeight(StringID str, int maxw) int GetStringHeight(StringID str, int maxw)
{ {
char buffer[DRAW_STRING_BUFFER]; return GetStringHeight(GetString(str), maxw);
GetString(buffer, str, lastof(buffer));
return GetStringHeight(buffer, maxw);
} }
/** /**
@ -720,10 +716,7 @@ int GetStringHeight(StringID str, int maxw)
*/ */
int GetStringLineCount(StringID str, int maxw) int GetStringLineCount(StringID str, int maxw)
{ {
char buffer[DRAW_STRING_BUFFER]; Layouter layout(GetString(str), maxw);
GetString(buffer, str, lastof(buffer));
Layouter layout(buffer, maxw);
return (uint)layout.size(); 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) 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]; return DrawStringMultiLine(left, right, top, bottom, GetString(str), colour, align, underline, fontsize);
GetString(buffer, str, lastof(buffer));
return DrawStringMultiLine(left, right, top, bottom, buffer, colour, align, underline, fontsize);
} }
/** /**
@ -860,10 +851,7 @@ Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
*/ */
Dimension GetStringBoundingBox(StringID strid, FontSize start_fontsize) Dimension GetStringBoundingBox(StringID strid, FontSize start_fontsize)
{ {
char buffer[DRAW_STRING_BUFFER]; return GetStringBoundingBox(GetString(strid), start_fontsize);
GetString(buffer, strid, lastof(buffer));
return GetStringBoundingBox(buffer, start_fontsize);
} }
/** /**

View File

@ -72,7 +72,7 @@ enum CargoSuffixDisplay {
/** Transfer storage of cargo suffix information. */ /** Transfer storage of cargo suffix information. */
struct CargoSuffix { struct CargoSuffix {
CargoSuffixDisplay display; ///< How to display the cargo and text. CargoSuffixDisplay display; ///< How to display the cargo and text.
char text[512]; ///< Cargo suffix text. std::string text; ///< Cargo suffix text.
}; };
extern void GenerateIndustries(); 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) 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; suffix.display = CSD_CARGO_AMOUNT;
if (HasBit(indspec->callback_mask, CBM_IND_CARGO_SUFFIX)) { 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 (GB(callback, 0, 8) == 0xFF) return;
if (callback < 0x400) { if (callback < 0x400) {
StartTextRefStackUsage(indspec->grf_prop.grffile, 6); 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(); StopTextRefStackUsage();
suffix.display = CSD_CARGO_AMOUNT_TEXT; suffix.display = CSD_CARGO_AMOUNT_TEXT;
return; return;
@ -117,14 +117,14 @@ static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind,
} }
if (callback < 0x400) { if (callback < 0x400) {
StartTextRefStackUsage(indspec->grf_prop.grffile, 6); 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(); StopTextRefStackUsage();
suffix.display = CSD_CARGO_AMOUNT_TEXT; suffix.display = CSD_CARGO_AMOUNT_TEXT;
return; return;
} }
if (callback >= 0x800 && callback < 0xC00) { if (callback >= 0x800 && callback < 0xC00) {
StartTextRefStackUsage(indspec->grf_prop.grffile, 6); 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(); StopTextRefStackUsage();
suffix.display = CSD_CARGO_TEXT; suffix.display = CSD_CARGO_TEXT;
return; return;
@ -194,15 +194,7 @@ std::array<IndustryType, NUM_INDUSTRYTYPES> _sorted_industry_types; ///< Industr
/** Sort industry types by their name. */ /** Sort industry types by their name. */
static bool IndustryTypeNameSorter(const IndustryType &a, const IndustryType &b) static bool IndustryTypeNameSorter(const IndustryType &a, const IndustryType &b)
{ {
static char industry_name[2][64]; int r = StrNaturalCompare(GetString(GetIndustrySpec(a)->name), GetString(GetIndustrySpec(b)->name)); // Sort by name (natural sorting).
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).
/* If the names are equal, sort by industry type. */ /* If the names are equal, sort by industry type. */
return (r != 0) ? r < 0 : (a < b); 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 MakeCargoListString(const CargoID *cargolist, const CargoSuffix *cargo_suffix, int cargolistlen, StringID prefixstr) const
{ {
std::string cargostring; std::string cargostring;
char buf[1024];
int numcargo = 0; int numcargo = 0;
int firstcargo = -1; int firstcargo = -1;
@ -363,20 +354,17 @@ class BuildIndustryWindow : public Window {
} }
SetDParam(0, CargoSpec::Get(cargolist[j])->name); SetDParam(0, CargoSpec::Get(cargolist[j])->name);
SetDParamStr(1, cargo_suffix[j].text); SetDParamStr(1, cargo_suffix[j].text);
GetString(buf, STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION, lastof(buf)); cargostring += GetString(STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION);
cargostring += buf;
} }
if (numcargo > 0) { if (numcargo > 0) {
SetDParam(0, CargoSpec::Get(cargolist[firstcargo])->name); SetDParam(0, CargoSpec::Get(cargolist[firstcargo])->name);
SetDParamStr(1, cargo_suffix[firstcargo].text); SetDParamStr(1, cargo_suffix[firstcargo].text);
GetString(buf, prefixstr, lastof(buf)); cargostring = GetString(prefixstr) + cargostring;
cargostring = std::string(buf) + cargostring;
} else { } else {
SetDParam(0, STR_JUST_NOTHING); SetDParam(0, STR_JUST_NOTHING);
SetDParamStr(1, ""); SetDParamStr(1, "");
GetString(buf, prefixstr, lastof(buf)); cargostring = GetString(prefixstr);
cargostring = std::string(buf);
} }
return cargostring; return cargostring;
@ -1547,7 +1535,7 @@ protected:
for (byte j = 0; j < lengthof(i->produced_cargo); j++) { for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue; 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<IndustryDirectoryWindow::SorterType>(this->industries.SortType())) { switch (static_cast<IndustryDirectoryWindow::SorterType>(this->industries.SortType())) {

View File

@ -250,8 +250,6 @@ const std::string Text::GetDecodedText()
{ {
const std::string &encoded_text = this->GetEncodedText(); const std::string &encoded_text = this->GetEncodedText();
static char buf[1024];
::SetDParamStr(0, encoded_text); ::SetDParamStr(0, encoded_text);
::GetString(buf, STR_JUST_RAW_STRING, lastof(buf)); return ::GetString(STR_JUST_RAW_STRING);
return buf;
} }

View File

@ -155,8 +155,8 @@ void CopyOutDParam(uint64 *dst, int offs, int num)
*/ */
void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num) void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num)
{ {
char buf[DRAW_STRING_BUFFER]; /* Just get the string to extract the type information. */
GetString(buf, string, lastof(buf)); GetString(string);
MemCpyT(dst, _global_string_params.GetPointerToOffset(0), num); MemCpyT(dst, _global_string_params.GetPointerToOffset(0), num);
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {

View File

@ -1449,17 +1449,15 @@ void ViewportSign::UpdatePosition(int center, int top, StringID str, StringID st
this->top = top; this->top = top;
char buffer[DRAW_STRING_BUFFER]; std::string name = GetString(str);
this->width_normal = WidgetDimensions::scaled.fullbevel.left + Align(GetStringBoundingBox(name).width, 2) + WidgetDimensions::scaled.fullbevel.right;
GetString(buffer, str, lastof(buffer));
this->width_normal = WidgetDimensions::scaled.fullbevel.left + Align(GetStringBoundingBox(buffer).width, 2) + WidgetDimensions::scaled.fullbevel.right;
this->center = center; this->center = center;
/* zoomed out version */ /* zoomed out version */
if (str_small != STR_NULL) { 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(); this->MarkDirty();
} }