]> git.stg.codes - ssmd.git/blob - src/dataparser.cpp
Sync with working version
[ssmd.git] / src / dataparser.cpp
1 #include <string>
2 #include <list>
3
4 #include <boost/spirit/include/qi.hpp>
5 #include <boost/fusion/include/std_pair.hpp>
6 #include <boost/fusion/include/adapt_struct.hpp>
7
8 #include "datatypes.h"
9 #include "logger.h"
10
11 namespace qi = boost::spirit::qi;
12
13 using GTS::LineType;
14 using GTS::Lines;
15
16 BOOST_FUSION_ADAPT_STRUCT(
17     GTS::LineType,
18     (std::string, switchIP)
19     (std::string, readCommunity)
20     (std::string, writeCommunity)
21     (unsigned, uplinkPort)
22     (unsigned, userPort)
23     (std::string, mac)
24     (unsigned, upShape)
25     (unsigned, downShape)
26     (unsigned, upBurst)
27     (unsigned, downBurst)
28 )
29
30 template <typename Iterator>
31 struct LinesGrammar 
32   : qi::grammar<Iterator, Lines()>
33 {
34     LinesGrammar()
35       : LinesGrammar::base_type(query)
36     {
37         query          = +line;
38         line           =  ip        >> qi::lit(",") >>
39                           community >> qi::lit(",") >>
40                           community >> qi::lit(",") >>
41                           port      >> qi::lit(",") >>
42                           port      >> qi::lit(",") >>
43                           mac       >> qi::lit(",") >>
44                           shape     >> qi::lit(",") >>
45                           shape     >> qi::lit(",") >>
46                           shape     >> qi::lit(",") >>
47                           shape     >> eol;
48         ip             = +qi::digit >> qi::char_('.') >>
49                          +qi::digit >> qi::char_('.') >>
50                          +qi::digit >> qi::char_('.') >>
51                          +qi::digit;
52         community      = +qi::char_("a-zA-Z0-9_");
53         port           =  qi::int_;
54         mac            =  qi::xdigit >> qi::xdigit >>
55                           qi::xdigit >> qi::xdigit >>
56                           qi::xdigit >> qi::xdigit >>
57                           qi::xdigit >> qi::xdigit >>
58                           qi::xdigit >> qi::xdigit >>
59                           qi::xdigit >> qi::xdigit;
60         shape          =  qi::int_;
61         eol            =  qi::lit("\r\n") | '\r' | '\n';
62     }
63
64     qi::rule<Iterator, Lines()> query;
65     qi::rule<Iterator, LineType()> line;
66     qi::rule<Iterator, std::string()> ip, community, mac;
67     qi::rule<Iterator, unsigned()> port, shape;
68     qi::rule<Iterator> eol;
69 };
70
71 bool GTS::parseData(std::string & data, Lines & lines)
72 {
73     std::string::iterator begin(data.begin());
74     std::string::iterator end(data.end());
75
76     LinesGrammar<std::string::iterator> parser; //  Our parser
77
78     if (!qi::parse(begin, end, parser, lines)) {
79         logger << "parseData() - Failed to parse data" << std::endl;
80         return false;
81     }
82
83     return true;
84 }