#include #include #include #include class parser { public: typedef std::tr1::function error_callback_type; typedef std::tr1::function warning_callback_type; void set_callbacks(const warning_callback_type& warning_callback, const error_callback_type& error_callback) { warning_callback_ = warning_callback; error_callback_ = error_callback; } typedef std::tr1::function bool_callback_type; typedef std::tr1::function char_callback_type; typedef std::tr1::function short_callback_type; typedef std::tr1::function int_callback_type; typedef std::tr1::function long_callback_type; typedef std::tr1::function float_callback_type; typedef std::tr1::function double_callback_type; void set_callbacks( const bool_callback_type& bool_callback, const char_callback_type& char_callback, const short_callback_type& short_callback, const int_callback_type& int_callback, const long_callback_type& long_callback, const float_callback_type& float_callback, const double_callback_type& double_callback ) { bool_callback_ = bool_callback; char_callback_ = char_callback; short_callback_ = short_callback; int_callback_ = int_callback; long_callback_ = long_callback; float_callback_ = float_callback; double_callback_ = double_callback; } bool parse(std::istream& istream) { std::string line; std::size_t line_number = 0; while (std::getline(istream, line)) { ++line_number; std::istringstream stringstream(line); stringstream >> std::ws; if (stringstream.eof()) { } else { std::string type; stringstream >> type; if (type == "bool") { bool value; stringstream >> value; if (bool_callback_) bool_callback_(value); } else if (type == "char") { char value; stringstream >> value; if (char_callback_) char_callback_(value); } else if (type == "short") { short value; stringstream >> value; if (short_callback_) short_callback_(value); } else if (type == "int") { int value; stringstream >> value; if (int_callback_) int_callback_(value); } else if (type == "long") { long value; stringstream >> value; if (long_callback_) long_callback_(value); } else if (type == "float") { float value; stringstream >> value; if (float_callback_) float_callback_(value); } else if (type == "double") { double value; stringstream >> value; if (double_callback_) double_callback_(value); } else { if (error_callback_) { error_callback_(line_number, "parser error"); return false; } } } } return !istream.fail() && istream.eof() && !istream.bad();; } private: error_callback_type error_callback_; warning_callback_type warning_callback_; bool_callback_type bool_callback_; char_callback_type char_callback_; short_callback_type short_callback_; int_callback_type int_callback_; long_callback_type long_callback_; float_callback_type float_callback_; double_callback_type double_callback_; }; void error_callback(const std::string& filename, std::size_t line, const std::string& message) { std::cerr << filename << ":" << line << ": " << "error: " << message << std::endl; } void warning_callback(const std::string& filename, std::size_t line, const std::string& message) { std::cerr << filename << ":" << line << ": " << "warning: " << message << std::endl; } void bool_callback(bool value) { std::cout << "bool " << value << "\n"; } void char_callback(char value) { std::cout << "char " << value << "\n"; } void short_callback(short value) { std::cout << "short " << value << "\n"; } void int_callback(int value) { std::cout << "int " << value << "\n"; } void long_callback(long value) { std::cout << "long " << value << "\n"; } void float_callback(float value) { std::cout << "float " << value << "\n"; } void double_callback(double value) { std::cout << "double " << value << "\n"; } int main(int argc, char* argv[]) { std::string filename; parser p; p.set_callbacks( std::tr1::bind(error_callback, std::tr1::ref(filename), std::tr1::placeholders::_1, std::tr1::placeholders::_2), std::tr1::bind(warning_callback, std::tr1::ref(filename), std::tr1::placeholders::_1, std::tr1::placeholders::_2) ); p.set_callbacks( bool_callback, char_callback, short_callback, int_callback, long_callback, float_callback, double_callback ); filename = "stringstream"; std::stringstream stringstream("bool 1\nchar 2\nshort 3\nint 4\nlong 5\nfloat 6.0\ndouble 7.0"); p.parse(stringstream); }