Added handling of not existing file.

This commit is contained in:
2013-06-24 01:46:42 +04:00
parent 6f9ebd59c9
commit 6c99d0a6eb

View File

@@ -32,28 +32,36 @@ public:
, height(0) , height(0)
{} {}
void load( const std::string& name ) bool load( const std::string& name )
{ {
//
// Open file.
//
std::cout << "Loading " << name << "..." << std::flush; std::cout << "Loading " << name << "..." << std::flush;
std::ifstream is( name ); 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. // Read line by line and calculate width and hight.
// //
std::string line; std::string line;
while ( std::getline( is, line ) ) while ( std::getline( is, line ) )
{ {
ss << line << std::endl;
width = std::max( width, (int)line.length() ); width = std::max( width, (int)line.length() );
++height; ++height;
} }
// //
// Encode puzzle. // Encode puzzle.
// //
is.clear();
is.seekg(0);
std::cout << "encoding..." << std::flush; std::cout << "encoding..." << std::flush;
content.resize(width*height); content.resize(width*height);
content_type::iterator it = content.begin(); content_type::iterator it = content.begin();
while ( std::getline( ss, line ) ) while ( std::getline( is, line ) )
{ {
for ( char c : line ) for ( char c : line )
{ {
@@ -85,7 +93,8 @@ public:
for ( int i=0; i < width - line.length(); ++i) for ( int i=0; i < width - line.length(); ++i)
*it++ = empty; *it++ = empty;
} }
std::cout << "done" << std::endl; std::cout << "done" << std::endl;
return true;
} }
int size() int size()
@@ -138,6 +147,12 @@ protected:
public: public:
~catalog()
{
for ( auto p : puzzles )
delete p;
}
void push( puzzle * p ) void push( puzzle * p )
{ {
puzzles.push_back( p ); puzzles.push_back( p );
@@ -189,12 +204,19 @@ try
{ {
catalog c; 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"; ss << "screen_" << i << ".sokoban";
puzzle * p = new puzzle; puzzle * p = new puzzle;
p->load( ss.str()); if ( !p->load( ss.str()) )
{
delete p;
break;
}
c.push( p ); c.push( p );
} }