CSV like Parse ( whitespace delimiter and boost )

619 views Asked by At

i want to parse a CSV-like file, line with boost. There are many different methods like split, tokenise, spirit, regex...

A parsing line could look like: "abc" "def" "hij \"hgfd\" " and the result should look like:

"abc"
"def"
"hij \"hgfd\" "

I thought that using boost's tokenises with the escaped_list_separator would be a great idea but it is not possible to split on whitespace delimiter, isnt it ?

1

There are 1 answers

3
sehe On BEST ANSWER

Here's a quick and dirty to match just what you described using Spirit (multiple lines into a vector>):

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_match.hpp>
namespace qi = boost::spirit::qi;

int main() {
    std::vector<std::vector<std::string>> csv_data;

    if (std::cin 
            >> std::noskipws 
            >> qi::phrase_match(*qi::lexeme['"' >> *('\\' >> qi::char_ | ~qi::char_("\r\n\"")) >> '"'] % qi::eol, qi::blank, csv_data))
    {
        std::cout << "Parse succeeded: " << csv_data.size() << "\n";
        for(auto& row: csv_data) {
            for(auto& c: row) std::cout << c << '|';
            std::cout << "\n";
        }
    } else {
        std::cout << "Parse failed\n";
    }
}

The example printing:

Parse succeeded: 3
abc|def|hij "hgfd" |
qwehjr|aweqwejkl||

For a background on parsing (optionally) quoted delimited fields, including different quoting characters (', "), see here:

For a very, very, very complete example complete with support for partially quoted values and a

splitInto(input, output, ' ');

method that takes 'arbitrary' output containers and delimiter expressions, see here: