From 9de81f5b5a54af5b04b4d187c8a027fc861df181 Mon Sep 17 00:00:00 2001 From: richelbilderbeek Date: Tue, 11 Apr 2017 15:44:53 +0200 Subject: [PATCH] Expose cause for #3 --- solver.cc | 20 +++++++++++++++++--- solver.h | 3 +++ table.cc | 10 ++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/solver.cc b/solver.cc index fad2b27..5c80476 100644 --- a/solver.cc +++ b/solver.cc @@ -1,4 +1,5 @@ #include "solver.h" +#include #include #include #include @@ -7,6 +8,7 @@ #include Solver::Solver (std::string filename) { + assert(FileExists(filename)); // Initialize the table, read the data from the textfile bool init_ok = table_.Init(filename); if (init_ok) { @@ -32,6 +34,15 @@ Solver::Solver (std::string filename) { std::cout << "Unable to initialize table.\n"; } } + +bool FileExists(const std::string& filename) noexcept +{ + std::fstream f; + f.open(filename.c_str(),std::ios::in); + return f.is_open(); +} + + int Solver::RunLogicTilPossible(SolverSpeed algorithm) { int count = 0; int remaining = table_.NumberOfCells(); @@ -134,12 +145,15 @@ int main(int argc, char* argv[]) std::cout << "Pic-a-Pix solver 0.1\n"; if (argc > 1){ std::cout << "Initializing solver with: " << argv[1] << "\n"; + if (!FileExists(argv[1])) + { + std::cout << "File '" << argv[1] << "' cannot be found\n"; + return 1; + } Solver solver(argv[1]); } else { std::cout << "Please specify the input file\n"; } - - return 0; -} \ No newline at end of file +} diff --git a/solver.h b/solver.h index 9a58adb..abf92b9 100644 --- a/solver.h +++ b/solver.h @@ -32,4 +32,7 @@ class Solver { Table table_; }; +//Check if a file exists at the given path +bool FileExists(const std::string& filename) noexcept; + #endif // SOLVER_SOLVER_H_ diff --git a/table.cc b/table.cc index b133ccd..6419566 100644 --- a/table.cc +++ b/table.cc @@ -1,3 +1,4 @@ +#include #include "common.h" #include "table.h" @@ -199,6 +200,7 @@ bool Table::Init(std::string filename){ std::ifstream f; std::string line; f.open(filename.c_str()); + assert(f.is_open()); int lastindex = filename.find_last_of("."); raw_filename_ = filename.substr(0, lastindex); @@ -210,6 +212,9 @@ bool Table::Init(std::string filename){ // Setup width, and height linestream >> width_; linestream >> height_; + + assert(height_ > 0); + assert(height_ < 1'000'000'000); } // Setup cols for (int i=0; i> value ) { linedata.push_back(value); + assert(linedata.size() < 1'000'000); } + assert(height_ > 0); + assert(height_ < 1'000'000'000); Line* current_col = new Line(linedata, height_); current_col->set_type(kColumn); current_col->j(i); @@ -235,6 +243,8 @@ bool Table::Init(std::string filename){ while ( linestream >> value ) { linedata.push_back(value); } + assert(width_ > 0); + assert(width_ < 1'000'000'000); Line* current_row = new Line(linedata, width_); current_row->set_type(kRow); current_row->i(i);