diff --git a/modules/io/src/mol/star_parser.cc b/modules/io/src/mol/star_parser.cc index dce4dea150f99f9803dacc011a0e16fac7d169d9..4da41b237b9c72f5bcedd64f0c34c7ac82430f23 100644 --- a/modules/io/src/mol/star_parser.cc +++ b/modules/io/src/mol/star_parser.cc @@ -33,18 +33,23 @@ namespace ost { namespace io { StarParser::StarParser(std::istream& stream, bool items_as_row): filename_("<stream>"), line_num_(0), has_current_line_(false), current_line_(), - items_row_header_(), items_row_columns_(), + items_row_header_(), file_open_(true), items_row_columns_(), items_row_values_() { items_as_row_ = items_as_row; + if (!stream) { + file_open_ = false; + } + stream_.push(stream); } StarParser::StarParser(const String& filename, bool items_as_row): fstream_(filename.c_str()), filename_(filename), line_num_(0), has_current_line_(false), current_line_(), - items_row_header_(), items_row_columns_(), items_row_values_() + items_row_header_(), file_open_(true), items_row_columns_(), + items_row_values_() { items_as_row_ = items_as_row; @@ -54,6 +59,10 @@ StarParser::StarParser(const String& filename, bool items_as_row): } stream_.push(fstream_); + + if (!fstream_) { + file_open_ = false; + } } String StarParser::FormatDiagnostic(StarDiagType type, const String& message, @@ -536,6 +545,11 @@ void StarParser::ParseGlobal() void StarParser::Parse() { + if (!file_open_) { + throw IOException(this->FormatDiagnostic(STAR_DIAG_ERROR, + "Failed to open file '" + + filename_ + "'!")); + } StringRef line; std::stringstream ss; while (this->GetLine(line)) { diff --git a/modules/io/src/mol/star_parser.hh b/modules/io/src/mol/star_parser.hh index 75feb7adf42de84bbddb6f4559064ce0b7d1e640..c5febf7fb3c2672c2381cdac72c178e9e93beb9a 100644 --- a/modules/io/src/mol/star_parser.hh +++ b/modules/io/src/mol/star_parser.hh @@ -277,6 +277,7 @@ private: String current_line_; bool items_as_row_; StarLoopDesc items_row_header_; + bool file_open_; std::vector<StringRef> items_row_columns_; std::vector<String> items_row_values_; }; diff --git a/modules/io/tests/test_star_parser.cc b/modules/io/tests/test_star_parser.cc index e72edb632d81900330f36beec23b835ed179396c..dd56aaa4cd5df3976339443bc6ae38015bd8bcb5 100644 --- a/modules/io/tests/test_star_parser.cc +++ b/modules/io/tests/test_star_parser.cc @@ -238,11 +238,12 @@ BOOST_AUTO_TEST_CASE(star_data_item) BOOST_MESSAGE(" Running star_data_item tests..."); std::ifstream s("testfiles/data-item.cif"); DataItemTestParser star_p(s); - star_p.Parse(); + BOOST_CHECK_NO_THROW(star_p.Parse()); BOOST_CHECK_EQUAL(star_p.s1, "a"); BOOST_CHECK_EQUAL(star_p.s2, "a b c"); BOOST_CHECK_EQUAL(star_p.s3, "a\nb\nc"); BOOST_CHECK_EQUAL(star_p.s4, "a'b"); + BOOST_MESSAGE(" done."); } BOOST_AUTO_TEST_CASE(format_diag_stream) @@ -450,5 +451,14 @@ BOOST_AUTO_TEST_CASE(star_try_bool_conversions) IOException); BOOST_MESSAGE(" done."); } + +BOOST_AUTO_TEST_CASE(star_wrong_filename) +{ + BOOST_MESSAGE(" Running star_wrong_filename tests..."); + DataItemTestParser star_p("testfiles/doesnotexist.foo"); + BOOST_CHECK_THROW(star_p.Parse(), IOException); + BOOST_MESSAGE(" done."); +} + BOOST_AUTO_TEST_SUITE_END();