From b5fea3cb1384da2f2afd7b262737744d5e817f04 Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Sat, 9 Sep 2006 00:08:34 +0000 Subject: [PATCH] make really sure load_mupa has a file to parse. 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 | 16 +++++++++------- alg/test/test_mussa.cpp | 8 ++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/alg/mussa.cpp b/alg/mussa.cpp index f87673a..6990db7 100644 --- a/alg/mussa.cpp +++ b/alg/mussa.cpp @@ -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; } diff --git a/alg/test/test_mussa.cpp b/alg/test/test_mussa.cpp index 3a85036..d8b2ab5 100644 --- a/alg/test/test_mussa.cpp +++ b/alg/test/test_mussa.cpp @@ -11,6 +11,7 @@ namespace assign = boost::assign; #include #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); -- 2.30.2