Example #1
0
int CSV_Parser::parse_quoted_fields(const STR& input_line, STR& field, int& i)
{
    /*
        Quoted fields are the ones which are enclosed within quotes
        For instance - Consider that input_line is - 1997,Ford,E350,"Super, luxurious truck"
        An example for a quoted field would be - Super luxurious truck
        Another instance being - 1997,Ford,E350,"Super, ""luxurious"" truck"
    */
    int j;
    field = "";

    for(j=i; j<input_line.length(); j++)
    {
        if(input_line[j] == '"' && input_line[++j] != '"')
        {
            int k = input_line.find_first_of(CSV_DELIMITER, j);
            if(k > input_line.length())
            {
                k = input_line.length();
            }
            for(k -= j; k-- > 0; )
            {
                field += input_line[j++];
            }
            break;
        }
        else
        {
            field += input_line[j];
        }
    }
    return j;
}
Example #2
0
int CSV_Parser::parse_normal_fields(const STR& input_line, STR& field, int& i)
{
    /*
        Normal fields are the ones which contain no escaped or quoted characters
        For instance - Consider that input_line is - 1997,Ford,E350,"Super, luxurious truck"
        An example for a normal field would be - Ford
    */
    int j;
    j = input_line.find_first_of(CSV_DELIMITER, i);
    if(j > input_line.length())
    {
        j = input_line.length();
    }
    field = std :: string(input_line, i, j-i);
    return j;
}
Example #3
0
static size_t TokenizeT(const STR& str,
	const STR& delimiters,
	std::vector<STR>* tokens) {
	tokens->clear();

	size_t start = str.find_first_not_of(delimiters);
	while (start != STR::npos) {
		size_t end = str.find_first_of(delimiters, start + 1);
		if (end == STR::npos) {
			tokens->push_back(str.substr(start));
			break;
		}
		else {
			tokens->push_back(str.substr(start, end - start));
			start = str.find_first_not_of(delimiters, end + 1);
		}
	}

	return tokens->size();
}