source: nscp/include/Settings.h @ d48c31a

0.4.00.4.10.4.2stable
Last change on this file since d48c31a was d48c31a, checked in by Michael Medin <michael@…>, 7 years ago

New build enviornment (static) to work with NT4 and w2k3 and hopefully everything else.
Also fixed the "broken password" issue..

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