I'm trying to parse a simple text file using boost::spirit. The text file is a line delimited list of strings. I can get it to mostly work, except for when it comes to blank lines, which I would like to skip.
I've tried several approaches, but I either stop parsing at the blank line, or I get the blank line included in my results.
Is there a way to tell my grammar to skip blank lines?
code
std::ifstream ifs("one.txt");
ifs >> std::noskipws;
std::vector< std::string > people;
if (parse(
istream_iterator(ifs),
istream_iterator(),
*(as_string[+print >> (eol | eoi)]),
people))
{
std::cout << "Size = " << people.size() << std::endl;
for (auto person : people)
{
std::cout << person << std::endl;
}
}
one.txt
Sally
Joe
Frank
Mary Ann
Bob
What I Get
Sally
Joe
Frank
Mary Ann
What I Want to Get
Sally
Joe
Frank
Mary Ann
Bob
Bonus: Can I strip leading or trailing spaces from the lines in the grammar at the same time? I need to keep the space in Mary Ann of course.
I'll refer to Boost spirit skipper issues for more background. Quick notes:
lexeme
includes whitespace inside the subexpression (but it does still pre-skip, so lines with only whitespace count as empty lines; useno_skip
if you don't want preskip to happen)+
requires at least 1 match, so empty names are not considered a nameblank
skipper skips whitespaces, but not newlines; this is because newlines are significant to your grammar. Also note that thelexeme
still keeps the internal whitespaceSee it Live On Coliru
UPDATE In response to the comment, the added complexity was due to skipping whitespace. If you are happy trimming whitespace after the fact, by all means, use
See it Live On Coliru as well