![]() |
![]() |
00001 //#include <iostream> 00002 #include "CondCore/Utilities/interface/CSVDataLineParser.h" 00003 #include <boost/spirit/core.hpp> 00004 #include <boost/spirit/utility/confix.hpp> 00005 #include <boost/spirit/utility/escape_char.hpp> 00006 using namespace boost::spirit; 00007 00008 struct generic_actor{ 00009 std::vector<boost::any>& vec_; 00010 generic_actor(std::vector<boost::any>& vec ):vec_(vec){} 00011 template <typename ActionIteratorT> 00012 void operator ()(ActionIteratorT const& first, 00013 ActionIteratorT const& last) const{ 00014 vec_.push_back(std::string(first, last-first)); 00015 } 00016 void operator ()(int val) const{ 00017 vec_.push_back(val); 00018 } 00019 void operator ()(double val) const{ 00020 vec_.push_back(val); 00021 } 00022 }; 00023 00024 bool CSVDataLineParser::parse( const std::string& inputLine){ 00025 if(inputLine.empty()) return true; 00026 m_result.clear(); 00027 boost::spirit::rule<> item_parser, list_parser,strparser,numparser; 00028 strparser=confix_p('\"',*c_escape_ch_p, '\"')[generic_actor(m_result)]; 00029 numparser=(strict_real_p)[generic_actor(m_result)] | int_p[generic_actor(m_result)]; 00030 list_parser=(numparser|strparser) >>*(ch_p(',')>>(numparser|strparser)); 00031 parse_info<> result=boost::spirit::parse(inputLine.c_str(),list_parser); 00032 if(result.full){ 00033 return true; 00034 } 00035 return false; 00036 } 00037 00038 std::vector<boost::any> CSVDataLineParser::result() const{ 00039 return m_result; 00040 }