| 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 | /** |
|---|
| 30 | @file |
|---|
| 31 | @brief Implementation of ConfigFileSection. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "ConfigFileSection.h" |
|---|
| 35 | |
|---|
| 36 | #include "ConfigFileEntryValue.h" |
|---|
| 37 | #include "ConfigFileEntryVectorValue.h" |
|---|
| 38 | |
|---|
| 39 | namespace orxonox |
|---|
| 40 | { |
|---|
| 41 | /////////////////////// |
|---|
| 42 | // ConfigFileSection // |
|---|
| 43 | /////////////////////// |
|---|
| 44 | /** |
|---|
| 45 | @brief Destructor: Deletes all entries. |
|---|
| 46 | */ |
|---|
| 47 | ConfigFileSection::~ConfigFileSection() |
|---|
| 48 | { |
|---|
| 49 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ) |
|---|
| 50 | delete (*(it++)); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | @brief Deletes all elements of a config vector if their index is greater or equal to @a startindex. |
|---|
| 55 | |
|---|
| 56 | @param name The name of the vector |
|---|
| 57 | @param startindex The index of the first element that will be deleted |
|---|
| 58 | */ |
|---|
| 59 | void ConfigFileSection::deleteVectorEntries(const std::string& name, unsigned int startindex) |
|---|
| 60 | { |
|---|
| 61 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ) |
|---|
| 62 | { |
|---|
| 63 | if (((*it)->getName() == name) && ((*it)->getIndex() >= startindex)) |
|---|
| 64 | { |
|---|
| 65 | delete (*it); |
|---|
| 66 | this->entries_.erase(it++); |
|---|
| 67 | } |
|---|
| 68 | else |
|---|
| 69 | { |
|---|
| 70 | ++it; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | @brief Returns the size of a config vector. |
|---|
| 77 | @param name The name of the vector |
|---|
| 78 | */ |
|---|
| 79 | unsigned int ConfigFileSection::getVectorSize(const std::string& name) const |
|---|
| 80 | { |
|---|
| 81 | unsigned int size = 0; |
|---|
| 82 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
|---|
| 83 | if ((*it)->getName() == name) |
|---|
| 84 | if ((*it)->getIndex() >= size) |
|---|
| 85 | size = (*it)->getIndex() + 1; |
|---|
| 86 | return size; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | @brief Returns the title and comment of the section as it will be stored in the file. |
|---|
| 91 | */ |
|---|
| 92 | std::string ConfigFileSection::getFileEntry() const |
|---|
| 93 | { |
|---|
| 94 | if (this->additionalComment_.empty()) |
|---|
| 95 | return ('[' + this->name_ + ']'); |
|---|
| 96 | else |
|---|
| 97 | return ('[' + this->name_ + "] " + this->additionalComment_); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | @brief Returns the entry with given name (or NULL if it doesn't exist). |
|---|
| 102 | |
|---|
| 103 | @param name The name of the entry |
|---|
| 104 | */ |
|---|
| 105 | ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name) const |
|---|
| 106 | { |
|---|
| 107 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
|---|
| 108 | { |
|---|
| 109 | if ((*it)->getName() == name) |
|---|
| 110 | return *it; |
|---|
| 111 | } |
|---|
| 112 | return NULL; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | @brief Returns the entry of a vector element with given name and index (or NULL if it doesn't exist). |
|---|
| 117 | |
|---|
| 118 | @param name The name of the vector |
|---|
| 119 | @param index The index of the element in the vector |
|---|
| 120 | */ |
|---|
| 121 | ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name, unsigned int index) const |
|---|
| 122 | { |
|---|
| 123 | for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
|---|
| 124 | { |
|---|
| 125 | if (((*it)->getName() == name) && ((*it)->getIndex() == index)) |
|---|
| 126 | return *it; |
|---|
| 127 | } |
|---|
| 128 | return NULL; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | @brief Returns the iterator to the entry with given name. If the entry doesn't exist, it is created using the fallback value. |
|---|
| 133 | |
|---|
| 134 | @param name The name of the entry |
|---|
| 135 | @param fallback The value that will be used if the entry doesn't exist |
|---|
| 136 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
|---|
| 137 | */ |
|---|
| 138 | std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString) |
|---|
| 139 | { |
|---|
| 140 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
|---|
| 141 | { |
|---|
| 142 | if ((*it)->getName() == name) |
|---|
| 143 | { |
|---|
| 144 | (*it)->setString(bString); |
|---|
| 145 | return it; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | this->bUpdated_ = true; |
|---|
| 150 | |
|---|
| 151 | return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString)); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | @brief Returns the iterator to the entry of a vector element with given name and index. If the entry doesn't exist, it is created using the fallback value. |
|---|
| 156 | |
|---|
| 157 | @param name The name of the vector |
|---|
| 158 | @param index The index of the element in the vector |
|---|
| 159 | @param fallback The value that will be used if the entry doesn't exist |
|---|
| 160 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
|---|
| 161 | */ |
|---|
| 162 | std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
|---|
| 163 | { |
|---|
| 164 | for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) |
|---|
| 165 | { |
|---|
| 166 | if (((*it)->getName() == name) && ((*it)->getIndex() == index)) |
|---|
| 167 | { |
|---|
| 168 | (*it)->setString(bString); |
|---|
| 169 | return it; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | this->bUpdated_ = true; |
|---|
| 174 | |
|---|
| 175 | if (index == 0) |
|---|
| 176 | return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString)); |
|---|
| 177 | else |
|---|
| 178 | return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString)); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|