triclosan
triclosan

Reputation: 5714

Parsing nested data in boost-spirit

I need parse some text-tree :

std::string data = "<delimiter>field1a fieald1b fieald1c<delimiter1>subfield11<delimiter1>subfieald12<delimiter1>subfieald13 ... <delimiter>field2a fieald2b fieald2c<delimiter1>subfield21<delimiter1>subfieald22<delimiter1>subfieald23 ..."

where <delimiter>,<delimiter1> is part of std::string not a single char

It is possible tokenize this string with boost::spirit?

Upvotes: 1

Views: 740

Answers (2)

hkaiser
hkaiser

Reputation: 11521

The list parser is you friend:

namespace qi = boost::spirit::qi;

// tokenize on '<delimiter1>' and return the vector
rule<std::string::iterator, qi::space_type, std::vector<std::string>()> fields =
    *(char_ - "<delimiter1>") % "<delimiter1>";

std::string data("<delimiter>field1a fieald1b ...");
std::vector<std::vector<std::string> > fields_data;

// tokenize of '<delimiter>' and return a vector of vectors
qi::phrase_parse(data.begin(), data.end(), 
    fields % "<delimiter>", qi::space, fields_data);

You might need a recent version of Spirit for this to work (Boost V1.47 or SVN trunk).

Upvotes: 3

Elemental
Elemental

Reputation: 7466

Yes you could use spirit to do this format but it seems to me to be much more than you need.

I would just code the tokenise myself directly using std string functions. Alternately boost:regex should do this very easily for you.

Upvotes: 2

Related Questions