boost qi :: phrase_parse只读取第一个元素

时间:2015-04-26 20:43:32

标签: c++ boost boost-spirit-qi

我使用boost::spirit实现了简单的ascii解析器。 目标ascii文件看起来像

n

0 23 45 10.0 0.5

.....

n-1 x y .....

但它只在measure_list中返回1个元素

如果我尝试将ASCII读作简单的vector<double>而不是结构化的例子 - 它可以正常工作。怎么了?

struct measure
{
   int id;
   double x, y, size_, angle;
} 

BOOST_FUSION_ADAPT_STRUCT(measure, (int, id)(double, x)(double, y)(double, size_)(double, angel))

typedef std::vector<measure> data_t;

void RelativeMeasure(string filename)
        {
                clear();

                if(!filesystem::exists(filename)) return;

                file_name = filename;



                ifstream calibration_file(filename);

                if(calibration_file.is_open())
                {
                        int key_count;
                        calibration_file >> key_count;

                        istreambuf_iterator<char> eos;
                        istreambuf_iterator<char> it(calibration_file);

                        std::string strver(it, eos);

                        std::vector<measure> measure_list;
                        measure_list.reserve(100000);

                        qi::phrase_parse(strver.begin(), strver.end(), (qi::int_ > qi::double_ > qi::double_ > qi::double_ > qi::double_) % qi::eol, qi::blank, measure_list);

                        for each(auto measure in measure_list) key_list.push_back(KeyPoint(measure.x, measure.y, measure.size_, measure.angel));
}

1 个答案:

答案 0 :(得分:0)

我看到的最可能的罪魁祸首是你在n之后不吃换行符。也许也使用+qi::eol作为分隔符。

但这并不能解释你是否读过第一个条目。

您可以使用流API(使用boost::spirit::istream_iterator多通道适配器)来简化操作:

<强> Live On Coliru

void RelativeMeasure(std::string filename)
{
    std::ifstream calfile(filename, std::ios::binary);

    int key_count;
    std::vector<measure> measure_list;

    using namespace qi;
    if (
        calfile >> std::noskipws >> phrase_match(int_, blank, key_count)
        && calfile >> phrase_match(qi::repeat(key_count)[int_ > double_ > double_ > double_ > double_ > +eol], blank, measure_list)
    )
    {
        std::vector<KeyPoint> key_list;
        key_list.reserve(measure_list.size());
        // using a converting constructor (why not parse into this at once?)
        std::copy(measure_list.begin(), measure_list.end(), back_inserter(key_list));
    }
}