Skip to content

Commit 4a624c6

Browse files
authored
More reliable way to deal with whitespace
1 parent 164cf60 commit 4a624c6

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

portable/launchers_src/launcher_template_WINDOWS.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /
1919
std::wstring exeDir = exePath;
2020
exeDir = exeDir.substr(0, exeDir.find_last_of(L"\\/"));
2121

22-
// Get command line string
22+
// Get command line string and extract arguments
2323
LPWSTR commandLine = GetCommandLineW();
24-
// Skip the current executable path and name
2524
std::wstring args;
26-
if (commandLine) {
27-
// If path is double quoted, skip the entire double quote
28-
if (commandLine[0] == L'"') {
29-
LPWSTR closingQuote = wcschr(commandLine + 1, L'"');
30-
if (closingQuote) {
31-
args = closingQuote + 1; // Skip closing quote and space
32-
}
33-
// Otherwise skip to first space
34-
} else {
35-
LPWSTR spacePos = wcschr(commandLine, L' ');
36-
if (spacePos) {
37-
args = spacePos + 1; // GetCommandLineW puts 2 spaces when path isn't double quoted
38-
}
25+
// If executable path is double quoted, skip the entire quoted section
26+
if (commandLine[0] == L'"') {
27+
LPWSTR closingQuote = wcschr(commandLine + 1, L'"');
28+
if (closingQuote) {
29+
args = closingQuote + 1;
30+
}
31+
// For non-quoted path, skip to character after first space if it exists
32+
} else {
33+
LPWSTR spacePos = wcschr(commandLine, L' ');
34+
if (spacePos) {
35+
args = spacePos + 1;
3936
}
4037
}
38+
// Strip leading whitespace
39+
size_t args_start = args.find_first_not_of(L" ");
40+
args = (args_start != std::wstring::npos) ? args.substr(args_start) : L"";
4141

4242
// Define the path to the "scripts" directory
4343
std::wstring scriptsDir = exeDir + L"\\scripts";
@@ -60,7 +60,7 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /
6060
// Define the command to run and append arguments if present
6161
std::wstring target;
6262
if (!args.empty()) {
63-
target = L"cmd.exe /c \"\"" LAUNCH_TARGET L"\"" + args + L"\"";
63+
target = L"cmd.exe /c \"\"" LAUNCH_TARGET L"\" " + args + L"\"";
6464
} else {
6565
target = L"cmd.exe /c \"" LAUNCH_TARGET L"\"";
6666
}

0 commit comments

Comments
 (0)