madf: (Default)
[personal profile] madf
Мо кому згодиться - парсер детальної статистики Stargazer на Boost.Spirit2:

#include <string>
#include <list>

#include <boost/cstdint.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace qi = boost::spirit::qi;

struct Stat {
    std::string ip;
    int dir;
    uint64_t down;
    uint64_t up;
    double cash;
};

struct Interval {
    std::string fromTime;
    std::string toTime;
};

typedef std::list<Stat> Stats;
typedef std::pair<Interval, Stats> StatRec;
typedef std::list<StatRec> StatRecs;

BOOST_FUSION_ADAPT_STRUCT(
    Stat,
    (std::string, ip)
    (int, dir)
    (uint64_t, down)
    (uint64_t, up)
    (double, cash)
)

BOOST_FUSION_ADAPT_STRUCT(
    Interval,
    (std::string, fromTime)
    (std::string, toTime)
)

template <typename Iterator>
struct StatGrammar 
  : qi::grammar<Iterator, StatRecs()>
{
    StatGrammar()
      : StatGrammar::base_type(query)
    {
        query          = +chunk;
        chunk          =  timeline >> statlines;
        statlines      = +statline;
        timeline       =  qi::lit("->") >> space >> timevalue >> qi::lit(" - ") >> timevalue >> eol;
        statline       =  space >> ip >> space >> qi::int_ >> space >> qi::int_ >> space >> qi::int_ >> space >> qi::double_ >> eol;
        timevalue      = +qi::digit >> qi::char_('.') >> +qi::digit >> qi::char_('.') >> +qi::digit;
        ip             = +qi::digit >> qi::char_('.') >> +qi::digit >> qi::char_('.') >> +qi::digit >> qi::char_('.') >> +qi::digit;
        space          = *qi::char_("\t ");
        eol            =  qi::lit("\r\n") | '\r' | '\n';
    }

    qi::rule<Iterator, StatRecs()> query;
    qi::rule<Iterator, StatRec()> chunk;
    qi::rule<Iterator, Stats()> statlines;
    qi::rule<Iterator, Interval()> timeline;
    qi::rule<Iterator, Stat()> statline;
    qi::rule<Iterator, std::string()> ip;
    qi::rule<Iterator, std::string()> timevalue;
    qi::rule<Iterator> space, eol;
};
_Winnie C++ Colorizer

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

madf: (Default)
madf

April 2018

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 24th, 2025 01:59 am
Powered by Dreamwidth Studios