示例#1
0
bool DAOProxy::split(const string& str, VectorString& array)
{
    //ACS_TRACE("cdb::DAOProxy::split");

    // The string that will be added to the list next.
    string strCur; 
    // Tells us what kind of quote we are in.
    bool bQuote = 0;

    unsigned int iter = 0;
    unsigned int len = str.length();

    array.clear();

    while(iter < len)
	{
	// We got to a whitespace and we are not in a quote: push the currently
	// build substring at the end of the array.
	if(!bQuote && str[iter] == ',')
	    {
	    if(strCur.length()!=0)
		{
		array.push_back(strCur);
		strCur.erase(strCur.begin(), strCur.end());  // using erase because clear not supported on VxWorks
		}
	    }
	// Escape sequence.
	else if(str[iter] == '\\')      
	    {
	    ++iter;
	    // Whoops, escape ended before the new line.
	    if(iter == len) 
		{
		return false;
		}
	    switch(str[iter])
		{
		case 'n':
		    strCur += '\n';
		    break;
		case 'r':
		    strCur += '\r';
		    break;
		case ',':
		case '\\':
		case '\'':
		case '"':
		    // Treat next character verbatim, regardless what it may be.
		    strCur += str[iter];
		    break;
		default:
		    // An unrecognized escape!
		    return false;
		}
	    }
	// The quote ended.
	else if(bQuote && str[iter] == '"')
	    {
	    // Indicate that we are in the quote no longer.
	    bQuote = 0;
	    array.push_back(strCur);
	    strCur.erase(strCur.begin(), strCur.end());  // using erase because clear not supported on VxWorks
	    }
	// The quote begun.
	else if(str[iter] == '"')
	    {
	    if(strCur.length()!=0)
		{
		array.push_back(strCur);
		strCur.erase(strCur.begin(), strCur.end()); // using erase because clear not supported on VxWorks 
		}
	    bQuote = 1;
	    }
	else
	    {
	    // A regular character.
	    strCur += str[iter];
	    }
	++iter;
	}

    // Push the last string to the end of the array.
    if(strCur.length()!=0)
	{
	array.push_back(strCur);
	}

    return true;
}