source: nscp/trunk/include/Settings.h @ 2172776

Last change on this file since 2172776 was 2172776, checked in by Michael Medin <michael@…>, 6 years ago

2006-03-05 MickeM

  • Fixed -v FILEAGE check_nt (NSClient) check.
  • Added licence agreement header to all files
  • Property mode set to 100644
File size: 9.3 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
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                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#pragma once
22
23#include <Singleton.h>
24#include <string>
25#include <map>
26#include <INISettings.h>
27#include <REGSettings.h>
28#define BUFF_LEN 4096
29
30class SettingsException {
31private:
32        std::string err;
33public:
34        SettingsException(std::string str) : err(str) {}
35        std::string getMessage() {
36                return err;
37        }
38
39};
40
41class SettingsT
42{
43private:
44        typedef struct {
45                typedef enum { sType, iType} typeEnum;
46                typeEnum type;
47                std::string sVal;
48                int iVal;
49        } valueStruct;
50        typedef std::map<std::string,valueStruct> saveKeyList;
51        typedef std::map<std::string,saveKeyList> saveSectionList;
52        saveSectionList data_;
53        std::string file_;
54        bool bHasInternalData;
55        TSettings *settingsManager;
56
57public:
58        typedef std::list<std::string> sectionList;
59        SettingsT(void) : bHasInternalData(false), settingsManager(NULL)
60        {
61        }
62
63        virtual ~SettingsT(void)
64        {
65                if (settingsManager)
66                        delete settingsManager;
67        }
68        std::string getActiveType() {
69                if (!settingsManager) {
70                        return "";
71                } return settingsManager->getActiveType();
72        }
73
74        /**
75         * Set the file to read from
76         * @param file A INI-file to use as settings repository
77         */
78        void setFile(std::string file, bool forceini = false) {
79                file_ = file;
80                if (forceini) {
81                        if (settingsManager)
82                                delete settingsManager;
83                        settingsManager = new INISettings(file);
84                        return;
85                }
86                if (REGSettings::hasSettings()) {
87                        if (settingsManager)
88                                delete settingsManager;
89                        settingsManager = new REGSettings();
90                } else if (INISettings::hasSettings(file)) {
91                        if (settingsManager)
92                                delete settingsManager;
93                        settingsManager = new INISettings(file);
94                } else {
95                        throw SettingsException("No settings method specified, cannot start");
96                }
97        }
98
99#define UNLIKELY_VALUE_1 -1234
100#define UNLIKELY_VALUE_2 -4321
101        void read(int type = -1) {
102                bool bNew = false;
103                TSettings *sM = settingsManager;
104                if ((type != -1)&&(type != settingsManager->getActiveTypeID())) {
105                        if (type == REGSettings::getType()) {
106                                sM = new REGSettings();
107                                bNew = true;
108                        } else if (type == INISettings::getType()) {
109                                sM = new INISettings(file_);
110                                bNew = true;
111                        } else {
112                                throw SettingsException("Invalid settings subsystem specified");
113                        }
114                }
115                if (sM == NULL) {
116                        throw SettingsException("Invalid settings subsystem specified");
117                }
118                sectionList sections = sM->getSections();
119                for (sectionList::const_iterator it=sections.begin();it!=sections.end();++it) {
120                        sectionList section = sM->getSection(*it);
121                        for (sectionList::const_iterator it2=section.begin();it2!=section.end();++it2) {
122                                std::string s = sM->getString((*it), (*it2));
123                                int i = strEx::stoi(s);
124                                std::string s2 = strEx::itos(i);
125                                std::cout << "importing: " << (*it) << "/" << (*it2) << "=" << s << std::endl;
126                                if (s == s2) {
127                                        setInt((*it), (*it2), i);
128                                } else {
129                                        setString((*it), (*it2), s);
130                                }
131
132/*
133                                std::cout << "  Key: " << (*it2) << std::endl;
134                                int i = sM->getInt((*it), (*it2), UNLIKELY_VALUE_1);
135                                std::cout << "Int vaöl: " << i << std::endl;
136                                if (i == UNLIKELY_VALUE_1) {
137                                        if (sM->getInt((*it), (*it2), UNLIKELY_VALUE_2)==UNLIKELY_VALUE_2) {
138                                                std::cout << "Writing: " << (*it) << " - " << (*it2) << " - " << sM->getString((*it), (*it2)) << std::endl;
139                                                setString((*it), (*it2), sM->getString((*it), (*it2)));
140                                        } else
141                                                setInt((*it), (*it2), i);
142                                } else if (i == 0) {
143                                        std::string s = sM->getString((*it), (*it2));
144                                        std::cout << "Size: " << s.size() << " |" << s << "| " << std::endl;
145                                        if (s.size() == 0)
146                                                setString((*it), (*it2), s);
147                                        else
148                                                setInt((*it), (*it2), i);
149                                } else
150                                        setInt((*it), (*it2), i);
151                                        */
152                        }
153                }
154                if (bNew) {
155                        delete sM;
156                }
157        }
158        void write(int type = -1) {
159                bool bNew = false;
160                TSettings *sM = settingsManager;
161                if ((type != -1)&&(type != settingsManager->getActiveTypeID())) {
162                        if (type == REGSettings::getType()) {
163                                sM = new REGSettings();
164                                bNew = true;
165                        } else if (type == INISettings::getType()) {
166                                sM = new INISettings(file_);
167                                bNew = true;
168                        } else {
169                                throw SettingsException("Invalid settings subsystem specified");
170                        }
171                }
172                if (sM == NULL) {
173                        throw SettingsException("Invalid settings subsystem specified");
174                }
175                if (bHasInternalData) {
176                        for (saveSectionList::const_iterator it=data_.begin();it!=data_.end();++it) {
177                                for (saveKeyList::const_iterator kit = it->second.begin(); kit != it->second.end(); ++kit) {
178                                        if (kit->second.type == valueStruct::sType)
179                                                sM->setString(it->first, kit->first, kit->second.sVal);
180                                        else
181                                                sM->setInt(it->first, kit->first, kit->second.iVal);
182                                }
183                        }
184                }
185                if (bNew) {
186                        delete sM;
187                }
188        }
189
190        sectionList getSections(unsigned int bufferLength = BUFF_LEN) {
191                if (!settingsManager)
192                        throw SettingsException("No settings manager found have you configured.");
193                sectionList ret;
194                ret = settingsManager->getSections();
195                if (bHasInternalData) {
196                        for (saveSectionList::const_iterator kit = data_.begin(); kit != data_.end(); ++kit) {
197                                ret.push_back(kit->first);
198                        }
199                }
200                ret.sort();
201                ret.unique();
202                return ret;
203        }
204
205        /**
206         * Get all keys from a section as a list<string>
207         * @param section The section to return all keys from
208         * @return A list with all keys from the section
209         */
210        sectionList getSection(std::string section, unsigned int bufferLength = BUFF_LEN) {
211                if (!settingsManager)
212                        throw SettingsException("No settings manager found have you configured.");
213                sectionList ret;
214                ret = settingsManager->getSection(section);
215                if (bHasInternalData) {
216                        saveSectionList::const_iterator it = data_.find(section);
217                        if (it != data_.end()) {
218                                for (saveKeyList::const_iterator kit = it->second.begin(); kit != it->second.end(); ++kit) {
219                                        ret.push_back(kit->first);
220                                }
221                        }
222                }
223                ret.sort();
224                ret.unique();
225                return ret;
226        }
227        /**
228         * Get a string from the settings file
229         * @param section Section to read from
230         * @param key Key to retrieve
231         * @param defaultValue Default value to return if key is not found
232         * @return The value or defaultValue if the key is not found
233         */
234        std::string getString(std::string section, std::string key, std::string defaultValue = "") const {
235                if (!settingsManager)
236                        throw SettingsException("No settings manager found have you configured.");
237                if (bHasInternalData) {
238                        saveSectionList::const_iterator it = data_.find(section);
239                        if (it != data_.end()) {
240                                saveKeyList::const_iterator kit = it->second.find(key);
241                                if (kit != it->second.end()) {
242                                        if (kit->second.type == valueStruct::sType)
243                                                return kit->second.sVal;
244                                        else
245                                                return strEx::itos(kit->second.iVal);
246                                }
247                        }
248                }
249                std::string ret = settingsManager->getString(section, key, defaultValue);
250                return ret;
251        }
252
253        void setString(std::string section, std::string key, std::string value) {
254                bHasInternalData = true;
255                (data_[section])[key].sVal = value;
256                (data_[section])[key].type = valueStruct::sType;
257        }
258
259        /**
260         * Get an integer from the settings file
261         * @param section Section to read from
262         * @param key Key to retrieve
263         * @param defaultValue Default value to return if key is not found
264         * @return The value or defaultValue if the key is not found
265         */
266        int getInt(std::string section, std::string key, int defaultValue = 0) {
267                if (!settingsManager)
268                        throw SettingsException("No settings manager found have you configured.");
269                if (bHasInternalData) {
270                        saveSectionList::const_iterator it = data_.find(section);
271                        if (it != data_.end()) {
272                                saveKeyList::const_iterator kit = it->second.find(key);
273                                if (kit != it->second.end()) {
274                                        if (kit->second.type == valueStruct::sType)
275                                                return strEx::stoi(kit->second.sVal);
276                                        else
277                                                return kit->second.iVal;
278                                }
279                        }
280                }
281                return settingsManager->getInt(section, key, defaultValue);
282        }
283        void setInt(std::string section, std::string key, int value) {
284                bHasInternalData = true;
285                (data_[section])[key].iVal = value;
286                (data_[section])[key].type = valueStruct::iType;
287        }
288};
289
290typedef Singleton<SettingsT> Settings;          // Implement the settings manager as a singleton
Note: See TracBrowser for help on using the repository browser.