#ifndef TXTPRT_IMPORT #define TXTPRT_IMPORT #include #include #include #include #include "entity.h" struct txtprt_i_exception { txtprt_i_exception (std::string m) : msg(m) { } std::string msg; }; class txtprt_parser { std::ifstream ifs; int cur_line; std::string line_data; public: txtprt_parser () { } geometry *read (std::string filename); inline void match_char (char c) { char o; match_achar (o); if (o != c) throw txtprt_i_exception ((std::string) "Fileformat invalid, expected '" + c + "' got: '" + o + "' -> '" + line_data.substr (0, 10) + "..."); } inline void match_achar (char &c) { std::string s = line_data.substr (0, 1); c = s[0]; line_data.erase (0, 1); } inline void match_space () { match_char (' '); } inline void match_nl () { match_char ('\n'); } inline void match_number (double &d) { size_t p = line_data.find_first_not_of ("0123456789.-+e"); std::string tmp = line_data.substr (0, p); line_data.erase (0, p); d = strtod (tmp.c_str (), NULL); } inline void match_section (char c, std::string §) { // c - section char, sect - name of section match_char (c); match_space (); size_t p = line_data.find_first_of ("\n"); sect = line_data.substr (0, p); line_data.erase (0, p); match_nl (); } inline void match_section (char c, double &num) { // c - section char, sect - name of section match_char (c); match_space (); match_number (num); match_nl (); } inline void match_asection (char &c, std::string §) { // c - section char, sect - name of section match_achar (c); match_space (); size_t p = line_data.find_first_of ("\n"); sect = line_data.substr (0, p); line_data.erase (0, p); match_nl (); } inline void match_asection (char &c, double &num) { // c - section char, sect - name of section match_achar (c); match_space (); match_number (num); match_nl (); } }; #endif