Updated Copyright year to 2013
[getmangos.git] / src / shared / Config / Config.cpp
blobae85d29872edcf9644c4b00fbc688b8a930b37e0
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Config.h"
20 #include "ace/Configuration_Import_Export.h"
22 #include "Policies/SingletonImp.h"
24 INSTANTIATE_SINGLETON_1(Config);
26 static bool GetValueHelper(ACE_Configuration_Heap* mConf, const char* name, ACE_TString& result)
28 if (!mConf)
29 return false;
31 ACE_TString section_name;
32 ACE_Configuration_Section_Key section_key;
33 ACE_Configuration_Section_Key root_key = mConf->root_section();
35 int i = 0;
36 while (mConf->enumerate_sections(root_key, i, section_name) == 0)
38 mConf->open_section(root_key, section_name.c_str(), 0, section_key);
39 if (mConf->get_string_value(section_key, name, result) == 0)
40 return true;
41 ++i;
44 return false;
47 Config::Config()
48 : mConf(NULL)
52 Config::~Config()
54 delete mConf;
57 bool Config::SetSource(const char* file)
59 mFilename = file;
61 return Reload();
64 bool Config::Reload()
66 delete mConf;
67 mConf = new ACE_Configuration_Heap;
69 if (mConf->open() == 0)
71 ACE_Ini_ImpExp config_importer(*mConf);
72 if (config_importer.import_config(mFilename.c_str()) == 0)
73 return true;
76 delete mConf;
77 mConf = NULL;
78 return false;
81 std::string Config::GetStringDefault(const char* name, const char* def)
83 ACE_TString val;
84 return GetValueHelper(mConf, name, val) ? val.c_str() : def;
87 bool Config::GetBoolDefault(const char* name, bool def)
89 ACE_TString val;
90 if (!GetValueHelper(mConf, name, val))
91 return def;
93 const char* str = val.c_str();
94 if (strcmp(str, "true") == 0 || strcmp(str, "TRUE") == 0 ||
95 strcmp(str, "yes") == 0 || strcmp(str, "YES") == 0 ||
96 strcmp(str, "1") == 0)
97 return true;
98 else
99 return false;
103 int32 Config::GetIntDefault(const char* name, int32 def)
105 ACE_TString val;
106 return GetValueHelper(mConf, name, val) ? atoi(val.c_str()) : def;
110 float Config::GetFloatDefault(const char* name, float def)
112 ACE_TString val;
113 return GetValueHelper(mConf, name, val) ? (float)atof(val.c_str()) : def;