From 80bd5ad727a4abe29fe53484f93aead8e85e0e9c Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Wed, 26 Apr 2023 12:56:14 +0100 Subject: [PATCH] Codechange: Use std::strto* variants everywhere (#10720) --- src/company_gui.cpp | 4 ++-- src/console.cpp | 2 +- src/console_cmds.cpp | 2 +- src/debug.cpp | 4 ++-- src/fileio.cpp | 2 +- src/fios.cpp | 2 +- src/newgrf_debug_gui.cpp | 2 +- src/openttd.cpp | 6 +++--- src/settings.cpp | 6 +++--- src/stdafx.h | 1 - src/strgen/strgen.cpp | 6 +++--- src/strgen/strgen_base.cpp | 6 +++--- src/strings.cpp | 4 ++-- src/timetable_gui.cpp | 2 +- 14 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 8d9f2d2db6..cf523142ea 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -1766,7 +1766,7 @@ public: if (str == nullptr) return; /* Set a new company manager face number */ if (!StrEmpty(str)) { - this->face = strtoul(str, nullptr, 10); + this->face = std::strtoul(str, nullptr, 10); ScaleAllCompanyManagerFaceBits(this->face); ShowErrorMessage(STR_FACE_FACECODE_SET, INVALID_STRING_ID, WL_INFO); this->UpdateData(); @@ -2714,7 +2714,7 @@ struct CompanyWindow : Window default: NOT_REACHED(); case WID_C_GIVE_MONEY: { - Money money = (Money)(strtoull(str, nullptr, 10) / _currency->rate); + Money money = (Money)(std::strtoull(str, nullptr, 10) / _currency->rate); uint32 money_c = Clamp(ClampToI32(money), 0, 20000000); // Clamp between 20 million and 0 Command::Post(STR_ERROR_CAN_T_GIVE_MONEY, money_c, (CompanyID)this->window_number); diff --git a/src/console.cpp b/src/console.cpp index 393dca2754..fd0082e4be 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -146,7 +146,7 @@ bool GetArgumentInteger(uint32 *value, const char *arg) return true; } - *value = strtoul(arg, &endptr, 0); + *value = std::strtoul(arg, &endptr, 0); return arg != endptr; } diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index e307d49f4f..74e380eec9 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1131,7 +1131,7 @@ DEF_CONSOLE_CMD(ConNewGame) return true; } - StartNewGameWithoutGUI((argc == 2) ? strtoul(argv[1], nullptr, 10) : GENERATE_NEW_SEED); + StartNewGameWithoutGUI((argc == 2) ? std::strtoul(argv[1], nullptr, 10) : GENERATE_NEW_SEED); return true; } diff --git a/src/debug.cpp b/src/debug.cpp index 25c90fa12c..85c187afe8 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -174,7 +174,7 @@ void SetDebugString(const char *s, void (*error_func)(const std::string &)) if (*s >= '0' && *s <= '9') { const DebugLevel *i; - v = strtoul(s, &end, 0); + v = std::strtoul(s, &end, 0); s = end; for (i = debug_level; i != endof(debug_level); ++i) { @@ -201,7 +201,7 @@ void SetDebugString(const char *s, void (*error_func)(const std::string &)) } if (*s == '=') s++; - v = strtoul(s, &end, 0); + v = std::strtoul(s, &end, 0); s = end; if (found != nullptr) { new_levels[found->name] = v; diff --git a/src/fileio.cpp b/src/fileio.cpp index 04f6408664..9a41a281e6 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -538,7 +538,7 @@ bool TarScanner::AddFile(const std::string &filename, size_t basepath_length, co /* Calculate the size of the file.. for some strange reason this is stored as a string */ strecpy(buf, th.size, lastof(buf)); - size_t skip = strtoul(buf, &end, 8); + size_t skip = std::strtoul(buf, &end, 8); switch (th.typeflag) { case '\0': diff --git a/src/fios.cpp b/src/fios.cpp index b07db3c8b1..793a4fd433 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -111,7 +111,7 @@ const FiosItem *FileList::FindItem(const char *file) /* If no name matches, try to parse it as number */ char *endptr; - int i = strtol(file, &endptr, 10); + int i = std::strtol(file, &endptr, 10); if (file == endptr || *endptr != '\0') i = -1; if (IsInsideMM(i, 0, this->size())) return &this->at(i); diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index 2a8a4b40ba..01c0826d33 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -618,7 +618,7 @@ struct NewGRFInspectWindow : Window { { if (StrEmpty(str)) return; - NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][this->current_edit_param - 0x60] = strtol(str, nullptr, 16); + NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][this->current_edit_param - 0x60] = std::strtol(str, nullptr, 16); this->SetDirty(); } diff --git a/src/openttd.cpp b/src/openttd.cpp index 01de330ade..283ea903d9 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -286,8 +286,8 @@ static void ParseResolution(Dimension *res, const char *s) return; } - res->width = std::max(strtoul(s, nullptr, 0), 64UL); - res->height = std::max(strtoul(t + 1, nullptr, 0), 64UL); + res->width = std::max(std::strtoul(s, nullptr, 0), 64UL); + res->height = std::max(std::strtoul(t + 1, nullptr, 0), 64UL); } @@ -637,7 +637,7 @@ int openttd_main(int argc, char *argv[]) _skip_all_newgrf_scanning += 1; break; } - case 'G': scanner->generation_seed = strtoul(mgo.opt, nullptr, 10); break; + case 'G': scanner->generation_seed = std::strtoul(mgo.opt, nullptr, 10); break; case 'c': _config_file = mgo.opt; break; case 'x': scanner->save_config = false; break; case 'X': only_local_path = true; break; diff --git a/src/settings.cpp b/src/settings.cpp index bab8e2d095..87e9c02208 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -174,7 +174,7 @@ const uint16 INIFILE_VERSION = (IniFileVersion)(IFV_MAX_VERSION - 1); ///< Curre size_t OneOfManySettingDesc::ParseSingleValue(const char *str, size_t len, const std::vector &many) { /* check if it's an integer */ - if (isdigit(*str)) return strtoul(str, nullptr, 0); + if (isdigit(*str)) return std::strtoul(str, nullptr, 0); size_t idx = 0; for (auto one : many) { @@ -245,7 +245,7 @@ static int ParseIntList(const char *p, T *items, int maxitems) default: { if (n == maxitems) return -1; // we don't accept that many numbers char *end; - unsigned long v = strtoul(p, &end, 0); + unsigned long v = std::strtoul(p, &end, 0); if (p == end) return -1; // invalid character (not a number) if (sizeof(T) < sizeof(v)) v = Clamp(v, std::numeric_limits::min(), std::numeric_limits::max()); items[n++] = v; @@ -377,7 +377,7 @@ void ManyOfManySettingDesc::FormatValue(char *buf, const char *last, const void size_t IntSettingDesc::ParseValue(const char *str) const { char *end; - size_t val = strtoul(str, &end, 0); + size_t val = std::strtoul(str, &end, 0); if (end == str) { ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE); msg.SetDParamStr(0, str); diff --git a/src/stdafx.h b/src/stdafx.h index f09cf88f72..cf2500bc97 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -251,7 +251,6 @@ # define strcasecmp stricmp # define strncasecmp strnicmp -# define strtoull _strtoui64 /* MSVC doesn't have these :( */ # define S_ISDIR(mode) (mode & S_IFDIR) diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index b0dad3f58e..3407a3e441 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -117,7 +117,7 @@ struct FileStringReader : StringReader { void FileStringReader::HandlePragma(char *str) { if (!memcmp(str, "id ", 3)) { - this->data.next_string_id = strtoul(str + 3, nullptr, 0); + this->data.next_string_id = std::strtoul(str + 3, nullptr, 0); } else if (!memcmp(str, "name ", 5)) { strecpy(_lang.name, str + 5, lastof(_lang.name)); } else if (!memcmp(str, "ownname ", 8)) { @@ -143,14 +143,14 @@ void FileStringReader::HandlePragma(char *str) strecpy(_lang.digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_decimal_separator)); } else if (!memcmp(str, "winlangid ", 10)) { const char *buf = str + 10; - long langid = strtol(buf, nullptr, 16); + long langid = std::strtol(buf, nullptr, 16); if (langid > (long)UINT16_MAX || langid < 0) { FatalError("Invalid winlangid {}", buf); } _lang.winlangid = (uint16)langid; } else if (!memcmp(str, "grflangid ", 10)) { const char *buf = str + 10; - long langid = strtol(buf, nullptr, 16); + long langid = std::strtol(buf, nullptr, 16); if (langid >= 0x7F || langid < 0) { FatalError("Invalid grflangid {}", buf); } diff --git a/src/strgen/strgen_base.cpp b/src/strgen/strgen_base.cpp index 6c8ddccc1c..4f293dc41c 100644 --- a/src/strgen/strgen_base.cpp +++ b/src/strgen/strgen_base.cpp @@ -309,7 +309,7 @@ bool ParseRelNum(char **buf, int *value, int *offset) rel = true; s++; } - int v = strtol(s, &end, 0); + int v = std::strtol(s, &end, 0); if (end == s) return false; if (rel || v < 0) { *value += v; @@ -319,7 +319,7 @@ bool ParseRelNum(char **buf, int *value, int *offset) if (offset != nullptr && *end == ':') { /* Take the Nth within */ s = end + 1; - *offset = strtol(s, &end, 0); + *offset = std::strtol(s, &end, 0); if (end == s) return false; } *buf = end; @@ -509,7 +509,7 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a if (*s >= '0' && *s <= '9') { char *end; - *argno = strtoul(s, &end, 0); + *argno = std::strtoul(s, &end, 0); if (*end != ':') StrgenFatal("missing arg #"); s = end + 1; } diff --git a/src/strings.cpp b/src/strings.cpp index e6f96c4ee5..d359b858c6 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -874,7 +874,7 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg memset(sub_args_need_free, 0, sizeof(sub_args_need_free)); char *p; - uint32 stringid = strtoul(str, &p, 16); + uint32 stringid = std::strtoul(str, &p, 16); if (*p != ':' && *p != '\0') { while (*p != '\0') p++; str = p; @@ -926,7 +926,7 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg bool lookup = (l == SCC_ENCODED); if (lookup) s += len; - param = strtoull(s, &p, 16); + param = std::strtoull(s, &p, 16); if (lookup) { if (param >= TAB_SIZE_GAMESCRIPT) { diff --git a/src/timetable_gui.cpp b/src/timetable_gui.cpp index 65b3b26de7..d803ca4537 100644 --- a/src/timetable_gui.cpp +++ b/src/timetable_gui.cpp @@ -667,7 +667,7 @@ struct TimetableWindow : Window { if (str == nullptr) return; const Vehicle *v = this->vehicle; - uint64 val = StrEmpty(str) ? 0 : strtoul(str, nullptr, 10); + uint64 val = StrEmpty(str) ? 0 : std::strtoul(str, nullptr, 10); auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, query_widget == WID_VT_CHANGE_SPEED); switch (query_widget) {