// Copyright (c) 2010 Peter Schueller // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; // parse list of doubles from input stream // throw exception (perhaps including filename) on errors std::vector parse(std::istream& input, const std::string& filename); // main function int main(int, char**) { try { parse(std::cin, "STDIN"); } catch(const std::exception& e) { std::cerr << "Exception: " << e.what() << std::endl; return -1; } return 0; } // implementation std::vector parse(std::istream& input, const std::string& filename) { // get input into string std::ostringstream buf; buf << input.rdbuf(); std::string str = buf.str(); // prepare iterators std::string::iterator first = str.begin(); std::string::iterator last = str.end(); // prepare output std::vector output; // parse bool r = qi::phrase_parse( first, last, // iterators over input qi::double_ >> *(',' >> qi::double_) >> qi::eoi, // recognize list of doubles ascii::space | '#' >> *(ascii::char_ - qi::eol) >> qi::eol, // comment skipper output); // store into this object // error detection if( !r || first != last ) throw std::runtime_error("parse error in "+filename); // return result return output; }