好用的工具函数

 

extern "C" void CommandLineToArgs(TCHAR szCommandLine[], int *argc, LPTSTR argv[])
{
    // Parse command line into individual arguments - arg/argv style.
 int i, iArgc = 0;
    BOOL fInQuotes, bEndFound = FALSE;
 TCHAR *p = szCommandLine;

 if(!szCommandLine || !argc || !argv)
  return;

    for(i = 0; i < *argc && !bEndFound; i++)
    {
        // Clear our quote flag
        fInQuotes = FALSE;

        // Move past and zero out any leading whitespace
        while( *p && _istspace(*p) )
            *(p++) = TEXT('\0');

        // If the next character is a quote, move over it and remember that we saw it
        if (*p == TEXT('\"'))
        {
            *(p++) = TEXT('\0');
            fInQuotes = TRUE;
        }

        // Point the current argument to our current location
        argv[i] = p;

        // If this argument contains some text, then update our argument count
        if (*p)
            iArgc = i + 1;

        // Move over valid text of this argument.
        while (*p)
        {
            if (fInQuotes)
            {
                // If in quotes, we only break out when we see another quote.
                if (*p == TEXT('\"'))
                {
                    *(p++) = TEXT('\0');
                    break;
                }
    
   }
   else
   {
    // If we are not in quotes and we see a quote, replace it with a space
    // and set "fInQuotes" to TRUE
    if (*p == TEXT('\"'))
    {
     *(p++) = TEXT(' ');
     fInQuotes = TRUE;
    }   // If we are not in quotes and we see whitespace, then we break out
    else if (_istspace(*p))
    {
     *(p++) = TEXT('\0');
     break;
    }
            }
            // Move to the next character
            p++;
        }

  if(!*p)
   bEndFound = TRUE;
    }

    *argc = iArgc;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值