diff --git a/src/search/utils/input_file_parser.cc b/src/search/utils/input_file_parser.cc index d7d0970584..307338ecec 100644 --- a/src/search/utils/input_file_parser.cc +++ b/src/search/utils/input_file_parser.cc @@ -1,4 +1,5 @@ #include "input_file_parser.h" + #include "system.h" #include @@ -16,10 +17,8 @@ InputFileParser::InputFileParser(istream &stream) : stream(stream), context(""), only_whitespaces("\\s*") { } -InputFileParser::~InputFileParser() { -} - string InputFileParser::find_next_line(bool throw_error_on_failure) { + assert(may_start_line); // We probably forgot a confirm_end_of_line. string next_line; while (!stream.eof()) { getline(stream, next_line); @@ -35,9 +34,8 @@ string InputFileParser::find_next_line(bool throw_error_on_failure) { } void InputFileParser::initialize_tokens() { - assert(may_start_line); + assert(may_start_line()); assert(token_number == 0); - assert(tokens.empty()); assert(line != ""); istringstream stream(line); string word; @@ -48,6 +46,10 @@ void InputFileParser::initialize_tokens() { assert(tokens.size() > 0); } +bool InputFileParser::may_start_line() { + return tokens.empty(); +} + int InputFileParser::parse_int(const string &str, const string &cause) { try { string::size_type parsed_length; @@ -65,10 +67,9 @@ void InputFileParser::set_context(const string &context) { } string InputFileParser::read(const string &message) { - if (may_start_line) { + if (may_start_line()) { line = find_next_line(true); initialize_tokens(); - may_start_line = false; } if (token_number >= tokens.size()) { error("Unexpected end of line. Message: " + message); @@ -84,7 +85,6 @@ int InputFileParser::read_int(const string &message) { } string InputFileParser::read_line(const string &message) { - assert(may_start_line); // We probably forgot a confirm_end_of_line. line = find_next_line(true); return line; } @@ -102,11 +102,10 @@ void InputFileParser::read_magic_line(const string &magic) { } void InputFileParser::confirm_end_of_line() { - if (may_start_line) { + if (may_start_line()) { return; } if (token_number == tokens.size()) { - may_start_line = true; token_number = 0; tokens.clear(); } else { @@ -116,7 +115,6 @@ void InputFileParser::confirm_end_of_line() { } void InputFileParser::confirm_end_of_file() { - assert(may_start_line); string next_line = find_next_line(false); if(next_line != "") { error("Expected end of file, found non-empty line " + next_line); @@ -132,8 +130,7 @@ void InputFileParser::error(const string &message) const { if (context != "") { cerr << "Context: " << context << endl; } - cerr << message << endl - << "Exiting." << endl; + cerr << message << endl << "Exiting." << endl; utils::exit_with(ExitCode::SEARCH_INPUT_ERROR); } } diff --git a/src/search/utils/input_file_parser.h b/src/search/utils/input_file_parser.h index dae1f28910..422e7cab22 100644 --- a/src/search/utils/input_file_parser.h +++ b/src/search/utils/input_file_parser.h @@ -16,11 +16,13 @@ class InputFileParser { size_t token_number; std::string line; std::vector tokens; - bool may_start_line = true; const std::regex only_whitespaces; + std::string find_next_line(bool throw_error_on_failure=true); + void initialize_tokens(); + bool may_start_line(); + int parse_int(const std::string &str, const std::string &cause); public: explicit InputFileParser(std::istream &stream); - ~InputFileParser(); /* Set context for error reporting. @@ -28,28 +30,34 @@ class InputFileParser { void set_context(const std::string &context); /* Read a single token within a line. Tokens within a line are - separated by arbitrary whitespaces. Set curser to the end of the - read token. + separated by arbitrary whitespaces. Report error if the current + line does not contain a token after the cursor position. Set + cursor to the end of the read token. */ - std::string read(const std::string &message); // TODO: templates + std::string read(const std::string &message); /* Read a single token in a line as integer, analoguously to read(...). Report an error if the token is not a string of digits representing an int. */ - int read_int(const std::string &message); // TODO: templates + int read_int(const std::string &message); /* - Read a complete line as a single string. Set cursor to the - beginning of the next line. + Read a complete line as a single string token. Report an error if + the cursor is not at the beginning of a line before reading. Set + cursor to the beginning of the next line. */ - std::string read_line(const std::string &message); // TODO: templates + std::string read_line(const std::string &message); /* - Read a complete line as a single integer. Report an error if the - line is not a string of digits representing an int. + Read a complete line as a single integer token, analogously to + read_line(...). Report an error if the line is not a string of + digits representing an int. */ - int read_line_int(const std::string &message); // TODO: templates + int read_line_int(const std::string &message); /* - Read a complete line and compare it to a *magic* string. + Read a complete line and compare it to a *magic* string. Report an + error if the cursor is not at the beginning of a line before + reading. Report an error if the content of the line is not equal + to the *magic* string. */ void read_magic_line(const std::string &magic); /* @@ -63,10 +71,6 @@ class InputFileParser { void confirm_end_of_file(); // TODO: Should this be public at all? Or should we add a get_line method? void error(const std::string &message) const; -private: - std::string find_next_line(bool throw_error_on_failure=true); - void initialize_tokens(); - int parse_int(const std::string &str, const std::string &cause); }; } #endif