2010-10-07 15:35:36 +02:00
|
|
|
/*
|
2014-01-01 00:07:53 +01:00
|
|
|
* Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/>
|
2010-10-07 15:35:36 +02:00
|
|
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
|
|
|
|
*
|
|
|
|
|
* 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; either version 2 of the License, or (at your
|
|
|
|
|
* option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
2008-10-02 16:23:55 -05:00
|
|
|
*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
2010-07-29 01:22:45 +02:00
|
|
|
#include "Config.h"
|
2013-07-15 17:31:44 +02:00
|
|
|
#include "Errors.h"
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
using namespace boost::property_tree;
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2013-07-15 17:31:44 +02:00
|
|
|
bool ConfigMgr::LoadInitial(char const* file)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2013-07-15 17:31:44 +02:00
|
|
|
ASSERT(file);
|
2011-09-28 13:00:43 +02:00
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
std::lock_guard<std::mutex> lock(_configLock);
|
2011-09-28 13:00:43 +02:00
|
|
|
|
2013-07-15 17:31:44 +02:00
|
|
|
_filename = file;
|
|
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
try
|
|
|
|
|
{
|
2014-06-22 16:29:49 +02:00
|
|
|
ptree fullTree;
|
|
|
|
|
boost::property_tree::ini_parser::read_ini(file, fullTree);
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2014-06-22 16:29:49 +02:00
|
|
|
// Since we're using only one section per config file, we skip the section and have direct property access
|
|
|
|
|
for (auto section : fullTree)
|
2014-06-22 15:45:54 +02:00
|
|
|
{
|
2014-06-22 16:29:49 +02:00
|
|
|
_config = section.second;
|
2014-06-22 15:45:54 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception const& /*ex*/)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-07-15 17:31:44 +02:00
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
return true;
|
2013-07-15 17:31:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigMgr::Reload()
|
|
|
|
|
{
|
|
|
|
|
return LoadInitial(_filename.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
std::string ConfigMgr::GetStringDefault(const char* name, const std::string &def)
|
2013-07-15 17:31:44 +02:00
|
|
|
{
|
2014-06-22 15:45:54 +02:00
|
|
|
std::string value = _config.get<std::string>(ptree::path_type(name,'/'), def);
|
2013-07-15 17:31:44 +02:00
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
value.erase(std::remove(value.begin(), value.end(), '"'), value.end());
|
2013-07-15 17:31:44 +02:00
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
return value;
|
2012-08-03 14:20:18 +02:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2013-07-15 17:31:44 +02:00
|
|
|
bool ConfigMgr::GetBoolDefault(const char* name, bool def)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2014-06-22 15:45:54 +02:00
|
|
|
try
|
|
|
|
|
{
|
2014-06-30 14:44:52 +02:00
|
|
|
std::string val = _config.get<std::string>(ptree::path_type(name, '/'));
|
2014-06-22 15:45:54 +02:00
|
|
|
val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
|
|
|
|
|
return (val == "true" || val == "TRUE" || val == "yes" || val == "YES" || val == "1");
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception const& /*ex*/)
|
|
|
|
|
{
|
2009-03-18 20:46:39 +01:00
|
|
|
return def;
|
2014-06-22 15:45:54 +02:00
|
|
|
}
|
2012-08-03 14:20:18 +02:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2013-07-15 17:31:44 +02:00
|
|
|
int ConfigMgr::GetIntDefault(const char* name, int def)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2014-06-30 14:44:52 +02:00
|
|
|
return _config.get<int>(ptree::path_type(name, '/'), def);
|
2012-08-03 14:20:18 +02:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2013-07-15 17:31:44 +02:00
|
|
|
float ConfigMgr::GetFloatDefault(const char* name, float def)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2014-06-30 14:44:52 +02:00
|
|
|
return _config.get<float>(ptree::path_type(name, '/'), def);
|
2012-08-03 14:20:18 +02:00
|
|
|
}
|
2011-09-28 13:00:43 +02:00
|
|
|
|
2013-07-15 17:31:44 +02:00
|
|
|
std::string const& ConfigMgr::GetFilename()
|
2011-09-28 13:00:43 +02:00
|
|
|
{
|
2014-06-22 15:45:54 +02:00
|
|
|
std::lock_guard<std::mutex> lock(_configLock);
|
2011-09-28 13:00:43 +02:00
|
|
|
return _filename;
|
|
|
|
|
}
|
2013-11-07 16:34:44 +01:00
|
|
|
|
|
|
|
|
std::list<std::string> ConfigMgr::GetKeysByString(std::string const& name)
|
|
|
|
|
{
|
2014-06-22 15:45:54 +02:00
|
|
|
std::lock_guard<std::mutex> lock(_configLock);
|
2013-11-07 16:34:44 +01:00
|
|
|
|
|
|
|
|
std::list<std::string> keys;
|
|
|
|
|
|
2014-06-22 15:45:54 +02:00
|
|
|
for (const ptree::value_type& child : _config)
|
2013-11-07 16:34:44 +01:00
|
|
|
{
|
2014-06-22 15:45:54 +02:00
|
|
|
if (child.first.compare(0, name.length(), name) == 0)
|
2013-11-07 16:34:44 +01:00
|
|
|
{
|
2014-06-22 15:45:54 +02:00
|
|
|
keys.push_back(child.first);
|
2013-11-07 16:34:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-06-22 15:45:54 +02:00
|
|
|
|
2013-11-07 16:34:44 +01:00
|
|
|
return keys;
|
|
|
|
|
}
|