19 #ifndef INCLUDE_UTIL_SCAN_HPP
20 #define INCLUDE_UTIL_SCAN_HPP
31 Scan( std::string s, std::string format ) : i_(0), ok_(true)
34 std::string::size_type pos = 0;
35 std::string::size_type prev_pos = 0;
36 std::string::size_type npos = std::string::npos;
39 std::vector< std::pair<std::string, std::string> > literals;
42 std::string specifier1 = makeSpecifier(n);
43 std::string specifier2 = makeSpecifier(n+1);
44 pos = format.find(specifier1, prev_pos);
45 std::string literal1 = (pos == npos) ? format.substr(prev_pos) : format.substr(prev_pos, pos - prev_pos);
46 if (pos != npos) pos += specifier1.length();
48 pos = format.find(specifier2, prev_pos);
49 std::string literal2 = (pos == npos) ? format.substr(prev_pos) : format.substr(prev_pos, pos - prev_pos);
50 literals.push_back( std::make_pair( literal1, literal2 ) );
55 for (std::vector< std::pair<std::string, std::string> >::iterator it = literals.begin(); it != literals.end(); it++) {
56 if (pos != npos && pos == s.find( (*it).first, pos)) {
57 std::string::size_type a = pos + (*it).first.length();
58 std::string::size_type b = s.find( (*it).second, a );
59 values_.push_back( s.substr( a , (b == a) ? npos : b-a ) );
70 template<
typename T> Scan &operator()(T &t)
72 std::istringstream istr( getNextValue() );
89 std::string makeSpecifier(
int n )
91 std::ostringstream ostr;
96 std::string getNextValue()
98 if (i_<values_.size())
99 return values_[ i_++ ];
106 std::vector<std::string> values_;
113 #endif // ! INCLUDE_UTIL_SCAN_HPP