[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ConfigFileManager.h" |
---|
[2103] | 30 | |
---|
[2710] | 31 | #include <boost/filesystem.hpp> |
---|
| 32 | |
---|
[1505] | 33 | #include "util/Convert.h" |
---|
[3196] | 34 | #include "util/Math.h" |
---|
[3280] | 35 | #include "util/StringUtils.h" |
---|
[5781] | 36 | #include "ConsoleCommand.h" |
---|
[2103] | 37 | #include "ConfigValueContainer.h" |
---|
[5929] | 38 | #include "PathConfig.h" |
---|
[1505] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | ////////////////////////// |
---|
| 43 | // ConfigFileEntryValue // |
---|
| 44 | ////////////////////////// |
---|
| 45 | |
---|
[6425] | 46 | void ConfigFileEntryValue::update() |
---|
[1505] | 47 | { |
---|
[6425] | 48 | // Make sure we remove the quotes when bString changes |
---|
| 49 | if (this->bString_) |
---|
| 50 | this->value_ = stripEnclosingQuotes(this->value_); |
---|
| 51 | // Assemble the entry line |
---|
| 52 | this->fileEntry_ = this->getKeyString() + " = "; |
---|
[6432] | 53 | if (this->bString_ && !this->value_.empty()) |
---|
[6425] | 54 | this->fileEntry_ += '"' + addSlashes(this->value_) + '"'; |
---|
[1505] | 55 | else |
---|
[6425] | 56 | this->fileEntry_ += this->value_; |
---|
| 57 | if (!this->additionalComment_.empty()) |
---|
| 58 | this->fileEntry_ += ' ' + this->additionalComment_; |
---|
[1505] | 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
[2103] | 62 | //////////////////////////////// |
---|
[1505] | 63 | // ConfigFileEntryVectorValue // |
---|
[2103] | 64 | //////////////////////////////// |
---|
[6425] | 65 | void ConfigFileEntryVectorValue::update() |
---|
[1505] | 66 | { |
---|
[6425] | 67 | this->keyString_ = this->name_ + '[' + multi_cast<std::string>(this->index_) + ']'; |
---|
| 68 | ConfigFileEntryValue::update(); |
---|
[1505] | 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | /////////////////////// |
---|
| 73 | // ConfigFileSection // |
---|
| 74 | /////////////////////// |
---|
| 75 | ConfigFileSection::~ConfigFileSection() |
---|
| 76 | { |
---|
| 77 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ) |
---|
| 78 | delete (*(it++)); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void ConfigFileSection::deleteVectorEntries(const std::string& name, unsigned int startindex) |
---|
| 82 | { |
---|
| 83 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ) |
---|
| 84 | { |
---|
| 85 | if (((*it)->getName() == name) && ((*it)->getIndex() >= startindex)) |
---|
| 86 | { |
---|
| 87 | delete (*it); |
---|
| 88 | this->entries_.erase(it++); |
---|
| 89 | } |
---|
| 90 | else |
---|
| 91 | { |
---|
| 92 | ++it; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[6432] | 97 | unsigned int ConfigFileSection::getVectorSize(const std::string& name) const |
---|
[1505] | 98 | { |
---|
| 99 | unsigned int size = 0; |
---|
| 100 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 101 | if ((*it)->getName() == name) |
---|
| 102 | if ((*it)->getIndex() > size) |
---|
| 103 | size = (*it)->getIndex(); |
---|
[2662] | 104 | if (size == 0) |
---|
| 105 | return 0; |
---|
| 106 | else |
---|
| 107 | return (size + 1); |
---|
[1505] | 108 | } |
---|
| 109 | |
---|
| 110 | std::string ConfigFileSection::getFileEntry() const |
---|
| 111 | { |
---|
[6417] | 112 | if (this->additionalComment_.empty()) |
---|
| 113 | return ('[' + this->name_ + ']'); |
---|
[1505] | 114 | else |
---|
[6417] | 115 | return ('[' + this->name_ + "] " + this->additionalComment_); |
---|
[1505] | 116 | } |
---|
| 117 | |
---|
[6432] | 118 | std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const |
---|
[1505] | 119 | { |
---|
[6432] | 120 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 121 | { |
---|
| 122 | if ((*it)->getName() == name) |
---|
| 123 | return it; |
---|
| 124 | } |
---|
| 125 | return this->entries_.end(); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString) |
---|
| 129 | { |
---|
[1505] | 130 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 131 | { |
---|
| 132 | if ((*it)->getName() == name) |
---|
| 133 | { |
---|
| 134 | (*it)->setString(bString); |
---|
| 135 | return it; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | this->bUpdated_ = true; |
---|
| 140 | |
---|
[6432] | 141 | return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString)); |
---|
[1505] | 142 | } |
---|
| 143 | |
---|
[6432] | 144 | std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index) const |
---|
[1505] | 145 | { |
---|
[6432] | 146 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 147 | { |
---|
| 148 | if (((*it)->getName() == name) && ((*it)->getIndex() == index)) |
---|
| 149 | return it; |
---|
| 150 | } |
---|
| 151 | return this->entries_.end(); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 155 | { |
---|
[1505] | 156 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
---|
| 157 | { |
---|
| 158 | if (((*it)->getName() == name) && ((*it)->getIndex() == index)) |
---|
| 159 | { |
---|
| 160 | (*it)->setString(bString); |
---|
| 161 | return it; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | this->bUpdated_ = true; |
---|
| 166 | |
---|
| 167 | if (index == 0) |
---|
[6432] | 168 | return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString)); |
---|
[1505] | 169 | else |
---|
[6432] | 170 | return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString)); |
---|
[1505] | 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | //////////////// |
---|
| 175 | // ConfigFile // |
---|
| 176 | //////////////// |
---|
[6432] | 177 | ConfigFile::ConfigFile(const std::string& filename) |
---|
| 178 | : filename_(filename) |
---|
| 179 | , bUpdated_(false) |
---|
| 180 | { |
---|
| 181 | } |
---|
| 182 | |
---|
[1505] | 183 | ConfigFile::~ConfigFile() |
---|
| 184 | { |
---|
[2103] | 185 | this->clear(); |
---|
[1505] | 186 | } |
---|
| 187 | |
---|
[6432] | 188 | void ConfigFile::load() |
---|
[1505] | 189 | { |
---|
[2710] | 190 | // Be sure we start from new in the memory |
---|
[2103] | 191 | this->clear(); |
---|
[1505] | 192 | |
---|
[2710] | 193 | // Get default file if necessary and available |
---|
[5929] | 194 | boost::filesystem::path filepath(PathConfig::getConfigPath() / this->filename_); |
---|
[2710] | 195 | if (!boost::filesystem::exists(filepath)) |
---|
| 196 | { |
---|
[5695] | 197 | // Try to get default one from the data folder |
---|
[5929] | 198 | boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / "defaultConfig" / this->filename_); |
---|
[2710] | 199 | if (boost::filesystem::exists(defaultFilepath)) |
---|
| 200 | { |
---|
[2725] | 201 | COUT(3) << "Copied " << this->filename_ << " from the defaultConfig folder." << std::endl; |
---|
[2710] | 202 | boost::filesystem::copy_file(defaultFilepath, filepath); |
---|
| 203 | } |
---|
| 204 | } |
---|
[2103] | 205 | |
---|
[1505] | 206 | // Open the file |
---|
| 207 | std::ifstream file; |
---|
[2759] | 208 | file.open(filepath.string().c_str(), std::fstream::in); |
---|
[2710] | 209 | if (file.is_open()) |
---|
[1505] | 210 | { |
---|
[2710] | 211 | ConfigFileSection* newsection = 0; |
---|
[1505] | 212 | |
---|
[2710] | 213 | while (file.good() && !file.eof()) |
---|
[1505] | 214 | { |
---|
[3198] | 215 | std::string line; |
---|
| 216 | std::getline(file, line); |
---|
[1505] | 217 | |
---|
[6417] | 218 | const std::string& temp = getStripped(line); |
---|
[2710] | 219 | if (!isEmpty(temp) && !isComment(temp)) |
---|
[1505] | 220 | { |
---|
[2710] | 221 | size_t pos1 = temp.find('['); |
---|
| 222 | if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos; |
---|
| 223 | size_t pos2 = line.find(']'); |
---|
[1505] | 224 | |
---|
[2710] | 225 | if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1) |
---|
[1505] | 226 | { |
---|
[2710] | 227 | // New section |
---|
[6417] | 228 | const std::string& comment = line.substr(pos2 + 1); |
---|
[2710] | 229 | if (isComment(comment)) |
---|
| 230 | newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment); |
---|
[1505] | 231 | else |
---|
[2710] | 232 | newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1)); |
---|
| 233 | this->sections_.insert(this->sections_.end(), newsection); |
---|
| 234 | continue; |
---|
| 235 | } |
---|
| 236 | } |
---|
[1505] | 237 | |
---|
[2710] | 238 | if (newsection != 0) |
---|
| 239 | { |
---|
| 240 | if (isComment(line)) |
---|
| 241 | { |
---|
| 242 | // New comment |
---|
| 243 | newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line))); |
---|
| 244 | continue; |
---|
| 245 | } |
---|
| 246 | else |
---|
| 247 | { |
---|
| 248 | size_t pos1 = line.find('='); |
---|
| 249 | |
---|
| 250 | if (pos1 != std::string::npos && pos1 > 0) |
---|
[1505] | 251 | { |
---|
[2710] | 252 | // New entry |
---|
| 253 | size_t pos2 = line.find('['); |
---|
| 254 | size_t pos3 = line.find(']'); |
---|
| 255 | size_t commentposition = getNextCommentPosition(line, pos1 + 1); |
---|
| 256 | while (isBetweenQuotes(line, commentposition)) |
---|
[1505] | 257 | { |
---|
[2710] | 258 | commentposition = getNextCommentPosition(line, commentposition + 1); |
---|
[1505] | 259 | } |
---|
[6417] | 260 | std::string value, comment; |
---|
[2710] | 261 | if (commentposition == std::string::npos) |
---|
| 262 | { |
---|
| 263 | value = removeTrailingWhitespaces(line.substr(pos1 + 1)); |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
| 267 | value = removeTrailingWhitespaces(line.substr(pos1 + 1, commentposition - pos1 - 1)); |
---|
| 268 | comment = removeTrailingWhitespaces(line.substr(commentposition)); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1) |
---|
| 272 | { |
---|
| 273 | // There might be an array index |
---|
| 274 | unsigned int index = 0; |
---|
[3196] | 275 | if (convertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1))) |
---|
[2710] | 276 | { |
---|
| 277 | // New array |
---|
[6432] | 278 | std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false); |
---|
[2710] | 279 | (*it)->setValue(value); |
---|
| 280 | (*it)->setComment(comment); |
---|
| 281 | continue; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | // New value |
---|
| 286 | newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment)); |
---|
| 287 | continue; |
---|
[1505] | 288 | } |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
[2710] | 293 | file.close(); |
---|
[1505] | 294 | |
---|
[2710] | 295 | COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; |
---|
[1505] | 296 | |
---|
[6432] | 297 | // Save the file in case something changed (like stripped white space) |
---|
[2710] | 298 | this->save(); |
---|
| 299 | } // end file.is_open() |
---|
[1505] | 300 | } |
---|
| 301 | |
---|
| 302 | void ConfigFile::save() const |
---|
| 303 | { |
---|
[6432] | 304 | this->saveAs(this->filename_); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | void ConfigFile::saveAs(const std::string& filename) const |
---|
| 308 | { |
---|
[1505] | 309 | std::ofstream file; |
---|
[6432] | 310 | file.open((PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out); |
---|
[1505] | 311 | file.setf(std::ios::fixed, std::ios::floatfield); |
---|
| 312 | file.precision(6); |
---|
| 313 | |
---|
| 314 | if (!file.is_open()) |
---|
| 315 | { |
---|
[6432] | 316 | COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl; |
---|
[1505] | 317 | return; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 321 | { |
---|
| 322 | file << (*it)->getFileEntry() << std::endl; |
---|
| 323 | |
---|
| 324 | for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries) |
---|
| 325 | file << (*it_entries)->getFileEntry() << std::endl; |
---|
| 326 | |
---|
| 327 | file << std::endl; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | file.close(); |
---|
| 331 | |
---|
[6432] | 332 | COUT(4) << "Saved config file \"" << filename << "\"." << std::endl; |
---|
[1505] | 333 | } |
---|
| 334 | |
---|
[6432] | 335 | void ConfigFile::clear() |
---|
[1505] | 336 | { |
---|
[6432] | 337 | for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ) |
---|
| 338 | delete (*(it++)); |
---|
| 339 | this->sections_.clear(); |
---|
[1505] | 340 | } |
---|
| 341 | |
---|
[6432] | 342 | const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
[1505] | 343 | { |
---|
[6432] | 344 | const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString); |
---|
| 345 | this->saveIfUpdated(); |
---|
| 346 | return output; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 350 | { |
---|
| 351 | const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString); |
---|
| 352 | this->saveIfUpdated(); |
---|
| 353 | return output; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex) |
---|
| 357 | { |
---|
| 358 | if (ConfigFileSection* sectionPtr = this->getSection(section)) |
---|
[1505] | 359 | { |
---|
[6432] | 360 | sectionPtr->deleteVectorEntries(name, startindex); |
---|
| 361 | this->save(); |
---|
[1505] | 362 | } |
---|
| 363 | } |
---|
| 364 | |
---|
[6432] | 365 | ConfigFileSection* ConfigFile::getSection(const std::string& section) const |
---|
[2103] | 366 | { |
---|
[6432] | 367 | for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 368 | if ((*it)->getName() == section) |
---|
| 369 | return (*it); |
---|
| 370 | return NULL; |
---|
[2103] | 371 | } |
---|
| 372 | |
---|
[6432] | 373 | ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section) |
---|
[1505] | 374 | { |
---|
| 375 | for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 376 | if ((*it)->getName() == section) |
---|
| 377 | return (*it); |
---|
| 378 | |
---|
| 379 | this->bUpdated_ = true; |
---|
| 380 | |
---|
| 381 | return (*this->sections_.insert(this->sections_.end(), new ConfigFileSection(section))); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void ConfigFile::saveIfUpdated() |
---|
| 385 | { |
---|
| 386 | bool sectionsUpdated = false; |
---|
| 387 | |
---|
| 388 | for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it) |
---|
| 389 | { |
---|
| 390 | if ((*it)->bUpdated_) |
---|
| 391 | { |
---|
| 392 | sectionsUpdated = true; |
---|
| 393 | (*it)->bUpdated_ = false; |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | if (this->bUpdated_ || sectionsUpdated) |
---|
| 398 | { |
---|
| 399 | this->bUpdated_ = false; |
---|
| 400 | this->save(); |
---|
| 401 | } |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | |
---|
[6432] | 405 | //////////////////////// |
---|
| 406 | // SettingsConfigFile // |
---|
| 407 | //////////////////////// |
---|
[2103] | 408 | |
---|
[6432] | 409 | SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0; |
---|
[2103] | 410 | |
---|
[6432] | 411 | SettingsConfigFile::SettingsConfigFile(const std::string& filename) |
---|
| 412 | : ConfigFile(filename) |
---|
| 413 | { |
---|
| 414 | ConsoleCommand* command = createConsoleCommand(createFunctor(&ConfigFile::load, this), "reloadSettings"); |
---|
| 415 | CommandExecutor::addConsoleCommandShortcut(command); |
---|
| 416 | command = createConsoleCommand(createFunctor(&SettingsConfigFile::setFilename, this), "setSettingsFile"); |
---|
| 417 | CommandExecutor::addConsoleCommandShortcut(command); |
---|
| 418 | command = createConsoleCommand(createFunctor(&SettingsConfigFile::config, this), "config"); |
---|
| 419 | CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); |
---|
| 420 | command = createConsoleCommand(createFunctor(&SettingsConfigFile::tconfig, this), "tconfig"); |
---|
| 421 | CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue()); |
---|
| 422 | command = createConsoleCommand(createFunctor(&SettingsConfigFile::getConfig, this), "getConfig"); |
---|
| 423 | CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()); |
---|
| 424 | } |
---|
[2103] | 425 | |
---|
[6432] | 426 | SettingsConfigFile::~SettingsConfigFile() |
---|
[1505] | 427 | { |
---|
| 428 | } |
---|
| 429 | |
---|
[6432] | 430 | void SettingsConfigFile::load() |
---|
[1505] | 431 | { |
---|
[6432] | 432 | ConfigFile::load(); |
---|
| 433 | this->updateConfigValues(); |
---|
[1505] | 434 | } |
---|
| 435 | |
---|
[6432] | 436 | void SettingsConfigFile::setFilename(const std::string& filename) |
---|
[1505] | 437 | { |
---|
[6432] | 438 | ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename); |
---|
[1505] | 439 | } |
---|
| 440 | |
---|
[6432] | 441 | void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container) |
---|
[1505] | 442 | { |
---|
[6432] | 443 | if (container == NULL) |
---|
| 444 | return; |
---|
| 445 | std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container); |
---|
| 446 | this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second)); |
---|
| 447 | this->sectionNames_.insert(container->getSectionName()); |
---|
[1505] | 448 | } |
---|
| 449 | |
---|
[6432] | 450 | void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container) |
---|
[1505] | 451 | { |
---|
[6432] | 452 | if (container == NULL) |
---|
| 453 | return; |
---|
| 454 | const std::string& sectionLC = getLowercase(container->getSectionName()); |
---|
| 455 | ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 456 | for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) |
---|
| 457 | { |
---|
| 458 | if (it->second.second == container) |
---|
| 459 | { |
---|
| 460 | // Remove entry from section name set this was the last container for that section |
---|
| 461 | if (upper == this->containers_.lower_bound(sectionLC)) |
---|
| 462 | this->sectionNames_.erase(container->getSectionName()); |
---|
| 463 | this->containers_.erase(it); |
---|
| 464 | break; |
---|
| 465 | } |
---|
| 466 | } |
---|
[1505] | 467 | } |
---|
| 468 | |
---|
[6432] | 469 | void SettingsConfigFile::updateConfigValues() |
---|
[1505] | 470 | { |
---|
[6432] | 471 | for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it) |
---|
| 472 | { |
---|
| 473 | it->second.second->update(); |
---|
| 474 | it->second.second->getIdentifier()->updateConfigValues(); |
---|
| 475 | } |
---|
[1505] | 476 | } |
---|
| 477 | |
---|
[6432] | 478 | void SettingsConfigFile::clean(bool bCleanComments) |
---|
[1505] | 479 | { |
---|
[6432] | 480 | for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); ) |
---|
| 481 | { |
---|
| 482 | const std::string& sectionLC = getLowercase((*itSection)->getName()); |
---|
| 483 | ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC); |
---|
| 484 | ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 485 | if (lower != upper) |
---|
| 486 | { |
---|
| 487 | // The section exists, delete comment |
---|
| 488 | if (bCleanComments) |
---|
| 489 | (*itSection)->setComment(""); |
---|
| 490 | for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); ) |
---|
| 491 | { |
---|
| 492 | const std::string& entryLC = getLowercase((*itEntry)->getName()); |
---|
| 493 | bool bFound = false; |
---|
| 494 | for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer) |
---|
| 495 | { |
---|
| 496 | if (itContainer->second.first == entryLC) |
---|
| 497 | { |
---|
| 498 | // The config-value exists, delete comment |
---|
| 499 | if (bCleanComments) |
---|
| 500 | (*itEntry)->setComment(""); |
---|
| 501 | ++itEntry; |
---|
| 502 | bFound = true; |
---|
| 503 | break; |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | if (!bFound) |
---|
| 507 | { |
---|
| 508 | // The config-value doesn't exist |
---|
| 509 | delete (*itEntry); |
---|
| 510 | (*itSection)->entries_.erase(itEntry++); |
---|
| 511 | } |
---|
| 512 | } |
---|
| 513 | ++itSection; |
---|
| 514 | } |
---|
| 515 | else |
---|
| 516 | { |
---|
| 517 | // The section doesn't exist |
---|
| 518 | delete (*itSection); |
---|
| 519 | this->sections_.erase(itSection++); |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | // Save the file |
---|
| 524 | this->save(); |
---|
[1505] | 525 | } |
---|
| 526 | |
---|
[6432] | 527 | bool SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value) |
---|
[1505] | 528 | { |
---|
[6432] | 529 | return this->configImpl(section, entry, value, &ConfigValueContainer::set); |
---|
[1505] | 530 | } |
---|
| 531 | |
---|
[6432] | 532 | bool SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value) |
---|
[1505] | 533 | { |
---|
[6432] | 534 | return this->configImpl(section, entry, value, &ConfigValueContainer::tset); |
---|
[1505] | 535 | } |
---|
| 536 | |
---|
[6432] | 537 | bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)) |
---|
[1505] | 538 | { |
---|
[6432] | 539 | const std::string& sectionLC = getLowercase(section); |
---|
| 540 | const std::string& entryLC = getLowercase(entry); |
---|
| 541 | ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 542 | for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) |
---|
| 543 | { |
---|
| 544 | // Note: Config value vectors cannot be supported |
---|
| 545 | if (it->second.first == entryLC && !it->second.second->isVector()) |
---|
| 546 | { |
---|
| 547 | return (it->second.second->*function)(value); |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | return false; |
---|
[1505] | 551 | } |
---|
| 552 | |
---|
[6432] | 553 | std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry) |
---|
[1505] | 554 | { |
---|
[6432] | 555 | const std::string& sectionLC = getLowercase(section); |
---|
| 556 | const std::string& entryLC = getLowercase(entry); |
---|
| 557 | ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC); |
---|
| 558 | for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it) |
---|
| 559 | { |
---|
| 560 | // Note: Config value vectors cannot be supported |
---|
| 561 | if (it->second.first == entryLC && ! it->second.second->isVector()) |
---|
| 562 | { |
---|
| 563 | std::string value; |
---|
| 564 | it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL); |
---|
| 565 | return value; |
---|
| 566 | } |
---|
| 567 | } |
---|
| 568 | return ""; |
---|
[1505] | 569 | } |
---|
| 570 | |
---|
[6432] | 571 | |
---|
| 572 | /////////////////////// |
---|
| 573 | // ConfigFileManager // |
---|
| 574 | /////////////////////// |
---|
| 575 | |
---|
| 576 | ConfigFileManager* ConfigFileManager::singletonPtr_s = 0; |
---|
| 577 | |
---|
| 578 | ConfigFileManager::ConfigFileManager() |
---|
[1505] | 579 | { |
---|
[6432] | 580 | this->configFiles_.assign(NULL); |
---|
[1505] | 581 | } |
---|
| 582 | |
---|
[6432] | 583 | ConfigFileManager::~ConfigFileManager() |
---|
[1505] | 584 | { |
---|
[6432] | 585 | for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it) |
---|
| 586 | if (*it) |
---|
| 587 | delete (*it); |
---|
[1505] | 588 | } |
---|
| 589 | |
---|
[6432] | 590 | void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename) |
---|
[1505] | 591 | { |
---|
[6432] | 592 | if (this->getConfigFile(type)) |
---|
| 593 | delete this->configFiles_[type]; |
---|
| 594 | // Create and load config file |
---|
| 595 | switch (type) |
---|
[2103] | 596 | { |
---|
[6432] | 597 | case ConfigFileType::Settings: |
---|
| 598 | this->configFiles_[type] = new SettingsConfigFile(filename); |
---|
| 599 | break; |
---|
| 600 | case ConfigFileType::JoyStickCalibration: |
---|
| 601 | case ConfigFileType::CommandHistory: |
---|
| 602 | this->configFiles_[type] = new ConfigFile(filename); |
---|
| 603 | break; |
---|
[2103] | 604 | } |
---|
[6432] | 605 | this->configFiles_[type]->load(); |
---|
[1505] | 606 | } |
---|
| 607 | } |
---|