2018-06-10 16:28:03 +00:00
|
|
|
#include "ConfigMgr.h"
|
2018-10-24 23:02:30 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <experimental/filesystem>
|
2018-06-10 16:28:03 +00:00
|
|
|
|
2018-11-06 18:35:02 +11:00
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
|
2018-06-10 16:28:03 +00:00
|
|
|
/**
|
|
|
|
* Loads an ini file and parses it
|
|
|
|
* @param configName the name of ini file relative to m_configFolderRoot to load alongside global.ini
|
|
|
|
* @return true if loading was successful
|
|
|
|
*/
|
2018-11-29 16:55:48 +01:00
|
|
|
bool Sapphire::ConfigMgr::loadConfig( const std::string& configName )
|
2018-06-10 16:28:03 +00:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
// get global config
|
2018-11-06 18:35:02 +11:00
|
|
|
auto configFile = fs::path( fs::path( m_configFolderRoot ) / configName );
|
|
|
|
|
|
|
|
if( !fs::exists( configFile ) )
|
|
|
|
{
|
2018-12-18 21:40:44 +11:00
|
|
|
copyDefaultConfig( configName );
|
|
|
|
return false;
|
2018-11-06 18:35:02 +11:00
|
|
|
}
|
2018-06-10 16:28:03 +00:00
|
|
|
|
2018-11-06 18:35:02 +11:00
|
|
|
m_pInih = std::unique_ptr< INIReader >( new INIReader( configFile.string() ) );
|
2018-10-24 23:02:30 +02:00
|
|
|
|
|
|
|
if( m_pInih->ParseError() < 0 )
|
|
|
|
return false;
|
2018-06-10 16:28:03 +00:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return true;
|
2018-06-10 16:28:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
bool Sapphire::ConfigMgr::copyDefaultConfig( const std::string& configName )
|
2018-06-10 16:28:03 +00:00
|
|
|
{
|
2018-11-06 18:35:02 +11:00
|
|
|
fs::path configPath( m_configFolderRoot );
|
2018-08-29 21:40:59 +02:00
|
|
|
configPath /= configName;
|
2018-06-10 16:28:03 +00:00
|
|
|
|
2018-11-06 18:35:02 +11:00
|
|
|
if( !fs::exists( configPath.string() + m_configDefaultSuffix ) )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
|
|
|
// no default file :(
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-10 16:28:03 +00:00
|
|
|
|
2018-11-06 18:35:02 +11:00
|
|
|
fs::copy_file( configPath.string() + m_configDefaultSuffix, configPath );
|
2018-06-10 16:28:03 +00:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return true;
|
2018-10-24 23:02:30 +02:00
|
|
|
}
|