Skip to content

Commit

Permalink
More robust command line argument processing
Browse files Browse the repository at this point in the history
  • Loading branch information
watamario15 committed Jul 16, 2023
1 parent 25073dc commit 900397d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, wchar_t *lpCmd

global::hInst = hInstance;

// Replaces slashes with backslashes.
for (i = 0; lpCmdLine[i]; ++i) {
if (lpCmdLine[i] == L'/') lpCmdLine[i] = L'\\';
while (*lpCmdLine == L' ') ++lpCmdLine;
bool isQuoted = false;
if (*lpCmdLine == L'"') {
isQuoted = true;
++lpCmdLine;
}

// Removes quotes if exist (uses the `i` obtained from the last operation).
if (lpCmdLine[0] == L'"') {
lpCmdLine[i - 1] = L'\0';
global::cmdLine = lpCmdLine + 1;
} else {
global::cmdLine = lpCmdLine;
for (i = 0; lpCmdLine[i]; ++i) { // Replaces slashes with backslashes, and removes a closing quote.
if (lpCmdLine[i] == L'"' || (lpCmdLine[i] == L' ' && !isQuoted)) {
lpCmdLine[i] = L'\0';
break;
}
if (lpCmdLine[i] == L'/') lpCmdLine[i] = L'\\';
}
for (--i; i >= 0 && lpCmdLine[i] == ' '; --i) lpCmdLine[i] = L'\0'; // Removes trailing spaces.
global::cmdLine = lpCmdLine;

WNDCLASSW wcl;
memset(&wcl, 0, sizeof(WNDCLASSW));
Expand Down

0 comments on commit 900397d

Please sign in to comment.