From 0b63d9bd3cded95a5db5119cd3e21fe3fa967d33 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Tue, 15 May 2012 23:07:03 +0800 Subject: [PATCH] added config file class, but not very well encapsulated --- grive/CMakeLists.txt | 6 ++- grive/src/Config.cc | 70 ++++++++++++++++++++++++++++++++++ grive/src/Config.hh | 44 +++++++++++++++++++++ grive/src/main.cc | 63 ++++-------------------------- libgrive/src/util/Exception.hh | 3 ++ 5 files changed, 129 insertions(+), 57 deletions(-) create mode 100644 grive/src/Config.cc create mode 100644 grive/src/Config.hh diff --git a/grive/CMakeLists.txt b/grive/CMakeLists.txt index e2dead6..8dca167 100644 --- a/grive/CMakeLists.txt +++ b/grive/CMakeLists.txt @@ -7,8 +7,12 @@ include_directories( add_definitions( -DVERSION="${GRIVE_VERSION}" ) +file (GLOB GRIVE_EXE_SRC + ${grive_SOURCE_DIR}/src/*.cc +) + add_executable( grive_executable - src/main.cc + ${GRIVE_EXE_SRC} ) target_link_libraries( grive_executable diff --git a/grive/src/Config.cc b/grive/src/Config.cc new file mode 100644 index 0000000..ea9b88d --- /dev/null +++ b/grive/src/Config.cc @@ -0,0 +1,70 @@ +/* + 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 "Config.hh" + +#include +#include + +namespace gr { + +const std::string& Config::Filename() +{ + static const char *env_cfg = ::getenv( "GR_CONFIG" ) ; + static const std::string filename = + (env_cfg != 0) ? env_cfg : std::string( ::getenv( "HOME") ) + "/.grive" ; + + return filename ; +} + +Config::Config() +{ + std::ifstream ifile( Filename().c_str() ) ; + if ( ifile ) + { + try + { + std::string cfg_str( + (std::istreambuf_iterator( ifile )), + (std::istreambuf_iterator()) ) ; + + m_cfg = Json::Parse( cfg_str ) ; + } + catch ( Exception& e ) + { + throw Error() + << File( Filename() ) + << expt::ErrMsg("Cannot open config file ") + << expt::Nested(e) ; + } + } +} + +void Config::Save( ) +{ + std::ofstream ofile( Filename().c_str() ) ; + ofile << m_cfg ; +} + +Json& Config::Get() +{ + return m_cfg ; +} + +} // end of namespace diff --git a/grive/src/Config.hh b/grive/src/Config.hh new file mode 100644 index 0000000..a8c0872 --- /dev/null +++ b/grive/src/Config.hh @@ -0,0 +1,44 @@ +/* + 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 "util/Exception.hh" +#include "protocol/Json.hh" + +namespace gr { + +class Config +{ +public : + struct Error : virtual Exception {} ; + typedef boost::error_info File ; + + static const std::string& Filename() ; + + Config() ; + + Json& Get() ; + void Save() ; + +private : + Json m_cfg ; +} ; + +} // end of namespace diff --git a/grive/src/main.cc b/grive/src/main.cc index 175d661..cab2067 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -17,6 +17,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "Config.hh" + #include "drive/Drive.hh" #include "protocol/OAuth2.hh" @@ -31,68 +33,17 @@ #include #include -#include #include #include -#include - -#include -#include const std::string client_id = "22314510474.apps.googleusercontent.com" ; const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ; -namespace gr -{ - class ConfigError : public std::runtime_error - { - public : - ConfigError( const std::string& msg ) : runtime_error( msg ) - { - } - } ; - - const std::string& ConfigFilename() - { - static const char *env_cfg = ::getenv( "GR_CONFIG" ) ; - static const std::string filename = - (env_cfg != 0) ? env_cfg : std::string( ::getenv( "HOME") ) + "/.grive" ; - - return filename ; - } - - Json ReadConfig() - { - std::ifstream ifile( ConfigFilename().c_str() ) ; - if ( !ifile ) - return Json() ; - - try - { - std::string cfg_str( - (std::istreambuf_iterator( ifile )), - (std::istreambuf_iterator()) ) ; - - return Json::Parse( cfg_str ) ; - } - catch ( std::runtime_error& e ) - { - throw ConfigError( std::string("Cannot open config file ") + e.what() ) ; - } - } - - void SaveConfig( const Json& config ) - { - std::ofstream ofile( ConfigFilename().c_str() ) ; - ofile << config ; - } -} - int main( int argc, char **argv ) { using namespace gr ; - Json config = ReadConfig() ; + Config config ; DefaultLog nofile_log ; LogBase::Inst( &nofile_log ) ; @@ -120,9 +71,9 @@ int main( int argc, char **argv ) token.Auth( code ) ; // save to config - config.Add( "refresh_token", Json( token.RefreshToken() ) ) ; - assert( config["refresh_token"].As() == token.RefreshToken() ) ; - SaveConfig( config ) ; + config.Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ; + assert( config.Get()["refresh_token"].Str() == token.RefreshToken() ) ; + config.Save( ) ; break ; } @@ -147,7 +98,7 @@ int main( int argc, char **argv ) std::string refresh_token ; try { - refresh_token = config["refresh_token"].As() ; + refresh_token = config.Get()["refresh_token"].Str() ; } catch ( const std::runtime_error& error ) { diff --git a/libgrive/src/util/Exception.hh b/libgrive/src/util/Exception.hh index aa31411..fec5af7 100644 --- a/libgrive/src/util/Exception.hh +++ b/libgrive/src/util/Exception.hh @@ -70,6 +70,9 @@ namespace expt // generic error message typedef boost::error_info ErrMsg ; + + // nested exception + typedef boost::error_info Nested ; } } // end of namespace