#include #include #include #include #include #include #include #include #include #include #include #include #include #include template struct test_generator: boost::spirit::karma::grammar { test_generator(): test_generator::base_type(start) { using boost::spirit::big_word; using boost::spirit::byte_; start = ( big_word(2007) << byte_(12) << byte_(31) << byte_(12) << byte_(30) << byte_(5) ) ; } boost::spirit::karma::rule start; }; template struct test_parser: boost::spirit::qi::grammar { test_parser(): test_parser::base_type(start) { using boost::spirit::big_word; using boost::spirit::byte_; using boost::spirit::_1; using boost::spirit::_val; using boost::spirit::_a; using boost::spirit::_b; using boost::spirit::_c; using boost::phoenix::construct; date = (big_word[_a = _1] >> byte_[_b = _1] >> byte_[_c = _1]) [_val = construct(_a, _b, _c)] ; time = (byte_[_a = _1] >> byte_[_b = _1] >> byte_[_c = _1]) [_val = construct(_a, _b, _c)] ; start = ( date[std::cout << _1 << std::endl] >> time[std::cout << _1 << std::endl] ) ; } boost::spirit::qi::rule< Iterator , boost::gregorian::date() , boost::spirit::qi::locals< uint16_t // year, refer to as _a , uint8_t // month, refer to as _b , uint8_t // day, refer to as c > > date; boost::spirit::qi::rule< Iterator , boost::posix_time::time_duration() , boost::spirit::qi::locals< uint8_t // hour, refer to as _a , uint8_t // minute, refer to as _b , uint8_t // second, refer to as c > > time; boost::spirit::qi::rule start; }; int main() { typedef boost::spirit::multi_pass< std::istreambuf_iterator > input_iterator_type; typedef std::ostream_iterator output_iterator_type; std::string filename("date_time.bin"); std::ofstream out(filename.c_str(), std::ios::binary | std::ios::trunc); if(!out) { std::cerr << "Failed to open output file\n"; return 1; } typedef test_generator test_generator; test_generator pg; output_iterator_type oi(out); bool result = boost::spirit::karma::generate(oi, pg); out.close(); std::ifstream in(filename.c_str(), std::ios::binary); if(!in) { std::cerr << "Failed to open input file\n"; return 1; } input_iterator_type first = input_iterator_type(in); input_iterator_type last = input_iterator_type(); typedef test_parser test_parser; test_parser pp; bool r = parse(first, last, pp); if(!r || first != last) { std::string rest(first, last); std::cerr << "Failed parsing at: 0x" << std::hex << (int)rest[0] << std::dec << std::endl; in.close(); return 1; } in.close(); return 0; }