Added handling of not existing file.
This commit is contained in:
@@ -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 );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user