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.
* @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<char * const> 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);

View File

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

View File

@ -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();

View File

@ -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));
}

View File

@ -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<char *> 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);