make really sure load_mupa has a file to parse.
authorDiane Trout <diane@caltech.edu>
Sat, 9 Sep 2006 00:08:34 +0000 (00:08 +0000)
committerDiane Trout <diane@caltech.edu>
Sat, 9 Sep 2006 00:08:34 +0000 (00:08 +0000)
Fixes ticket:121
This includes more filesystem checks to make sure we're reading something
that's a file, and instead of doing fstream.eof() this uses the more
general bool converter for the stream. Which I think is equivalent to
fstream::good() (a more general test than fstream::eof() )

alg/mussa.cpp
alg/test/test_mussa.cpp

index f87673a35a2c0afb614530542563b77362d7d78a..6990db7bac926b554651cc151f4959c12c5e9a0e 100644 (file)
@@ -316,8 +316,14 @@ Mussa::load_mupa_file(fs::path para_file_path)
   clear();
 
   // if file was opened, read the parameter values
-  if (fs::exists(para_file_path))
+  if (not fs::exists(para_file_path))
   {
+    throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
+  } else if (fs::is_directory(para_file_path)) {
+    throw mussa_load_error("Config File: " + para_file_path.string() + " is a directory.");
+  } else if (fs::is_empty(para_file_path)) {
+    throw mussa_load_error("Config File: " + para_file_path.string() + " is empty");
+  } else  {
     para_file.open(para_file_path, ios::in);
 
     // what directory is the mupa file in?
@@ -329,7 +335,7 @@ Mussa::load_mupa_file(fs::path para_file_path)
     param = file_data_line.substr(0,split_index);
     value = file_data_line.substr(split_index+1);
     
-    while (!para_file.eof())
+    while (para_file)
     {
       did_seq = false;
       if (param == "ANA_NAME")
@@ -354,7 +360,7 @@ Mussa::load_mupa_file(fs::path para_file_path)
         sub_seq_end = 0;
         seq_params = true;
 
-        while ((!para_file.eof()) && seq_params)
+        while (para_file && seq_params)
         {
           getline(para_file,file_data_line);
           split_index = file_data_line.find(" ");
@@ -405,10 +411,6 @@ Mussa::load_mupa_file(fs::path para_file_path)
     //     << " threshold = " << threshold << endl;
   }
   // no file was loaded, signal error
-  else
-  {
-    throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
-  }
   dirty = true;
 }
 
index 3a850367f1d9b109a1f23cde12ac3285a05b7081..d8b2ab51fdbf00b3d43db1f6387c25c5ee09c2a6 100644 (file)
@@ -11,6 +11,7 @@ namespace assign = boost::assign;
 #include <vector>
 
 #include "alg/mussa.hpp"
+#include "mussa_exceptions.hpp"
 
 using namespace std;
 
@@ -119,6 +120,13 @@ BOOST_AUTO_TEST_CASE( mussa_load_full_path )
   BOOST_CHECK_EQUAL( m1.get_threshold(), 20);
 }
 
+BOOST_AUTO_TEST_CASE( mussa_mupa_directory )
+{
+  fs::path curdir(".");
+  Mussa m1;
+  BOOST_CHECK_THROW(m1.load_mupa_file( curdir ), mussa_load_error );
+}
+
 BOOST_AUTO_TEST_CASE( mussa_load_analysis )
 {
   fs::path example_dir(EXAMPLE_DIR, fs::native);