root/trunk/src/shared/Config/Config.cpp @ 9

Revision 2, 3.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include "ConfigEnv.h"
20#include "Policies/SingletonImp.h"
21
22INSTANTIATE_SINGLETON_1(Config);
23
24Config::Config() : mIgnoreCase(true), mConf(NULL)
25{
26}
27
28
29Config::~Config()
30{
31    delete mConf;
32}
33
34
35bool Config::SetSource(const char *file, bool ignorecase)
36{
37    mIgnoreCase = ignorecase;
38    mFilename = file;
39
40    return Reload();
41}
42
43bool Config::Reload()
44{
45    delete mConf;
46
47    mConf = new DOTCONFDocument(mIgnoreCase ?
48        DOTCONFDocument::CASEINSENSETIVE :
49    DOTCONFDocument::CASESENSETIVE);
50
51    if (mConf->setContent(mFilename.c_str()) == -1)
52    {
53        delete mConf;
54        mConf = NULL;
55        return false;
56    }
57
58    return true;
59}
60
61bool Config::GetString(const char* name, std::string *value)
62{
63    if(!mConf)
64        return false;
65
66    DOTCONFDocumentNode const *node = mConf->findNode(name);
67    if(!node || !node->getValue())
68        return false;
69
70    *value = node->getValue();
71
72    return true;
73}
74
75bool Config::GetString(const char* name, char const **value)
76{
77    if(!mConf)
78        return false;
79
80    DOTCONFDocumentNode const *node = mConf->findNode(name);
81    if(!node || !node->getValue())
82        return false;
83
84    *value = node->getValue();
85
86    return true;
87}
88
89
90std::string Config::GetStringDefault(const char* name, const char* def)
91{
92    if(!mConf)
93        return std::string(def);
94
95    DOTCONFDocumentNode const *node = mConf->findNode(name);
96    if(!node || !node->getValue())
97        return std::string(def);
98
99    return std::string(node->getValue());
100}
101
102
103bool Config::GetBool(const char* name, bool *value)
104{
105    if(!mConf)
106        return false;
107
108    DOTCONFDocumentNode const *node = mConf->findNode(name);
109    if(!node || !node->getValue())
110        return false;
111
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)
116    {
117        *value = true;
118    }
119    else
120        *value = false;
121
122    return true;
123}
124
125
126bool Config::GetBoolDefault(const char* name, const bool def)
127{
128    bool val;
129    return GetBool(name, &val) ? val : def;
130}
131
132
133bool Config::GetInt(const char* name, int *value)
134{
135    if(!mConf)
136        return false;
137
138    DOTCONFDocumentNode const *node = mConf->findNode(name);
139    if(!node || !node->getValue())
140        return false;
141
142    *value = atoi(node->getValue());
143
144    return true;
145}
146
147
148bool Config::GetFloat(const char* name, float *value)
149{
150    if(!mConf)
151        return false;
152
153    DOTCONFDocumentNode const *node = mConf->findNode(name);
154    if(!node || !node->getValue())
155        return false;
156
157    *value = atof(node->getValue());
158
159    return true;
160}
161
162
163int Config::GetIntDefault(const char* name, const int def)
164{
165    int val;
166    return GetInt(name, &val) ? val : def;
167}
168
169
170float Config::GetFloatDefault(const char* name, const float def)
171{
172    float val;
173    return (GetFloat(name, &val) ? val : def);
174}
Note: See TracBrowser for help on using the browser.