C++ (Qt)#include <boost/config/warning_disable.hpp>#include <boost/spirit/include/qi.hpp>#include <boost/spirit/include/phoenix_core.hpp>#include <boost/spirit/include/phoenix_operator.hpp>#include <QString> #include <iostream>#include <string> constexpr int nodeDimension = 3;struct ParsedNode{ uint number = -1; std::array<float, nodeDimension> coordinates = {{-1, -1, -1}};}; template <typename Iterator>bool parse_hypermesh(Iterator first, Iterator last, ParsedNode& node){ using boost::spirit::qi::uint_; using boost::spirit::qi::float_; using boost::spirit::qi::_1; using boost::spirit::qi::phrase_parse; using boost::spirit::ascii::space; using boost::phoenix::ref; uint nodeNumber; float x; float y; float z; bool result = phrase_parse(first, last, ( "*node(" >> uint_[ref(nodeNumber) = _1] >> ',' >> float_[ref(x) = _1] >> ',' >> float_[ref(y) = _1] >> ',' >> float_[ref(z) = _1] >> ',' >> *(uint_ >> ',') >> uint_ >> ')' ), space); if (!result || first != last){ return false; } node.number = nodeNumber; node.coordinates = {x, y, z}; return result;} int main(){ const QString testString("*node( 363484 , 232.7625 , -113.996 , 40.69956 , 0 , 0 , 0 , 0 , 0 )"); const std::wstring& convertedTestString = testString.toStdWString(); ParsedNode node; if (parse_hypermesh(convertedTestString.begin(), convertedTestString.end(), node)) { std::cout << "Parsing succeeded\n"; } else { std::cout << "Parsing failed\n"; } return 0;}
C++ (Qt)uint nodeNumber; float x; float y; float z;
C++ (Qt)using namespace boost::spirit;namespace phx = boost::phoenix;namespace sw = boost::spirit::standard_wide; struct Node{ static constexpr uint nodeDimension = 3; uint number = -1; std::array<float, nodeDimension> coordinates = {{-1, -1, -1}};}; template <typename Iterator>bool parse_hypermesh(Iterator first, Iterator last, Node& node){ return qi::phrase_parse(first, last, ( qi::lit("*node(") >> qi::uint_[phx::ref(node.number) = _1] >> ',' >> qi::float_[phx::ref(node.coordinates[0]) = _1] >> ',' >> qi::float_[phx::ref(node.coordinates[1]) = _1] >> ',' >> qi::float_[phx::ref(node.coordinates[2]) = _1] >> ',' >> *(qi::uint_ >> ',') >> qi::uint_ >> ')' ), sw::space) && first == last;} int main(){ std::string str = "*node( 363484 , 232.7625 , -113.996 , 40.69956 , 0 , 0 , 0 , 0 , 0 )"; Node node; if (parse_hypermesh(str.begin(), str.end(), node)) { std::cout << "Parsing succeeded\n"; std::cout << node.number << std::endl; for (auto x : node.coordinates) std::cout << x << std::endl; } else { std::cout << "Parsing failed\n"; } return 0;}
C++ (Qt)template <class ForwardIterator, class Skipper>class NodeParser : public qi::grammar<ForwardIterator, Node(), Skipper>{...}; template <class ForwardIterator, class Container>bool node_parse(ForwardIterator begin, ForwardIterator end, Container & container){ return qi::phrase_parse(begin, end, *NodeParser<ForwardIterator, sw::space_type>(), sw::space, container);}
C++ (Qt)typedef boost::spirit::istream_iterator istream_iterator_type; std::ifstream in(filename); in.unsetf( std::ios_base::skipws ); istream_iterator_type beg( in ); istream_iterator_type end; std::vector<node> container; node_parse(beg, end, container);
C++ (Qt)#include <stdio.h> int main( void ){ int num, last = 0; float val[3] = { 1, 2, 3 }; const char * fmt = "node ( %d , %f, %f, %f ) %d"; const char * test = "node ( 363484 ,232.7625, -113.996, 40.69956)777";// const char * test = "node(363484,232.7625,-113.996,40.69956)777"; int count = sscanf(test, fmt, &num, val, val + 1, val + 2, &last); printf("Ok = %d\n", int(last == 777)); printf("%d (%f, %f, %f)\n", num, val[0], val[1], val[2]); return 0;}