2009-02-04 10:33:18 -06:00
|
|
|
/*
|
2009-02-04 12:42:26 +01:00
|
|
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
2008-10-14 11:57:03 -05:00
|
|
|
*
|
2010-01-16 20:19:18 +03:00
|
|
|
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
|
2008-10-02 16:23:55 -05:00
|
|
|
*
|
|
|
|
|
* 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
|
2008-10-14 11:57:03 -05:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-10-02 16:23:55 -05:00
|
|
|
* 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
|
2008-10-14 11:57:03 -05:00
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2008-10-02 16:23:55 -05:00
|
|
|
*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-07-29 01:22:45 +02:00
|
|
|
#include "Config.h"
|
|
|
|
|
#include <ace/Configuration_Import_Export.h>
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-07-29 01:22:45 +02:00
|
|
|
static bool GetValueHelper(ACE_Configuration_Heap *mConf, const char *name, ACE_TString &result)
|
|
|
|
|
{
|
|
|
|
|
if (!mConf)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ACE_TString section_name;
|
|
|
|
|
ACE_Configuration_Section_Key section_key;
|
|
|
|
|
ACE_Configuration_Section_Key root_key = mConf->root_section();
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (mConf->enumerate_sections(root_key, i, section_name) == 0)
|
|
|
|
|
{
|
|
|
|
|
mConf->open_section(root_key, section_name.c_str(), 0, section_key);
|
|
|
|
|
if (mConf->get_string_value(section_key, name, result) == 0)
|
|
|
|
|
return true;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Config::Config() : mConf(NULL)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Config::~Config()
|
|
|
|
|
{
|
|
|
|
|
delete mConf;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-29 01:22:45 +02:00
|
|
|
bool Config::SetSource(const char *file)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
|
|
|
|
mFilename = file;
|
|
|
|
|
return Reload();
|
|
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
bool Config::Reload()
|
|
|
|
|
{
|
|
|
|
|
delete mConf;
|
2010-07-29 01:22:45 +02:00
|
|
|
mConf = new ACE_Configuration_Heap;
|
|
|
|
|
if (mConf->open() == 0)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-07-29 01:22:45 +02:00
|
|
|
ACE_Ini_ImpExp config_importer(*mConf);
|
|
|
|
|
if (config_importer.import_config(mFilename.c_str()) == 0)
|
|
|
|
|
return true;
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
2010-07-29 01:22:45 +02:00
|
|
|
delete mConf;
|
|
|
|
|
mConf = NULL;
|
|
|
|
|
return false;
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2009-03-18 20:46:39 +01:00
|
|
|
std::string Config::GetStringDefault(const char * name, std::string def)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-07-29 01:22:45 +02:00
|
|
|
ACE_TString val;
|
|
|
|
|
return GetValueHelper(mConf, name, val) ? val.c_str() : def;
|
2009-03-18 20:46:39 +01:00
|
|
|
};
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2009-03-18 20:46:39 +01:00
|
|
|
bool Config::GetBoolDefault(const char * name, const bool def)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-07-29 01:22:45 +02:00
|
|
|
ACE_TString val;
|
|
|
|
|
if (!GetValueHelper(mConf, name, val))
|
2009-03-18 20:46:39 +01:00
|
|
|
return def;
|
2010-07-29 01:22:45 +02:00
|
|
|
const char* str = val.c_str();
|
|
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
if(strcmp(str, "true") == 0 || strcmp(str, "TRUE") == 0 ||
|
|
|
|
|
strcmp(str, "yes") == 0 || strcmp(str, "YES") == 0 ||
|
|
|
|
|
strcmp(str, "1") == 0)
|
2009-03-18 20:46:39 +01:00
|
|
|
return true;
|
2008-10-02 16:23:55 -05:00
|
|
|
else
|
2009-03-18 20:46:39 +01:00
|
|
|
return false;
|
|
|
|
|
};
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2009-03-18 20:46:39 +01:00
|
|
|
int32 Config::GetIntDefault(const char * name, const int32 def)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-07-29 01:22:45 +02:00
|
|
|
ACE_TString val;
|
|
|
|
|
return GetValueHelper(mConf, name, val) ? atoi(val.c_str()) : def;
|
2009-03-18 20:46:39 +01:00
|
|
|
};
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2009-03-18 20:46:39 +01:00
|
|
|
float Config::GetFloatDefault(const char * name, const float def)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-07-29 01:22:45 +02:00
|
|
|
ACE_TString val;
|
|
|
|
|
return GetValueHelper(mConf, name, val) ? (float)atof(val.c_str()) : def;
|
2009-03-18 20:46:39 +01:00
|
|
|
};
|