From f2764176c58829d24fee7a830a3c9ac2b57d1906 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 25 Mar 2019 20:40:13 +0300 Subject: major fix for the command-line parser --- src/mir_core/src/cmdline.cpp | 42 +++++++++++++++--------------------------- src/mir_core/src/stdafx.h | 1 + 2 files changed, 16 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/mir_core/src/cmdline.cpp b/src/mir_core/src/cmdline.cpp index b0b98df2aa..29f4a8a83c 100644 --- a/src/mir_core/src/cmdline.cpp +++ b/src/mir_core/src/cmdline.cpp @@ -43,42 +43,30 @@ static OBJLIST arParams(5, CompareParams); MIR_CORE_DLL(void) CmdLine_Parse(const wchar_t *ptszCmdLine) { - bool bPrevSpace = true; - for (wchar_t *p = NEWWSTR_ALLOCA(ptszCmdLine); *p; p++) { - if (*p == ' ' || *p == '\t') { - *p = 0; - bPrevSpace = true; - continue; - } + int nArgs = 0; + wchar_t **pArgs = CommandLineToArgvW(ptszCmdLine, &nArgs); + if (pArgs == nullptr) + return; - // new word beginning - if (bPrevSpace) { - bPrevSpace = false; - if (*p != '/' && *p != '-') // not an option - skip it - continue; - } - else continue; // skip a text that isn't an option + for (int i=0; i < nArgs; i++) { + wchar_t *pOptionName = pArgs[i], *p; - wchar_t *pOptionName = p + 1; - if ((p = wcspbrk(pOptionName, L" \t=:")) == nullptr) { // no more text in string - arParams.insert(new CmdLineParam(pOptionName, L"")); - break; - } + // not an option? skip it + if (*pOptionName != '/' && *pOptionName != '-') + continue; - if (*p == ' ' || *p == '\t') { + pOptionName++; + if ((p = wcspbrk(pOptionName, L"=:")) == nullptr) { // no more text in string arParams.insert(new CmdLineParam(pOptionName, L"")); - p--; // the cycle will wipe this space automatically - continue; + break; } // parameter with value *p = 0; - arParams.insert(new CmdLineParam(pOptionName, ++p)); - if ((p = wcspbrk(p, L" \t")) == nullptr) // no more text in string - break; - - p--; // the cycle will wipe this space automatically + arParams.insert(new CmdLineParam(pOptionName, p+1)); } + + LocalFree(pArgs); } MIR_CORE_DLL(const wchar_t*) CmdLine_GetOption(const wchar_t* ptszParameter) diff --git a/src/mir_core/src/stdafx.h b/src/mir_core/src/stdafx.h index 6486e2fbfa..2c57073066 100644 --- a/src/mir_core/src/stdafx.h +++ b/src/mir_core/src/stdafx.h @@ -32,6 +32,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include +#include #include #include #include -- cgit v1.2.3