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