views:

119

answers:

1

Hi,

I need to be able to to split the executable path and arguments in a command.

Windows handles the following easily:

"notepad.exe C:\testfile.txt"

"notepad c:\testfolder\versioninfo.txt"

"C:\Windows\notepad.exe" "C:\test folder\versioninfo.txt"

rundll "C\Windows\somelibrary.dll"

Anyone has a piece of code to parse such strings?

Thanks.

A: 

I've used something like this in the past:

char* lpCmdLine = ...;
char* lpArgs = lpCmdLine;
// skip leading spaces
while(isspace(*lpArgs))
    lpArgs++;
if(*lpArgs == '\"')
{
    // executable is quoted; skip to first space after matching quote
    lpArgs++;
    int quotes = 1;
    while(*lpArgs)
    {
        if(isspace(*lpArgs) && !quotes)
            break;
        if(*lpArgs == '\"')
            quotes = !quotes;
    }
}
else
{
    // executable is not quoted; skip to first space
    while(*lpArgs && !isspace(*lpArgs))
        lpArgs++;
}
// TODO: skip any spaces before the first arg
Luke