I'm parsing log file with following format:
<line id>,<time>,<data_1>,<data_2>,<event_description>
The time is in format dd-MM-yy HH:mm:ss.fff.
I need to extract parsed time, data_1 and data_2.
Here's what I'm doing for each line:
auto unquoted_string = lexeme[+(boost::spirit::qi::char_ - ',')];
double data_1=-1, data_2=-1;
boost::fusion::vector<char> datestr;
bool r = phrase_parse(
std::begin(line),
std::end(line),
int_>>','>>unquoted_string[ref(datestr)=_1]>>',' >> double_[ref(data_1) = _1] >> ',' >> double_[ref(data_2) = _1] >>','>>unquoted_string,
boost::spirit::qi::space
);
Now I'm left with boost::fusion::vector<char> containing the datetime string that needs to be parsed. How do I convert it to std::string? Is there a better way to parse time within boost::karma/qi?
Spirit Karma is meant for generating output, not for parsing, so no you cannot use it for that.
For a job like this I'd suggest not parsing the entire date format, but instead the general form of the line as you gave it:
Let's define a recipient type:
Adapt it for automatic attribute propagation:
A simple rule for it:
And here we go:
Prints: Live On Coliru
Full Listing
Live On Coliru
BONUS
To actually parse the date-times, I'd suggest using Boost DateTime. Alternatively, look here for something based on
strptimethat's really versatile: C++ boost date_input_facet seems to parse dates unexpectedly with incorrect formats passed to the facet constructor