From b5ddb1e5d3e44f4f930e7286e6980d822a9f5872 Mon Sep 17 00:00:00 2001
From: Tobias Schmidt <tobias.schmidt@unibas.ch>
Date: Fri, 14 Jan 2011 14:47:17 +0100
Subject: [PATCH] sdf reader complains if file cannot be opened

---
 modules/io/src/mol/sdf_reader.cc | 20 ++++++++++++--------
 modules/io/src/mol/sdf_reader.hh |  2 +-
 modules/io/tests/test_io_sdf.cc  |  7 +++++++
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/modules/io/src/mol/sdf_reader.cc b/modules/io/src/mol/sdf_reader.cc
index 09dea4696..347da9930 100644
--- a/modules/io/src/mol/sdf_reader.cc
+++ b/modules/io/src/mol/sdf_reader.cc
@@ -36,18 +36,21 @@ namespace ost { namespace io {
 using boost::format;
 
 SDFReader::SDFReader(const String& filename)
-  : infile_(filename), instream_(infile_) {
-  this->ClearState();
+  : infile_(filename), instream_(infile_)
+{
+  this->ClearState(boost::filesystem::path(filename));
 }
 
 SDFReader::SDFReader(const boost::filesystem::path& loc)
-  : infile_(loc), instream_(infile_) {
-  this->ClearState();
+  : infile_(loc), instream_(infile_)
+{
+  this->ClearState(loc);
 }
 
 SDFReader::SDFReader(std::istream& instream)
-  : infile_(), instream_(instream) {
-  this->ClearState();
+  : infile_(), instream_(instream)
+{
+  this->ClearState(boost::filesystem::path(""));
 }
 
 // import data from provided stream
@@ -82,7 +85,7 @@ void SDFReader::Import(mol::EntityHandle& ent)
       }
       curr_chain_.SetStringProp(data_header, data_value);
     } else if (boost::iequals(line, "$$$$")) {
-      LOG_INFO("MOLECULE " << curr_chain_.GetName() << " (" << chain_count_ << ") added.")
+      LOG_VERBOSE("MOLECULE " << curr_chain_.GetName() << " (" << chain_count_ << ") added.")
       NextMolecule();
     }
   }
@@ -91,8 +94,9 @@ void SDFReader::Import(mol::EntityHandle& ent)
                << " residues, " << atom_count_ << " atoms");
 }
 
-void SDFReader::ClearState()
+void SDFReader::ClearState(const boost::filesystem::path& loc)
 {
+  if(!infile_) throw IOException("could not open "+loc.string());
   curr_chain_=mol::ChainHandle();
   curr_residue_=mol::ResidueHandle();
   chain_count_=0;
diff --git a/modules/io/src/mol/sdf_reader.hh b/modules/io/src/mol/sdf_reader.hh
index 0401e6ab7..3beacc031 100644
--- a/modules/io/src/mol/sdf_reader.hh
+++ b/modules/io/src/mol/sdf_reader.hh
@@ -39,7 +39,7 @@ public:
   void Import(mol::EntityHandle& ent);
 
 private:
-  void ClearState();
+  void ClearState(const boost::filesystem::path& loc);
   void NextMolecule();
 
   void ParseAndAddHeader(const String& line, int line_num, mol::EntityHandle& ent,
diff --git a/modules/io/tests/test_io_sdf.cc b/modules/io/tests/test_io_sdf.cc
index db108e4c5..08ae8d80c 100644
--- a/modules/io/tests/test_io_sdf.cc
+++ b/modules/io/tests/test_io_sdf.cc
@@ -151,6 +151,13 @@ BOOST_AUTO_TEST_CASE(write_sdf)
                             "testfiles/sdf/compound-out.sdf"));
 }
 
+BOOST_AUTO_TEST_CASE(nonexisting_file)
+{
+  mol::EntityHandle eh=mol::CreateEntity();
+  EntityIOSDFHandler sdfh;
+  BOOST_CHECK_THROW(sdfh.Import(eh,"non-existing-file.sdf"), IOException);
+}
+
 BOOST_AUTO_TEST_CASE(wrong_atomcount_error_sdf)
 {
   mol::EntityHandle eh=mol::CreateEntity();
-- 
GitLab