From eda10abc8c761acc4379d26d88e6a31ce3c80c5c Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 11 Apr 2024 13:25:40 +0200 Subject: [PATCH] Codechange: pass command line arguments as std::span to openttd_main --- src/openttd.cpp | 13 ++++++------- src/openttd.h | 2 +- src/os/macosx/osx_main.cpp | 2 +- src/os/unix/unix_main.cpp | 2 +- src/os/windows/win32_main.cpp | 26 ++++++++++++-------------- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/openttd.cpp b/src/openttd.cpp index 55862f6ba3..898ea24832 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -502,11 +502,10 @@ static std::vector CreateOptions() /** * Main entry point for this lovely game. - * @param argc The number of arguments passed to this game. - * @param argv The values of the arguments. + * @param arguments The command line arguments passed to the application. * @return 0 when there is no error. */ -int openttd_main(int argc, char *argv[]) +int openttd_main(std::span arguments) { _game_session_stats.start_time = std::chrono::steady_clock::now(); _game_session_stats.savegame_size = std::nullopt; @@ -530,7 +529,7 @@ int openttd_main(int argc, char *argv[]) _switch_mode = SM_MENU; auto options = CreateOptions(); - GetOptData mgo(std::span(argv + 1, argc - 1), options); + GetOptData mgo(arguments.subspan(1), options); int ret = 0; int i; @@ -614,7 +613,7 @@ int openttd_main(int argc, char *argv[]) } break; case 'q': { - DeterminePaths(argv[0], only_local_path); + DeterminePaths(arguments[0], only_local_path); if (StrEmpty(mgo.opt)) { ret = 1; return ret; @@ -660,7 +659,7 @@ int openttd_main(int argc, char *argv[]) * * The next two functions are needed to list the graphics sets. We can't do them earlier * because then we cannot show it on the debug console as that hasn't been configured yet. */ - DeterminePaths(argv[0], only_local_path); + DeterminePaths(arguments[0], only_local_path); TarScanner::DoScan(TarScanner::BASESET); BaseGraphics::FindSets(); BaseSounds::FindSets(); @@ -669,7 +668,7 @@ int openttd_main(int argc, char *argv[]) return ret; } - DeterminePaths(argv[0], only_local_path); + DeterminePaths(arguments[0], only_local_path); TarScanner::DoScan(TarScanner::BASESET); if (dedicated) Debug(net, 3, "Starting dedicated server, version {}", _openttd_revision); diff --git a/src/openttd.h b/src/openttd.h index 2fb27e9288..f6728ee70d 100644 --- a/src/openttd.h +++ b/src/openttd.h @@ -87,7 +87,7 @@ extern PauseMode _pause_mode; void AskExitGame(); void AskExitToGameMenu(); -int openttd_main(int argc, char *argv[]); +int openttd_main(std::span arguments); void StateGameLoop(); void HandleExitGameRequest(); diff --git a/src/os/macosx/osx_main.cpp b/src/os/macosx/osx_main.cpp index f33e371436..7d8a7994e9 100644 --- a/src/os/macosx/osx_main.cpp +++ b/src/os/macosx/osx_main.cpp @@ -41,7 +41,7 @@ int CDECL main(int argc, char *argv[]) signal(SIGPIPE, SIG_IGN); - int ret = openttd_main(argc, argv); + int ret = openttd_main(std::span(argv, argc)); CocoaReleaseAutoreleasePool(); diff --git a/src/os/unix/unix_main.cpp b/src/os/unix/unix_main.cpp index 41ad22ced5..4a71b886b3 100644 --- a/src/os/unix/unix_main.cpp +++ b/src/os/unix/unix_main.cpp @@ -29,5 +29,5 @@ int CDECL main(int argc, char *argv[]) signal(SIGPIPE, SIG_IGN); - return openttd_main(argc, argv); + return openttd_main(std::span(argv, argc)); } diff --git a/src/os/windows/win32_main.cpp b/src/os/windows/win32_main.cpp index ebc22ffc71..9b5c815e3a 100644 --- a/src/os/windows/win32_main.cpp +++ b/src/os/windows/win32_main.cpp @@ -18,11 +18,10 @@ #include "../../safeguards.h" -static int ParseCommandLine(char *line, char **argv, int max_argc) +static auto ParseCommandLine(char *line) { - int n = 0; - - do { + std::vector arguments; + for (;;) { /* skip whitespace */ while (*line == ' ' || *line == '\t') line++; @@ -31,22 +30,22 @@ static int ParseCommandLine(char *line, char **argv, int max_argc) /* special handling when quoted */ if (*line == '"') { - argv[n++] = ++line; + arguments.push_back(++line); while (*line != '"') { - if (*line == '\0') return n; + if (*line == '\0') return arguments; line++; } } else { - argv[n++] = line; + arguments.push_back(line); while (*line != ' ' && *line != '\t') { - if (*line == '\0') return n; + if (*line == '\0') return arguments; line++; } } *line++ = '\0'; - } while (n != max_argc); + }; - return n; + return arguments; } void CreateConsole(); @@ -73,13 +72,12 @@ int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) /* setup random seed to something quite random */ SetRandomSeed(GetTickCount()); - char *argv[64]; // max 64 command line arguments - int argc = ParseCommandLine(cmdline.data(), argv, lengthof(argv)); + auto arguments = ParseCommandLine(cmdline.data()); /* Make sure our arguments contain only valid UTF-8 characters. */ - for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); + for (auto argument : arguments) StrMakeValidInPlace(argument); - int ret = openttd_main(argc, argv); + int ret = openttd_main(arguments); /* Restore system timer resolution. */ timeEndPeriod(1);