Codechange: pass command line arguments as std::span to openttd_main

This commit is contained in:
Rubidium 2024-04-11 13:25:40 +02:00 committed by rubidium42
parent 3316b27496
commit eda10abc8c
5 changed files with 21 additions and 24 deletions

View File

@ -502,11 +502,10 @@ static std::vector<OptionData> CreateOptions()
/** /**
* Main entry point for this lovely game. * Main entry point for this lovely game.
* @param argc The number of arguments passed to this game. * @param arguments The command line arguments passed to the application.
* @param argv The values of the arguments.
* @return 0 when there is no error. * @return 0 when there is no error.
*/ */
int openttd_main(int argc, char *argv[]) int openttd_main(std::span<char * const> arguments)
{ {
_game_session_stats.start_time = std::chrono::steady_clock::now(); _game_session_stats.start_time = std::chrono::steady_clock::now();
_game_session_stats.savegame_size = std::nullopt; _game_session_stats.savegame_size = std::nullopt;
@ -530,7 +529,7 @@ int openttd_main(int argc, char *argv[])
_switch_mode = SM_MENU; _switch_mode = SM_MENU;
auto options = CreateOptions(); auto options = CreateOptions();
GetOptData mgo(std::span(argv + 1, argc - 1), options); GetOptData mgo(arguments.subspan(1), options);
int ret = 0; int ret = 0;
int i; int i;
@ -614,7 +613,7 @@ int openttd_main(int argc, char *argv[])
} }
break; break;
case 'q': { case 'q': {
DeterminePaths(argv[0], only_local_path); DeterminePaths(arguments[0], only_local_path);
if (StrEmpty(mgo.opt)) { if (StrEmpty(mgo.opt)) {
ret = 1; ret = 1;
return ret; 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 * 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. */ * 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); TarScanner::DoScan(TarScanner::BASESET);
BaseGraphics::FindSets(); BaseGraphics::FindSets();
BaseSounds::FindSets(); BaseSounds::FindSets();
@ -669,7 +668,7 @@ int openttd_main(int argc, char *argv[])
return ret; return ret;
} }
DeterminePaths(argv[0], only_local_path); DeterminePaths(arguments[0], only_local_path);
TarScanner::DoScan(TarScanner::BASESET); TarScanner::DoScan(TarScanner::BASESET);
if (dedicated) Debug(net, 3, "Starting dedicated server, version {}", _openttd_revision); if (dedicated) Debug(net, 3, "Starting dedicated server, version {}", _openttd_revision);

View File

@ -87,7 +87,7 @@ extern PauseMode _pause_mode;
void AskExitGame(); void AskExitGame();
void AskExitToGameMenu(); void AskExitToGameMenu();
int openttd_main(int argc, char *argv[]); int openttd_main(std::span<char * const> arguments);
void StateGameLoop(); void StateGameLoop();
void HandleExitGameRequest(); void HandleExitGameRequest();

View File

@ -41,7 +41,7 @@ int CDECL main(int argc, char *argv[])
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
int ret = openttd_main(argc, argv); int ret = openttd_main(std::span(argv, argc));
CocoaReleaseAutoreleasePool(); CocoaReleaseAutoreleasePool();

View File

@ -29,5 +29,5 @@ int CDECL main(int argc, char *argv[])
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
return openttd_main(argc, argv); return openttd_main(std::span(argv, argc));
} }

View File

@ -18,11 +18,10 @@
#include "../../safeguards.h" #include "../../safeguards.h"
static int ParseCommandLine(char *line, char **argv, int max_argc) static auto ParseCommandLine(char *line)
{ {
int n = 0; std::vector<char *> arguments;
for (;;) {
do {
/* skip whitespace */ /* skip whitespace */
while (*line == ' ' || *line == '\t') line++; while (*line == ' ' || *line == '\t') line++;
@ -31,22 +30,22 @@ static int ParseCommandLine(char *line, char **argv, int max_argc)
/* special handling when quoted */ /* special handling when quoted */
if (*line == '"') { if (*line == '"') {
argv[n++] = ++line; arguments.push_back(++line);
while (*line != '"') { while (*line != '"') {
if (*line == '\0') return n; if (*line == '\0') return arguments;
line++; line++;
} }
} else { } else {
argv[n++] = line; arguments.push_back(line);
while (*line != ' ' && *line != '\t') { while (*line != ' ' && *line != '\t') {
if (*line == '\0') return n; if (*line == '\0') return arguments;
line++; line++;
} }
} }
*line++ = '\0'; *line++ = '\0';
} while (n != max_argc); };
return n; return arguments;
} }
void CreateConsole(); void CreateConsole();
@ -73,13 +72,12 @@ int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
/* setup random seed to something quite random */ /* setup random seed to something quite random */
SetRandomSeed(GetTickCount()); SetRandomSeed(GetTickCount());
char *argv[64]; // max 64 command line arguments auto arguments = ParseCommandLine(cmdline.data());
int argc = ParseCommandLine(cmdline.data(), argv, lengthof(argv));
/* Make sure our arguments contain only valid UTF-8 characters. */ /* 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. */ /* Restore system timer resolution. */
timeEndPeriod(1); timeEndPeriod(1);