diff options
author | rubidium42 <rubidium@openttd.org> | 2021-05-30 13:02:44 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-06-10 20:09:44 +0200 |
commit | e3717ae903cb770ae2ebc4abe45fa0cc77326409 (patch) | |
tree | 2a9b087c8fce31b51de3a7fe987be622323f963e | |
parent | df181bb641a75e9fae557c683b790cfba961ec5a (diff) | |
download | openttd-e3717ae903cb770ae2ebc4abe45fa0cc77326409.tar.xz |
Codechange: [ContentInfo] Use std::string instead of string buffers
-rw-r--r-- | src/console_cmds.cpp | 4 | ||||
-rw-r--r-- | src/network/core/config.h | 5 | ||||
-rw-r--r-- | src/network/core/tcp_content_type.h | 10 | ||||
-rw-r--r-- | src/network/network_content.cpp | 14 | ||||
-rw-r--r-- | src/network/network_content_gui.cpp | 20 | ||||
-rw-r--r-- | src/newgrf_gui.cpp | 2 |
6 files changed, 30 insertions, 25 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 3c5ca5d24..7a756a657 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1812,7 +1812,7 @@ static void OutputContentState(const ContentInfo *const ci) char buf[sizeof(ci->md5sum) * 2 + 1]; md5sumToString(buf, lastof(buf), ci->md5sum); - IConsolePrintF(state_to_colour[ci->state], "%d, %s, %s, %s, %08X, %s", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, buf); + IConsolePrintF(state_to_colour[ci->state], "%d, %s, %s, %s, %08X, %s", ci->id, types[ci->type - 1], states[ci->state], ci->name.c_str(), ci->unique_id, buf); } DEF_CONSOLE_CMD(ConContent) @@ -1882,7 +1882,7 @@ DEF_CONSOLE_CMD(ConContent) if (strcasecmp(argv[1], "state") == 0) { IConsolePrintF(CC_WHITE, "id, type, state, name"); for (ConstContentIterator iter = _network_content_client.Begin(); iter != _network_content_client.End(); iter++) { - if (argc > 2 && strcasestr((*iter)->name, argv[2]) == nullptr) continue; + if (argc > 2 && strcasestr((*iter)->name.c_str(), argv[2]) == nullptr) continue; OutputContentState(*iter); } return true; diff --git a/src/network/core/config.h b/src/network/core/config.h index ce614188d..6d6038ec7 100644 --- a/src/network/core/config.h +++ b/src/network/core/config.h @@ -65,6 +65,11 @@ static const uint NETWORK_CLIENT_NAME_LENGTH = 25; ///< The maxim static const uint NETWORK_RCONCOMMAND_LENGTH = 500; ///< The maximum length of a rconsole command, in bytes including '\0' static const uint NETWORK_GAMESCRIPT_JSON_LENGTH = COMPAT_MTU-3; ///< The maximum length of a gamescript json string, in bytes including '\0'. Must not be longer than COMPAT_MTU including header (3 bytes) static const uint NETWORK_CHAT_LENGTH = 900; ///< The maximum length of a chat message, in bytes including '\0' +static const uint NETWORK_CONTENT_FILENAME_LENGTH = 48; ///< The maximum length of a content's filename, in bytes including '\0'. +static const uint NETWORK_CONTENT_NAME_LENGTH = 32; ///< The maximum length of a content's name, in bytes including '\0'. +static const uint NETWORK_CONTENT_VERSION_LENGTH = 16; ///< The maximum length of a content's version, in bytes including '\0'. +static const uint NETWORK_CONTENT_URL_LENGTH = 96; ///< The maximum length of a content's url, in bytes including '\0'. +static const uint NETWORK_CONTENT_DESC_LENGTH = 512; ///< The maximum length of a content's description, in bytes including '\0'. static const uint NETWORK_CONTENT_TAG_LENGTH = 32; ///< The maximum length of a content's tag, in bytes including '\0'. static const uint NETWORK_GRF_NAME_LENGTH = 80; ///< Maximum length of the name of a GRF diff --git a/src/network/core/tcp_content_type.h b/src/network/core/tcp_content_type.h index 2186997e8..25097c3ab 100644 --- a/src/network/core/tcp_content_type.h +++ b/src/network/core/tcp_content_type.h @@ -60,11 +60,11 @@ struct ContentInfo { ContentType type; ///< Type of content ContentID id; ///< Unique (server side) ID for the content uint32 filesize; ///< Size of the file - char filename[48]; ///< Filename (for the .tar.gz; only valid on download) - char name[32]; ///< Name of the content - char version[16]; ///< Version of the content - char url[96]; ///< URL related to the content - char description[512]; ///< Description of the content + std::string filename; ///< Filename (for the .tar.gz; only valid on download) + std::string name; ///< Name of the content + std::string version; ///< Version of the content + std::string url; ///< URL related to the content + std::string description; ///< Description of the content uint32 unique_id; ///< Unique ID; either GRF ID or shortname byte md5sum[16]; ///< The MD5 checksum std::vector<ContentID> dependencies; ///< The dependencies (unique server side ids) diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index 37df60877..f85c942af 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -56,10 +56,10 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p) ci->id = (ContentID)p->Recv_uint32(); ci->filesize = p->Recv_uint32(); - p->Recv_string(ci->name, lengthof(ci->name)); - p->Recv_string(ci->version, lengthof(ci->version)); - p->Recv_string(ci->url, lengthof(ci->url)); - p->Recv_string(ci->description, lengthof(ci->description), SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE); + ci->name = p->Recv_string(NETWORK_CONTENT_NAME_LENGTH); + ci->version = p->Recv_string(NETWORK_CONTENT_VERSION_LENGTH); + ci->url = p->Recv_string(NETWORK_CONTENT_URL_LENGTH); + ci->description = p->Recv_string(NETWORK_CONTENT_DESC_LENGTH, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE); ci->unique_id = p->Recv_uint32(); for (uint j = 0; j < sizeof(ci->md5sum); j++) { @@ -143,7 +143,7 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p) if (ici->type == ci->type && ici->unique_id == ci->unique_id && memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) { /* Preserve the name if possible */ - if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name)); + if (ci->name.empty()) ci->name = ici->name; if (ici->IsSelected()) ci->state = ici->state; /* @@ -485,7 +485,7 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p) this->curInfo->type = (ContentType)p->Recv_uint8(); this->curInfo->id = (ContentID)p->Recv_uint32(); this->curInfo->filesize = p->Recv_uint32(); - p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename)); + this->curInfo->filename = p->Recv_string(NETWORK_CONTENT_FILENAME_LENGTH); if (!this->BeforeDownload()) { this->CloseConnection(); @@ -704,7 +704,7 @@ void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t l } /* Copy the string, without extension, to the filename. */ - strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename)); + this->curInfo->filename = tmp; /* Request the next file. */ if (!this->BeforeDownload()) { diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 5a03561c9..dfd4370b3 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -147,7 +147,7 @@ void BaseNetworkContentDownloadStatusWindow::DrawWidget(const Rect &r, int widge void BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(const ContentInfo *ci, int bytes) { if (ci->id != this->cur_id) { - strecpy(this->name, ci->filename, lastof(this->name)); + strecpy(this->name, ci->filename.c_str(), lastof(this->name)); this->cur_id = ci->id; this->downloaded_files++; } @@ -408,7 +408,7 @@ class NetworkContentListWindow : public Window, ContentCallback { /** Sort content by name. */ static bool NameSorter(const ContentInfo * const &a, const ContentInfo * const &b) { - return strnatcmp(a->name, b->name, true) < 0; // Sort by name (natural sorting). + return strnatcmp(a->name.c_str(), b->name.c_str(), true) < 0; // Sort by name (natural sorting). } /** Sort content by type. */ @@ -445,7 +445,7 @@ class NetworkContentListWindow : public Window, ContentCallback { filter.string_filter.ResetState(); for (auto &tag : (*a)->tags) filter.string_filter.AddLine(tag.c_str()); - filter.string_filter.AddLine((*a)->name); + filter.string_filter.AddLine((*a)->name.c_str()); return filter.string_filter.GetState(); } @@ -703,17 +703,17 @@ public: SetDParamStr(0, this->selected->name); y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_NAME); - if (!StrEmpty(this->selected->version)) { + if (!this->selected->version.empty()) { SetDParamStr(0, this->selected->version); y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_VERSION); } - if (!StrEmpty(this->selected->description)) { + if (!this->selected->description.empty()) { SetDParamStr(0, this->selected->description); y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DESCRIPTION); } - if (!StrEmpty(this->selected->url)) { + if (!this->selected->url.empty()) { SetDParamStr(0, this->selected->url); y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_URL); } @@ -736,7 +736,7 @@ public: const ContentInfo *ci = *iter; if (ci->id != cid) continue; - p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name); + p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name.c_str()); break; } } @@ -765,7 +765,7 @@ public: for (const ContentInfo *ci : tree) { if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue; - p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name); + p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name.c_str()); } if (p != buf) { SetDParamStr(0, buf); @@ -842,7 +842,7 @@ public: case WID_NCL_OPEN_URL: if (this->selected != nullptr) { extern void OpenBrowser(const char *url); - OpenBrowser(this->selected->url); + OpenBrowser(this->selected->url.c_str()); } break; @@ -983,7 +983,7 @@ public: this->SetWidgetDisabledState(WID_NCL_UNSELECT, this->filesize_sum == 0); this->SetWidgetDisabledState(WID_NCL_SELECT_ALL, !show_select_all); this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade); - this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || StrEmpty(this->selected->url)); + this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || this->selected->url.empty()); for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) { this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE || this->selected->GetTextfile(tft) == nullptr); } diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index f017cbf7f..1b7ab44f6 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1550,7 +1550,7 @@ void ShowMissingContentWindow(const GRFConfig *list) ContentInfo *ci = new ContentInfo(); ci->type = CONTENT_TYPE_NEWGRF; ci->state = ContentInfo::DOES_NOT_EXIST; - strecpy(ci->name, c->GetName(), lastof(ci->name)); + ci->name = c->GetName(); ci->unique_id = BSWAP32(c->ident.grfid); memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum)); cv.push_back(ci); |