Skip to content

Commit

Permalink
Expose cause for #3
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Apr 11, 2017
1 parent 1546a67 commit 9de81f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
20 changes: 17 additions & 3 deletions solver.cc
@@ -1,4 +1,5 @@
#include "solver.h"
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <iostream>
Expand All @@ -7,6 +8,7 @@
#include <algorithm>

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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions solver.h
Expand Up @@ -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_
10 changes: 10 additions & 0 deletions table.cc
@@ -1,3 +1,4 @@
#include <cassert>
#include "common.h"
#include "table.h"

Expand Down Expand Up @@ -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);
Expand All @@ -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<width_; i++){
Expand All @@ -219,7 +224,10 @@ bool Table::Init(std::string filename){
int value;
while ( linestream >> 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);
Expand All @@ -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);
Expand Down

0 comments on commit 9de81f5

Please sign in to comment.