diff --git a/modules/io/src/mol/star_parser.cc b/modules/io/src/mol/star_parser.cc index b6f370d8e2321e68cd94a57d4ccb795a96895b53..834944c8aab3edc8180cc7097d9d21a2bbabd897 100644 --- a/modules/io/src/mol/star_parser.cc +++ b/modules/io/src/mol/star_parser.cc @@ -30,18 +30,27 @@ namespace ost { namespace io { StarParser::StarParser(std::istream& stream, bool items_as_row): stream_(stream), 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 (!fstream_) { + file_open_ = false; + } } StarParser::StarParser(const String& filename, bool items_as_row): fstream_(filename.c_str()), stream_(fstream_), 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; + + if (!fstream_) { + file_open_ = false; + } } String StarParser::FormatDiagnostic(StarDiagType type, const String& message, @@ -469,6 +478,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 96da40e73ed8126cc443ce66f971bcfd2e910750..b0ef784b76c5633487e7c113398d8c6e8751bf03 100644 --- a/modules/io/src/mol/star_parser.hh +++ b/modules/io/src/mol/star_parser.hh @@ -245,6 +245,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();