From 35c2ef9fc16adeb46f3fafcd2a664d642e262836 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Mon, 14 May 2012 23:32:22 +0800 Subject: [PATCH] use boost exception macro to throw exception --- libgrive/src/http/Download.cc | 37 ++++++---------- libgrive/src/http/Download.hh | 8 ++-- libgrive/src/util/Exception.cc | 11 ----- libgrive/src/util/Exception.hh | 8 ---- libgrive/src/util/OS.cc | 20 ++++++--- libgrive/src/util/OS.hh | 4 ++ libgrive/src/util/StdioFile.cc | 80 ++++++++++++++++++++++++++++++++++ libgrive/src/util/StdioFile.hh | 48 ++++++++++++++++++++ 8 files changed, 164 insertions(+), 52 deletions(-) create mode 100644 libgrive/src/util/StdioFile.cc create mode 100644 libgrive/src/util/StdioFile.hh diff --git a/libgrive/src/http/Download.cc b/libgrive/src/http/Download.cc index aad5eef..1545f0a 100644 --- a/libgrive/src/http/Download.cc +++ b/libgrive/src/http/Download.cc @@ -22,36 +22,40 @@ #include "Error.hh" +// boost headers +#include +#include +#include +#include +#include +#include +#include +#include + #include #include #include -#include #include 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(data), count ) ; + return m_file.Write( data, count ) ; } } } // end of namespace diff --git a/libgrive/src/http/Download.hh b/libgrive/src/http/Download.hh index 105c736..1c25b89 100644 --- a/libgrive/src/http/Download.hh +++ b/libgrive/src/http/Download.hh @@ -20,9 +20,9 @@ #pragma once #include "Receivable.hh" +#include "util/StdioFile.hh" #include -#include #include @@ -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 diff --git a/libgrive/src/util/Exception.cc b/libgrive/src/util/Exception.cc index 9df5bfa..68738b4 100644 --- a/libgrive/src/util/Exception.cc +++ b/libgrive/src/util/Exception.cc @@ -39,15 +39,4 @@ Exception::Exception( ) #endif } -const char* Exception::what() const throw() -{ - if ( const std::string *s = boost::get_error_info(*this) ) - return s->c_str() ; - else - { - static const char unknown[] = "unknown" ; - return unknown ; - } -} - } // end of namespace diff --git a/libgrive/src/util/Exception.hh b/libgrive/src/util/Exception.hh index 79d26ae..aa31411 100644 --- a/libgrive/src/util/Exception.hh +++ b/libgrive/src/util/Exception.hh @@ -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 ErrMsg ; - - // errno as in C - typedef boost::error_info ErrorNumber ; - - // the filename of the file that caused the error - typedef boost::error_info FileName ; } } // end of namespace diff --git a/libgrive/src/util/OS.cc b/libgrive/src/util/OS.cc index 78aae79..2e1cc99 100644 --- a/libgrive/src/util/OS.cc +++ b/libgrive/src/util/OS.cc @@ -23,6 +23,15 @@ #include "Exception.hh" #include "Path.hh" +// boost headers +#include +#include +#include +#include +#include +#include +#include + // OS specific headers #include #include @@ -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 diff --git a/libgrive/src/util/OS.hh b/libgrive/src/util/OS.hh index 340b403..73dac5e 100644 --- a/libgrive/src/util/OS.hh +++ b/libgrive/src/util/OS.hh @@ -19,6 +19,8 @@ #pragma once +#include "Exception.hh" + #include 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 ) ; diff --git a/libgrive/src/util/StdioFile.cc b/libgrive/src/util/StdioFile.cc new file mode 100644 index 0000000..3eceb80 --- /dev/null +++ b/libgrive/src/util/StdioFile.cc @@ -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 + +// boost headers +#include +#include +#include +#include +#include +#include +#include + +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 diff --git a/libgrive/src/util/StdioFile.hh b/libgrive/src/util/StdioFile.hh new file mode 100644 index 0000000..1bc364b --- /dev/null +++ b/libgrive/src/util/StdioFile.hh @@ -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 +#include + +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