From 6c99d0a6eb1443d8e0099ab95ab81a6c3f613515 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Mon, 24 Jun 2013 01:46:42 +0400 Subject: [PATCH] Added handling of not existing file. --- compiler/compiler.cpp | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/compiler/compiler.cpp b/compiler/compiler.cpp index f857519..ecfd90a 100644 --- a/compiler/compiler.cpp +++ b/compiler/compiler.cpp @@ -32,28 +32,36 @@ public: , height(0) {} - void load( const std::string& name ) + bool load( const std::string& name ) { + // + // Open file. + // std::cout << "Loading " << name << "..." << std::flush; std::ifstream is( name ); - std::stringstream ss; + if ( !is ) + { + std::cout << "file not found." << std::endl; + return false; + } // // Read line by line and calculate width and hight. // std::string line; while ( std::getline( is, line ) ) { - ss << line << std::endl; width = std::max( width, (int)line.length() ); ++height; } // // Encode puzzle. // + is.clear(); + is.seekg(0); std::cout << "encoding..." << std::flush; content.resize(width*height); content_type::iterator it = content.begin(); - while ( std::getline( ss, line ) ) + while ( std::getline( is, line ) ) { for ( char c : line ) { @@ -85,7 +93,8 @@ public: for ( int i=0; i < width - line.length(); ++i) *it++ = empty; } - std::cout << "done" << std::endl; + std::cout << "done" << std::endl; + return true; } int size() @@ -138,6 +147,12 @@ protected: public: + ~catalog() + { + for ( auto p : puzzles ) + delete p; + } + void push( puzzle * p ) { puzzles.push_back( p ); @@ -189,12 +204,19 @@ try { catalog c; - for ( int i = 1; i <= 90; ++i ) + // + // Compile all screen_xxx.sokoban files where xxx is the level. + // + for ( int i = 1; true; ++i ) { - std::stringstream ss; + std::ostringstream ss; ss << "screen_" << i << ".sokoban"; puzzle * p = new puzzle; - p->load( ss.str()); + if ( !p->load( ss.str()) ) + { + delete p; + break; + } c.push( p ); }