Removed trailing whitespaces and CRLFs
[getmangos.git] / src / shared / Config / Config.cpp
blob8aa9293c2aa2833db612cad06022c96e2b9efad4
1 /*
2 * Copyright (C) 2005-2008 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 "ConfigEnv.h"
20 #include "Policies/SingletonImp.h"
22 INSTANTIATE_SINGLETON_1(Config);
24 Config::Config() : mIgnoreCase(true), mConf(NULL)
29 Config::~Config()
31 delete mConf;
35 bool Config::SetSource(const char *file, bool ignorecase)
37 mIgnoreCase = ignorecase;
38 mFilename = file;
40 return Reload();
43 bool Config::Reload()
45 delete mConf;
47 mConf = new DOTCONFDocument(mIgnoreCase ?
48 DOTCONFDocument::CASEINSENSETIVE :
49 DOTCONFDocument::CASESENSETIVE);
51 if (mConf->setContent(mFilename.c_str()) == -1)
53 delete mConf;
54 mConf = NULL;
55 return false;
58 return true;
61 bool Config::GetString(const char* name, std::string *value)
63 if(!mConf)
64 return false;
66 DOTCONFDocumentNode const *node = mConf->findNode(name);
67 if(!node || !node->getValue())
68 return false;
70 *value = node->getValue();
72 return true;
75 bool Config::GetString(const char* name, char const **value)
77 if(!mConf)
78 return false;
80 DOTCONFDocumentNode const *node = mConf->findNode(name);
81 if(!node || !node->getValue())
82 return false;
84 *value = node->getValue();
86 return true;
90 std::string Config::GetStringDefault(const char* name, const char* def)
92 if(!mConf)
93 return std::string(def);
95 DOTCONFDocumentNode const *node = mConf->findNode(name);
96 if(!node || !node->getValue())
97 return std::string(def);
99 return std::string(node->getValue());
103 bool Config::GetBool(const char* name, bool *value)
105 if(!mConf)
106 return false;
108 DOTCONFDocumentNode const *node = mConf->findNode(name);
109 if(!node || !node->getValue())
110 return false;
112 const char* str = node->getValue();
113 if(strcmp(str, "true") == 0 || strcmp(str, "TRUE") == 0 ||
114 strcmp(str, "yes") == 0 || strcmp(str, "YES") == 0 ||
115 strcmp(str, "1") == 0)
117 *value = true;
119 else
120 *value = false;
122 return true;
126 bool Config::GetBoolDefault(const char* name, const bool def)
128 bool val;
129 return GetBool(name, &val) ? val : def;
133 bool Config::GetInt(const char* name, int *value)
135 if(!mConf)
136 return false;
138 DOTCONFDocumentNode const *node = mConf->findNode(name);
139 if(!node || !node->getValue())
140 return false;
142 *value = atoi(node->getValue());
144 return true;
148 bool Config::GetFloat(const char* name, float *value)
150 if(!mConf)
151 return false;
153 DOTCONFDocumentNode const *node = mConf->findNode(name);
154 if(!node || !node->getValue())
155 return false;
157 *value = atof(node->getValue());
159 return true;
163 int Config::GetIntDefault(const char* name, const int def)
165 int val;
166 return GetInt(name, &val) ? val : def;
170 float Config::GetFloatDefault(const char* name, const float def)
172 float val;
173 return (GetFloat(name, &val) ? val : def);