Re[3]: boost::spirit и структура
От: Аноним  
Дата: 14.05.09 09:42
Оценка: 3 (1)
TSP>Во всей документации не найти тривиального считывания данных в структурку.

Плохо ищите мне кажется
вот вам мой примерчег


#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/classic_assign_actor.hpp>
#include <boost/spirit/include/classic_insert_at_actor.hpp>
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>

using namespace BOOST_SPIRIT_CLASSIC_NS;

typedef struct {
    int m_id;
    std::string m_text;
    float m_value;
} my_struct_t;

typedef std::vector<my_struct_t> ms_array_t;

int main()
{
    my_struct_t ms;
    ms_array_t array;

    const std::string text = "1;Gopa1;2.50|2;Gopa2;2.59|3;Gopa3;2.54|4;Gopa4;2.56";

    rule<> my_rule = int_p[assign_a(ms.m_id)] >> ch_p(";") >>
            (+(~ch_p(";")))[assign_a(ms.m_text)] >> ch_p(";") >> real_p[assign_a(ms.m_value)];

    rule<> my_rule2 = my_rule[push_back_a(array, ms)];

    rule<> my_rule3 = my_rule2 >> *(ch_p("|") >> my_rule2);

    if (parse(text.c_str(), my_rule3).full) {
        BOOST_FOREACH(my_struct_t ms_, array) {
            std::cout << "Id " << ms_.m_id << std::endl;
            std::cout << "Text " << ms_.m_text << std::endl;
            std::cout << "Value " << ms_.m_value << std::endl;
        }
    }

    return 0;
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.