From 900397d146a6f0d33af6086a3f2c7e70a80683db Mon Sep 17 00:00:00 2001 From: watamario15 Date: Sun, 16 Jul 2023 13:28:10 +0900 Subject: [PATCH] More robust command line argument processing --- main.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index 994ae90..40bb828 100644 --- a/main.cpp +++ b/main.cpp @@ -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));