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

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