1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #include "ConfigEnv.h" |
---|
22 | #include "Policies/SingletonImp.h" |
---|
23 | |
---|
24 | INSTANTIATE_SINGLETON_1(Config); |
---|
25 | |
---|
26 | Config::Config() : mIgnoreCase(true), mConf(NULL) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | Config::~Config() |
---|
32 | { |
---|
33 | delete mConf; |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | bool Config::SetSource(const char *file, bool ignorecase) |
---|
38 | { |
---|
39 | mIgnoreCase = ignorecase; |
---|
40 | mFilename = file; |
---|
41 | |
---|
42 | return Reload(); |
---|
43 | } |
---|
44 | |
---|
45 | bool Config::Reload() |
---|
46 | { |
---|
47 | delete mConf; |
---|
48 | |
---|
49 | mConf = new DOTCONFDocument(mIgnoreCase ? |
---|
50 | DOTCONFDocument::CASEINSENSETIVE : |
---|
51 | DOTCONFDocument::CASESENSETIVE); |
---|
52 | |
---|
53 | if (mConf->setContent(mFilename.c_str()) == -1) |
---|
54 | { |
---|
55 | delete mConf; |
---|
56 | mConf = NULL; |
---|
57 | return false; |
---|
58 | } |
---|
59 | |
---|
60 | return true; |
---|
61 | } |
---|
62 | |
---|
63 | bool Config::GetString(const char* name, std::string *value) |
---|
64 | { |
---|
65 | if(!mConf) |
---|
66 | return false; |
---|
67 | |
---|
68 | DOTCONFDocumentNode const *node = mConf->findNode(name); |
---|
69 | if(!node || !node->getValue()) |
---|
70 | return false; |
---|
71 | |
---|
72 | *value = node->getValue(); |
---|
73 | |
---|
74 | return true; |
---|
75 | } |
---|
76 | |
---|
77 | bool Config::GetString(const char* name, char const **value) |
---|
78 | { |
---|
79 | if(!mConf) |
---|
80 | return false; |
---|
81 | |
---|
82 | DOTCONFDocumentNode const *node = mConf->findNode(name); |
---|
83 | if(!node || !node->getValue()) |
---|
84 | return false; |
---|
85 | |
---|
86 | *value = node->getValue(); |
---|
87 | |
---|
88 | return true; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | std::string Config::GetStringDefault(const char* name, const char* def) |
---|
93 | { |
---|
94 | if(!mConf) |
---|
95 | return std::string(def); |
---|
96 | |
---|
97 | DOTCONFDocumentNode const *node = mConf->findNode(name); |
---|
98 | if(!node || !node->getValue()) |
---|
99 | return std::string(def); |
---|
100 | |
---|
101 | return std::string(node->getValue()); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | bool Config::GetBool(const char* name, bool *value) |
---|
106 | { |
---|
107 | if(!mConf) |
---|
108 | return false; |
---|
109 | |
---|
110 | DOTCONFDocumentNode const *node = mConf->findNode(name); |
---|
111 | if(!node || !node->getValue()) |
---|
112 | return false; |
---|
113 | |
---|
114 | const char* str = node->getValue(); |
---|
115 | if(strcmp(str, "true") == 0 || strcmp(str, "TRUE") == 0 || |
---|
116 | strcmp(str, "yes") == 0 || strcmp(str, "YES") == 0 || |
---|
117 | strcmp(str, "1") == 0) |
---|
118 | { |
---|
119 | *value = true; |
---|
120 | } |
---|
121 | else |
---|
122 | *value = false; |
---|
123 | |
---|
124 | return true; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | bool Config::GetBoolDefault(const char* name, const bool def) |
---|
129 | { |
---|
130 | bool val; |
---|
131 | return GetBool(name, &val) ? val : def; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | bool Config::GetInt(const char* name, int *value) |
---|
136 | { |
---|
137 | if(!mConf) |
---|
138 | return false; |
---|
139 | |
---|
140 | DOTCONFDocumentNode const *node = mConf->findNode(name); |
---|
141 | if(!node || !node->getValue()) |
---|
142 | return false; |
---|
143 | |
---|
144 | *value = atoi(node->getValue()); |
---|
145 | |
---|
146 | return true; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | bool Config::GetFloat(const char* name, float *value) |
---|
151 | { |
---|
152 | if(!mConf) |
---|
153 | return false; |
---|
154 | |
---|
155 | DOTCONFDocumentNode const *node = mConf->findNode(name); |
---|
156 | if(!node || !node->getValue()) |
---|
157 | return false; |
---|
158 | |
---|
159 | *value = atof(node->getValue()); |
---|
160 | |
---|
161 | return true; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | int Config::GetIntDefault(const char* name, const int def) |
---|
166 | { |
---|
167 | int val; |
---|
168 | return GetInt(name, &val) ? val : def; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | float Config::GetFloatDefault(const char* name, const float def) |
---|
173 | { |
---|
174 | float val; |
---|
175 | return (GetFloat(name, &val) ? val : def); |
---|
176 | } |
---|