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)
{}
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 )
{
@@ -86,6 +94,7 @@ public:
*it++ = empty;
}
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 );
}