import core.sys.windows.windows;
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.path;
import std.string;
import std.utf;


struct S3
{}


private:

string[2] divideCommandLine(string commandLine)
{
	commandLine = commandLine.strip();
	immutable quoted = commandLine.skipOver('"');
	immutable r = commandLine.findSplit(quoted ? `"` : " ");
	return [r[0], r[2].stripLeft()];
}

string[] extractCommandLineArguments(string commandLine)
{
	int numArgs;
	LPWSTR* argsW = enforce(CommandLineToArgvW(toUTF16z(commandLine), &numArgs));
	scope(exit) LocalFree(argsW);
	return argsW[1 .. numArgs].map!(a => fromUTF16z(a))().array();
}

string fromUTF16z(in wchar* wstr)
{
	import core.stdc.wchar_;
	return to!string(wstr[0 .. wcslen(wstr)]);
}


// WinAPI
// --------------------------------------------------

extern(Windows) nothrow
{
	extern LPWSTR* CommandLineToArgvW(LPCWSTR lpCmdLine, int *pNumArgs);
	alias HANDLE HLOCAL;
	extern HLOCAL LocalFree(HLOCAL hMem);
}
