2012-05-15 19:07:03 +04:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
2012-05-24 15:06:03 +04:00
|
|
|
#include "util/StdioFile.hh"
|
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2012-07-29 21:17:10 +04:00
|
|
|
#include <iostream>
|
2012-05-15 19:07:03 +04:00
|
|
|
#include <iterator>
|
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
namespace gr {
|
2012-05-15 19:07:03 +04:00
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
Config::Config(const fs::path& configFile) :
|
|
|
|
m_configFile(configFile),
|
|
|
|
m_cfg( Read() )
|
2012-05-15 19:07:03 +04:00
|
|
|
{
|
2012-07-30 11:24:47 +04:00
|
|
|
if (configFile.empty())
|
|
|
|
{
|
|
|
|
throw Error() << expt::ErrMsg("Config cannot be initalised with an empty string.");
|
|
|
|
}
|
2012-05-15 19:07:03 +04:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:17:10 +04:00
|
|
|
const fs::path& Config::ConfigFile() const
|
2012-05-15 19:07:03 +04:00
|
|
|
{
|
2012-07-29 21:17:10 +04:00
|
|
|
return m_configFile ;
|
2012-05-15 19:07:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::Save( )
|
|
|
|
{
|
2012-07-29 21:17:10 +04:00
|
|
|
StdioFile file( m_configFile.string(), 0600 ) ;
|
2012-05-24 15:06:03 +04:00
|
|
|
m_cfg.Write( file ) ;
|
2012-05-15 19:07:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Json& Config::Get()
|
|
|
|
{
|
|
|
|
return m_cfg ;
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:17:10 +04:00
|
|
|
Json Config::Read()
|
2012-05-15 20:54:58 +04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-07-29 21:17:10 +04:00
|
|
|
return Json::ParseFile( m_configFile.string() ) ;
|
2012-05-15 20:54:58 +04:00
|
|
|
}
|
|
|
|
catch ( Exception& e )
|
|
|
|
{
|
2012-05-17 16:55:51 +04:00
|
|
|
return Json() ;
|
2012-05-15 20:54:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
} // end of namespace
|