mirror of
https://github.com/boostorg/exception.git
synced 2025-07-02 23:36:47 +02:00
fixed compile errors, removed tabs as required.
[SVN r44114]
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
//
|
||||
//The documentation for boost::exception can be found at:
|
||||
//
|
||||
// http://www.revergestudios.com/boost-exception/boost-exception.htm.
|
||||
// http://www.revergestudios.com/boost-exception/boost-exception.htm.
|
||||
//
|
||||
//The output from this program can vary depending on the compiler/OS combination.
|
||||
|
||||
@ -33,23 +33,23 @@ size_t const data_size = sizeof(data);
|
||||
|
||||
class
|
||||
error: //Base for all exception objects we throw.
|
||||
public std::exception,
|
||||
public boost::exception
|
||||
{
|
||||
public:
|
||||
public std::exception,
|
||||
public boost::exception
|
||||
{
|
||||
public:
|
||||
|
||||
char const *
|
||||
what() const throw()
|
||||
{
|
||||
return boost::exception::what();
|
||||
}
|
||||
char const *
|
||||
what() const throw()
|
||||
{
|
||||
return boost::exception::what();
|
||||
}
|
||||
|
||||
protected:
|
||||
protected:
|
||||
|
||||
~error() throw()
|
||||
{
|
||||
}
|
||||
};
|
||||
~error() throw()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class open_error: public error { };
|
||||
class read_error: public error { };
|
||||
@ -62,154 +62,154 @@ class fwrite_error: public write_error { };
|
||||
|
||||
boost::shared_ptr<FILE>
|
||||
my_fopen( char const * name, char const * mode )
|
||||
{
|
||||
if( FILE * f = ::fopen(name,mode) )
|
||||
return boost::shared_ptr<FILE>(f,fclose);
|
||||
else
|
||||
throw fopen_error() << BOOST_ERROR_INFO <<
|
||||
errno_info(errno) <<
|
||||
file_name_info(name) <<
|
||||
open_mode_info(mode) <<
|
||||
function_info("fopen");
|
||||
}
|
||||
{
|
||||
if( FILE * f = ::fopen(name,mode) )
|
||||
return boost::shared_ptr<FILE>(f,fclose);
|
||||
else
|
||||
throw fopen_error() << BOOST_ERROR_INFO <<
|
||||
errno_info(errno) <<
|
||||
file_name_info(name) <<
|
||||
open_mode_info(mode) <<
|
||||
function_info("fopen");
|
||||
}
|
||||
|
||||
void
|
||||
my_fread( void * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
|
||||
{
|
||||
assert(stream);
|
||||
if( count!=fread(buffer,size,count,stream.get()) || ferror(stream.get()) )
|
||||
throw fread_error() << BOOST_ERROR_INFO <<
|
||||
function_info("fread") <<
|
||||
errno_info(errno) <<
|
||||
file_stream_info(boost::weak_ptr<FILE>(stream));
|
||||
}
|
||||
{
|
||||
assert(stream);
|
||||
if( count!=fread(buffer,size,count,stream.get()) || ferror(stream.get()) )
|
||||
throw fread_error() << BOOST_ERROR_INFO <<
|
||||
function_info("fread") <<
|
||||
errno_info(errno) <<
|
||||
file_stream_info(boost::weak_ptr<FILE>(stream));
|
||||
}
|
||||
|
||||
void
|
||||
my_fwrite( void const * buffer, size_t size, size_t count, boost::shared_ptr<FILE> const & stream )
|
||||
{
|
||||
assert(stream);
|
||||
if( count!=fwrite(buffer,size,count,stream.get()) || ferror(stream.get()) )
|
||||
throw fwrite_error() << BOOST_ERROR_INFO <<
|
||||
function_info("fwrite") <<
|
||||
errno_info(errno) <<
|
||||
file_stream_info(boost::weak_ptr<FILE>(stream));
|
||||
}
|
||||
{
|
||||
assert(stream);
|
||||
if( count!=fwrite(buffer,size,count,stream.get()) || ferror(stream.get()) )
|
||||
throw fwrite_error() << BOOST_ERROR_INFO <<
|
||||
function_info("fwrite") <<
|
||||
errno_info(errno) <<
|
||||
file_stream_info(boost::weak_ptr<FILE>(stream));
|
||||
}
|
||||
|
||||
void
|
||||
reset_file( char const * file_name )
|
||||
{
|
||||
(void) my_fopen(file_name,"wb");
|
||||
}
|
||||
{
|
||||
(void) my_fopen(file_name,"wb");
|
||||
}
|
||||
|
||||
void
|
||||
create_data( char const * file_name )
|
||||
{
|
||||
boost::shared_ptr<FILE> f = my_fopen(file_name,"wb");
|
||||
my_fwrite( data, 1, data_size, f );
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<FILE> f = my_fopen(file_name,"wb");
|
||||
my_fwrite( data, 1, data_size, f );
|
||||
}
|
||||
|
||||
void
|
||||
copy_data( char const * src_file_name, char const * dst_file_name )
|
||||
{
|
||||
boost::shared_ptr<FILE> src = my_fopen(src_file_name,"rb");
|
||||
boost::shared_ptr<FILE> dst = my_fopen(dst_file_name,"wb");
|
||||
try
|
||||
{
|
||||
char buffer[data_size];
|
||||
my_fread( buffer, 1, data_size, src );
|
||||
my_fwrite( buffer, 1, data_size, dst );
|
||||
}
|
||||
catch(
|
||||
boost::exception & x )
|
||||
{
|
||||
if( boost::shared_ptr<boost::weak_ptr<FILE> const> f=boost::get_error_info<file_stream_info>(x) )
|
||||
if( boost::shared_ptr<FILE> fs = f->lock() )
|
||||
{
|
||||
if( fs==src )
|
||||
x << file_name_info(src_file_name);
|
||||
else if( fs==dst )
|
||||
x << file_name_info(dst_file_name);
|
||||
}
|
||||
x <<
|
||||
file_name_src_info(src_file_name) <<
|
||||
file_name_dst_info(dst_file_name);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
{
|
||||
boost::shared_ptr<FILE> src = my_fopen(src_file_name,"rb");
|
||||
boost::shared_ptr<FILE> dst = my_fopen(dst_file_name,"wb");
|
||||
try
|
||||
{
|
||||
char buffer[data_size];
|
||||
my_fread( buffer, 1, data_size, src );
|
||||
my_fwrite( buffer, 1, data_size, dst );
|
||||
}
|
||||
catch(
|
||||
boost::exception & x )
|
||||
{
|
||||
if( boost::shared_ptr<boost::weak_ptr<FILE> const> f=boost::get_error_info<file_stream_info>(x) )
|
||||
if( boost::shared_ptr<FILE> fs = f->lock() )
|
||||
{
|
||||
if( fs==src )
|
||||
x << file_name_info(src_file_name);
|
||||
else if( fs==dst )
|
||||
x << file_name_info(dst_file_name);
|
||||
}
|
||||
x <<
|
||||
file_name_src_info(src_file_name) <<
|
||||
file_name_dst_info(dst_file_name);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
dump_copy_info( boost::exception const & x )
|
||||
{
|
||||
if( boost::shared_ptr<std::string const> src = boost::get_error_info<file_name_src_info>(x) )
|
||||
std::cout << "Source file name: " << *src << "\n";
|
||||
if( boost::shared_ptr<std::string const> dst = boost::get_error_info<file_name_dst_info>(x) )
|
||||
std::cout << "Destination file name: " << *dst << "\n";
|
||||
}
|
||||
{
|
||||
if( boost::shared_ptr<std::string const> src = boost::get_error_info<file_name_src_info>(x) )
|
||||
std::cout << "Source file name: " << *src << "\n";
|
||||
if( boost::shared_ptr<std::string const> dst = boost::get_error_info<file_name_dst_info>(x) )
|
||||
std::cout << "Destination file name: " << *dst << "\n";
|
||||
}
|
||||
|
||||
void
|
||||
dump_file_info( boost::exception const & x )
|
||||
{
|
||||
if( boost::shared_ptr<std::string const> fn = boost::get_error_info<file_name_info>(x) )
|
||||
std::cout << "Source file name: " << *fn << "\n";
|
||||
}
|
||||
{
|
||||
if( boost::shared_ptr<std::string const> fn = boost::get_error_info<file_name_info>(x) )
|
||||
std::cout << "Source file name: " << *fn << "\n";
|
||||
}
|
||||
|
||||
void
|
||||
dump_clib_info( boost::exception const & x )
|
||||
{
|
||||
if( boost::shared_ptr<int const> err=boost::get_error_info<errno_info>(x) )
|
||||
std::cout << "OS error: " << *err << "\n";
|
||||
if( boost::shared_ptr<std::string const> fn=boost::get_error_info<function_info>(x) )
|
||||
std::cout << "Failed function: " << *fn << "\n";
|
||||
}
|
||||
{
|
||||
if( boost::shared_ptr<int const> err=boost::get_error_info<errno_info>(x) )
|
||||
std::cout << "OS error: " << *err << "\n";
|
||||
if( boost::shared_ptr<std::string const> fn=boost::get_error_info<function_info>(x) )
|
||||
std::cout << "Failed function: " << *fn << "\n";
|
||||
}
|
||||
|
||||
void
|
||||
dump_all_info( boost::exception const & x )
|
||||
{
|
||||
std::cout << "-------------------------------------------------\n";
|
||||
dump_copy_info(x);
|
||||
dump_file_info(x);
|
||||
dump_clib_info(x);
|
||||
std::cout << "\nOutput from what():\n";
|
||||
std::cout << x.what();
|
||||
}
|
||||
{
|
||||
std::cout << "-------------------------------------------------\n";
|
||||
dump_copy_info(x);
|
||||
dump_file_info(x);
|
||||
dump_clib_info(x);
|
||||
std::cout << "\nOutput from what():\n";
|
||||
std::cout << x.what();
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
try
|
||||
{
|
||||
create_data( "tmp1.txt" );
|
||||
copy_data( "tmp1.txt", "tmp2.txt" ); //This should succeed.
|
||||
{
|
||||
try
|
||||
{
|
||||
create_data( "tmp1.txt" );
|
||||
copy_data( "tmp1.txt", "tmp2.txt" ); //This should succeed.
|
||||
|
||||
reset_file( "tmp1.txt" ); //Creates empty file.
|
||||
try
|
||||
{
|
||||
copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt is empty.
|
||||
}
|
||||
catch(
|
||||
read_error & x )
|
||||
{
|
||||
std::cout << "\nCaught 'read_error' exception.\n";
|
||||
dump_all_info(x);
|
||||
}
|
||||
reset_file( "tmp1.txt" ); //Creates empty file.
|
||||
try
|
||||
{
|
||||
copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt is empty.
|
||||
}
|
||||
catch(
|
||||
read_error & x )
|
||||
{
|
||||
std::cout << "\nCaught 'read_error' exception.\n";
|
||||
dump_all_info(x);
|
||||
}
|
||||
|
||||
remove( "tmp1.txt" );
|
||||
remove( "tmp2.txt" );
|
||||
try
|
||||
{
|
||||
copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt does not exist.
|
||||
}
|
||||
catch(
|
||||
open_error & x )
|
||||
{
|
||||
std::cout << "\nCaught 'open_error' exception.\n";
|
||||
dump_all_info(x);
|
||||
}
|
||||
}
|
||||
catch(
|
||||
boost::exception & x )
|
||||
{
|
||||
std::cout << "\nCaught unexpected boost::exception!\n";
|
||||
dump_all_info(x);
|
||||
}
|
||||
}
|
||||
remove( "tmp1.txt" );
|
||||
remove( "tmp2.txt" );
|
||||
try
|
||||
{
|
||||
copy_data( "tmp1.txt", "tmp2.txt" ); //This should fail, tmp1.txt does not exist.
|
||||
}
|
||||
catch(
|
||||
open_error & x )
|
||||
{
|
||||
std::cout << "\nCaught 'open_error' exception.\n";
|
||||
dump_all_info(x);
|
||||
}
|
||||
}
|
||||
catch(
|
||||
boost::exception & x )
|
||||
{
|
||||
std::cout << "\nCaught unexpected boost::exception!\n";
|
||||
dump_all_info(x);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user