use boost exception macro to throw exception

pull/40/head
Matchman Green 2012-05-14 23:32:22 +08:00
parent 881ff16d05
commit 35c2ef9fc1
8 changed files with 164 additions and 52 deletions

View File

@ -22,36 +22,40 @@
#include "Error.hh"
// boost headers
#include <boost/throw_exception.hpp>
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_at_line.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_handle.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/info.hpp>
#include <openssl/evp.h>
#include <cassert>
#include <new>
#include <stdexcept>
#include <signal.h>
namespace gr { namespace http {
Download::Download( const std::string& filename ) :
m_file( filename.c_str(), std::ios::out | std::ios::binary ),
m_file( filename, "wb" ),
m_mdctx( ::EVP_MD_CTX_create() )
{
if ( m_mdctx == 0 )
throw std::bad_alloc() ;
if ( ::EVP_DigestInit_ex( m_mdctx, ::EVP_md5(), 0 ) != 1 )
throw Error() << expt::ErrMsg( "cannot create MD5 digest context" ) ;
if ( !m_file )
throw Error() << expt::ErrMsg( "cannot open file " + filename + " for writing" ) ;
BOOST_THROW_EXCEPTION( Error() << expt::ErrMsg( "cannot create MD5 digest context" ) ) ;
}
Download::Download( const std::string& filename, NoChecksum ) :
m_file( filename.c_str(), std::ios::out | std::ios::binary ),
m_file( filename, "wb" ),
m_mdctx( 0 )
{
if ( !m_file )
throw Error() << expt::ErrMsg( "cannot open file " + filename + " for writing" ) ;
}
Download::~Download( )
@ -87,19 +91,6 @@ std::string Download::Finish() const
return result ;
}
std::size_t Download::Callback( char *data, std::size_t size, std::size_t nmemb, Download *pthis )
{
assert( pthis != 0 ) ;
assert( data != 0 ) ;
std::size_t count = size * nmemb ;
if ( pthis->m_mdctx != 0 )
::EVP_DigestUpdate( pthis->m_mdctx, data, count ) ;
return pthis->m_file.rdbuf()->sputn( data, count ) ;
}
std::size_t Download::OnData( void *data, std::size_t count )
{
assert( data != 0 ) ;
@ -107,7 +98,7 @@ std::size_t Download::OnData( void *data, std::size_t count )
if ( m_mdctx != 0 )
::EVP_DigestUpdate( m_mdctx, data, count ) ;
return m_file.rdbuf()->sputn( reinterpret_cast<char*>(data), count ) ;
return m_file.Write( data, count ) ;
}
} } // end of namespace

View File

@ -20,9 +20,9 @@
#pragma once
#include "Receivable.hh"
#include "util/StdioFile.hh"
#include <string>
#include <fstream>
#include <openssl/evp.h>
@ -41,11 +41,9 @@ public :
void Clear() ;
std::size_t OnData( void *data, std::size_t count ) ;
static std::size_t Callback( char *data, std::size_t size, std::size_t nmemb, Download *pthis ) ;
private :
std::ofstream m_file ;
EVP_MD_CTX *m_mdctx ;
StdioFile m_file ;
EVP_MD_CTX *m_mdctx ;
} ;
} } // end of namespace

View File

@ -39,15 +39,4 @@ Exception::Exception( )
#endif
}
const char* Exception::what() const throw()
{
if ( const std::string *s = boost::get_error_info<expt::ErrMsg>(*this) )
return s->c_str() ;
else
{
static const char unknown[] = "unknown" ;
return unknown ;
}
}
} // end of namespace

View File

@ -41,8 +41,6 @@ struct Exception :
virtual public boost::exception
{
Exception( ) ;
const char* what() const throw() ;
} ;
struct FileError : virtual Exception {} ;
@ -72,12 +70,6 @@ namespace expt
// generic error message
typedef boost::error_info<struct MsgTag, std::string> ErrMsg ;
// errno as in C
typedef boost::error_info<struct ErrorNumberTag, int> ErrorNumber ;
// the filename of the file that caused the error
typedef boost::error_info<struct FileNameTag, std::string> FileName ;
}
} // end of namespace

View File

@ -23,6 +23,15 @@
#include "Exception.hh"
#include "Path.hh"
// boost headers
#include <boost/throw_exception.hpp>
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_at_line.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/info.hpp>
// OS specific headers
#include <errno.h>
#include <sys/stat.h>
@ -51,11 +60,12 @@ DateTime FileMTime( const std::string& filename )
struct stat s = {} ;
if ( ::stat( filename.c_str(), &s ) != 0 )
{
// save errno to prevend overwritten later
int err_num = errno ;
throw Exception()
<< expt::ErrMsg( "cannot get file attribute of " + filename )
<< expt::ErrorNumber( err_num ) ;
BOOST_THROW_EXCEPTION(
Error()
<< boost::errinfo_api_function("stat")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(filename)
) ;
}
#if defined __APPLE__ && defined __DARWIN_64_BIT_INO_T

View File

@ -19,6 +19,8 @@
#pragma once
#include "Exception.hh"
#include <string>
namespace gr {
@ -28,6 +30,8 @@ class Path ;
namespace os
{
struct Error : virtual Exception {} ;
void MakeDir( const std::string& dir ) ;
void MakeDir( const Path& dir ) ;

View File

@ -0,0 +1,80 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "StdioFile.hh"
#include <cassert>
// boost headers
#include <boost/throw_exception.hpp>
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_at_line.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/info.hpp>
namespace gr {
StdioFile::StdioFile( const std::string& filename, const char *mode ) :
m_file( std::fopen( filename.c_str(), mode ) )
{
if ( m_file == 0 )
{
BOOST_THROW_EXCEPTION(
Error()
<< boost::errinfo_api_function("fopen")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(filename)
<< boost::errinfo_file_open_mode(mode)
) ;
}
}
StdioFile::~StdioFile( )
{
assert( m_file != 0 ) ;
std::fclose( m_file ) ;
}
std::size_t StdioFile::Read( void *ptr, std::size_t size )
{
assert( m_file != 0 ) ;
return std::fread( ptr, 1, size, m_file ) ;
}
std::size_t StdioFile::Write( const void *ptr, std::size_t size )
{
assert( m_file != 0 ) ;
return std::fwrite( ptr, 1, size, m_file ) ;
}
int StdioFile::Seek( long offset, int whence )
{
assert( m_file != 0 ) ;
return std::fseek( m_file, offset, whence ) ;
}
long StdioFile::Tell() const
{
assert( m_file != 0 ) ;
return std::ftell( m_file ) ;
}
} // end of namespace

View File

@ -0,0 +1,48 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#include "Exception.hh"
#include <cstdio>
#include <string>
namespace gr {
class StdioFile
{
public :
struct Error : virtual Exception {} ;
public :
StdioFile( const std::string& filename, const char *mode ) ;
~StdioFile( ) ;
std::size_t Read( void *ptr, std::size_t size ) ;
std::size_t Write( const void *ptr, std::size_t size ) ;
int Seek( long offset, int whence ) ;
long Tell() const ;
private :
std::FILE *m_file ;
} ;
} // end of namespace