(Win32 refresh) CommandLine

Directly use CommandLineToArgvW, replacing legacy code. Support Unicode natively by natively using std::string and wchar.  shellapi.h is brought in for CommandLineToArgvW
This commit is contained in:
sukibaby
2025-01-20 09:45:43 -08:00
committed by teejusb
parent 54232bed78
commit f783588adb
+24 -34
View File
@@ -1,48 +1,38 @@
#include "global.h" #include "global.h"
#include "CommandLine.h" #include "CommandLine.h"
#include <windows.h> #include <windows.h>
#include <shellapi.h>
#include <vector>
#include <string>
/* Ugh. Windows doesn't give us the argv[] parser; all it gives is /* Use CommandLineToArgvW to parse the command line arguments. */
* CommandLineToArgvW, which is NT-only, so we have to do this ourself. Don't
* be fancy; only handle double quotes. */
int GetWin32CmdLine( char** &argv ) int GetWin32CmdLine( char** &argv )
{ {
char *pCmdLine = GetCommandLine(); LPWSTR* argvW = nullptr;
int argc = 0; int argc = 0;
argv = nullptr; argvW = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argvW == nullptr)
int i = 0;
while( pCmdLine[i] )
{ {
argv = (char **) realloc( argv, (argc+1) * sizeof(char *) ); argv = nullptr;
argv[argc] = pCmdLine+i; return -1;
++argc; }
// Skip to the end of this argument. std::vector<std::string> args;
while( pCmdLine[i] && pCmdLine[i] != ' ' ) for (int i = 0; i < argc; ++i)
{ {
if( pCmdLine[i] == '"' ) int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, nullptr, 0, nullptr, nullptr);
{ std::string arg(size_needed, 0);
// Erase the quote. WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, &arg[0], size_needed, nullptr, nullptr);
memmove( pCmdLine+i, pCmdLine+i+1, strlen(pCmdLine+i+1)+1 ); args.push_back(arg);
}
// Skip to the close quote. LocalFree(argvW);
while( pCmdLine[i] && pCmdLine[i] != '"' )
++i;
// Erase the close quote. argv = new char* [args.size()];
if( pCmdLine[i] == '"' ) for (size_t i = 0; i < args.size(); ++i)
memmove( pCmdLine+i, pCmdLine+i+1, strlen(pCmdLine+i+1)+1 ); {
} argv[i] = new char[args[i].size() + 1];
else strcpy(argv[i], args[i].c_str());
++i;
}
if( pCmdLine[i] == ' ' )
{
pCmdLine[i] = '\0';
++i;
}
} }
return argc; return argc;