1
0
Fork 0

Codechange: convert C-style GetTownName API to std::string returning API

pull/10951/head
Rubidium 2023-06-05 00:13:35 +02:00 committed by rubidium42
parent 14915526ad
commit 2dd2b698d2
4 changed files with 26 additions and 42 deletions

View File

@ -207,9 +207,7 @@ void Town::InitializeLayout(TownLayout layout)
void Town::FillCachedName() const void Town::FillCachedName() const
{ {
char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH]; this->cached_name.assign(GetTownName(this));
char *end = GetTownName(buf, this, lastof(buf));
this->cached_name.assign(buf, end);
} }
/** /**

View File

@ -1159,8 +1159,7 @@ public:
if (!this->townnamevalid) { if (!this->townnamevalid) {
this->townname_editbox.text.DeleteAll(); this->townname_editbox.text.DeleteAll();
} else { } else {
GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]); this->townname_editbox.text.Assign(GetTownName(&this->params, this->townnameparts));
this->townname_editbox.text.UpdateSize();
} }
UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX); UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
@ -1198,9 +1197,8 @@ public:
name = this->townname_editbox.text.buf; name = this->townname_editbox.text.buf;
} else { } else {
/* If user changed the name, send it */ /* If user changed the name, send it */
char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH]; std::string original_name = GetTownName(&this->params, this->townnameparts);
GetTownName(buf, &this->params, this->townnameparts, lastof(buf)); if (original_name != this->townname_editbox.text.buf) name = this->townname_editbox.text.buf;
if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
} }
bool success = Command<CMD_FOUND_TOWN>::Post(errstr, cc, bool success = Command<CMD_FOUND_TOWN>::Post(errstr, cc,

View File

@ -58,18 +58,18 @@ static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint3
} }
/** /**
* Fills buffer with specified town name * Get the town name for the given parameters and parts.
* @param buff buffer start * @param par Town name parameters.
* @param par town name parameters * @param townnameparts 'Encoded' town name.
* @param townnameparts 'encoded' town name * @return The town name.
* @param last end of buffer
* @return pointer to terminating '\0'
*/ */
char *GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last) std::string GetTownName(const TownNameParams *par, uint32 townnameparts)
{ {
StringBuilder builder(&buff, last); char buffer[DRAW_STRING_BUFFER];
char *state = buffer;
StringBuilder builder(&state, lastof(buffer));
GetTownName(builder, par, townnameparts); GetTownName(builder, par, townnameparts);
return builder.GetEnd(); return std::string(buffer, builder.GetEnd());
} }
/** /**
@ -84,17 +84,14 @@ void GetTownName(StringBuilder &builder, const Town *t)
} }
/** /**
* Fills buffer with town's name * Get the name of the given town.
* @param buff buffer start * @param t The town to get the name for.
* @param t we want to get name of this town * @return The town name.
* @param last end of buffer
* @return pointer to terminating '\0'
*/ */
char *GetTownName(char *buff, const Town *t, const char *last) std::string GetTownName(const Town *t)
{ {
StringBuilder builder(&buff, last); TownNameParams par(t);
GetTownName(builder, t); return GetTownName(&par, t->townnameparts);
return builder.GetEnd();
} }
@ -107,28 +104,19 @@ char *GetTownName(char *buff, const Town *t, const char *last)
*/ */
bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names) bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names)
{ {
/* reserve space for extra unicode character and terminating '\0' */ std::string name = GetTownName(par, r);
char buf1[(MAX_LENGTH_TOWN_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
char buf2[(MAX_LENGTH_TOWN_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
GetTownName(buf1, par, r, lastof(buf1));
/* Check size and width */ /* Check size and width */
if (Utf8StringLength(buf1) >= MAX_LENGTH_TOWN_NAME_CHARS) return false; if (Utf8StringLength(name) >= MAX_LENGTH_TOWN_NAME_CHARS) return false;
if (town_names != nullptr) { if (town_names != nullptr) {
if (town_names->find(buf1) != town_names->end()) return false; if (town_names->find(name) != town_names->end()) return false;
town_names->insert(buf1); town_names->insert(name);
} else { } else {
for (const Town *t : Town::Iterate()) { for (const Town *t : Town::Iterate()) {
/* We can't just compare the numbers since /* We can't just compare the numbers since
* several numbers may map to a single name. */ * several numbers may map to a single name. */
const char *buf = t->name.empty() ? nullptr : t->name.c_str(); if (name == (t->name.empty() ? GetTownName(t) : t->name)) return false;
if (buf == nullptr) {
GetTownName(buf2, t, lastof(buf2));
buf = buf2;
}
if (strcmp(buf1, buf) == 0) return false;
} }
} }

View File

@ -13,8 +13,8 @@
#include "core/random_func.hpp" #include "core/random_func.hpp"
#include "townname_type.h" #include "townname_type.h"
char *GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last); std::string GetTownName(const TownNameParams *par, uint32 townnameparts);
char *GetTownName(char *buff, const Town *t, const char *last); std::string GetTownName(const Town *t);
bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names = nullptr); bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names = nullptr);
bool GenerateTownName(Randomizer &randomizer, uint32 *townnameparts, TownNames *town_names = nullptr); bool GenerateTownName(Randomizer &randomizer, uint32 *townnameparts, TownNames *town_names = nullptr);