I've the following text:
[70000000:45]
4, 5, 6, 7
[60000000:60]
1, 2, 3, 4
[80000:90]
4, 5, 6, 7, 8, 9
The rows with square brackets contains a frequency and an angle in the form [freq:angle]
, while the subsequent row is a vector of number related to these parameters. I can have different sets of frequencies and angles, and a vector is defined for every of them.
I've the following structure:
struct Data {
std::vector<int> frequencies;
std::vector<int> elevations;
std::vector<std::vector<double>> gains;
};
I need to store in this structure the file data: in the frequencies
vector I'll have all frequencies in order, from top to bottom; in the elevations
vector I'll have the same thing but for elevation data, and in the gains
vector I'll have respective gain vectors.
The scope is that if I've an index, vector elements at this index will contain frequency, elevation and gains data related to them as in the file.
For examples, after the parsing, at index = 1
I will have
data.frequencies[1] = 60000000
data.elevations[1] = 60
data.gains[1] = {1, 2, 3, 4}
I need to parse the file in order to populate the structure. I was able to parse frequencies
and elevations
, and store them in vectors, but I cannot store gain data. I need to create a vector of vectors from these data.
Below you can find the code; after the parsing parsed
is populated with frequencies and elevation, and I need gains. What I can do in order to parse them?
#include <boost/optional/optional_io.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
const std::string file1 = R"xx(
[70000000:45]
4, 5, 6, 7
[60000000:60]
1, 2, 3, 4
[80000:90]
4, 5, 6, 7, 8, 9
)xx";
struct Data {
std::vector<int> frequencies;
std::vector<int> elevations;
std::vector<std::vector<double>> gains;
};
BOOST_FUSION_ADAPT_STRUCT(
Data,
(std::vector<int>, frequencies)
(std::vector<int>, elevations)
(std::vector<std::vector<double>>, gains)
)
template <typename It, typename Skipper = qi::space_type>
struct grammar : qi::grammar<It, Data(), Skipper> {
grammar() : grammar::base_type(start) {
auto frequencyParser = qi::int_[px::push_back(px::at_c<0>(qi::_val), qi::_1)];
auto elevationParser = qi::int_[px::push_back(px::at_c<1>(qi::_val), qi::_1)];
auto frequencyElevationParser = qi::lit('[') >> frequencyParser >> qi::lit(':') >> elevationParser >> qi::lit(']');
auto gainsParser = qi::double_ % qi::lit(','); // Problem here where I want to parse vector rows
start = *(frequencyElevationParser >> gainsParser);
}
private:
qi::rule<It, Data(), Skipper> start;
};
int main() {
using It = std::string::const_iterator;
Data parsed;
bool ok = qi::phrase_parse(file1.begin(), file1.end(), grammar<It>(), qi::space, parsed);
return 0;
}