From 954902a4d78ab2f0b1fda91de5ad66ea76822aba Mon Sep 17 00:00:00 2001
From: Stefan Bienert <stefan.bienert@unibas.ch>
Date: Wed, 27 Jul 2011 16:43:03 +0200
Subject: [PATCH] Made StarParser sensible to problems on file opening.

---
 modules/io/src/mol/star_parser.cc    | 18 ++++++++++++++++--
 modules/io/src/mol/star_parser.hh    |  1 +
 modules/io/tests/test_star_parser.cc | 12 +++++++++++-
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/modules/io/src/mol/star_parser.cc b/modules/io/src/mol/star_parser.cc
index b6f370d8e..834944c8a 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 96da40e73..b0ef784b7 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 e72edb632..dd56aaa4c 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();
 
-- 
GitLab