Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/ConfigValueContainer.cc @ 2989

Last change on this file since 2989 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 11.9 KB
RevLine 
[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/**
[2171]30    @file
[1505]31    @brief Implementation of the ConfigValueContainer class.
32*/
33
34#include "ConfigValueContainer.h"
35
36#include <fstream>
37
38#include "util/SubString.h"
39#include "util/Convert.h"
40#include "Language.h"
41#include "Identifier.h"
42
43
44
45namespace orxonox
46{
[1791]47    const unsigned int MAX_VECTOR_INDEX  = 255; // to avoid up to 4*10^9 vector entries in the config file after accidentally using a wrong argument
[1784]48
[1505]49    /**
[1747]50        @brief Initializes the ConfigValueContainer with defaultvalues.
[1505]51    */
[1887]52    void ConfigValueContainer::init(ConfigFileType type, Identifier* identifier, const std::string& sectionname, const std::string& varname)
[1505]53    {
54        this->type_ = type;
55        this->identifier_ = identifier;
[1887]56        this->sectionname_ = sectionname;
[1505]57        this->varname_ = varname;
[1747]58        this->callback_ = 0;
59        this->bContainerIsNew_ = true;
60        this->bDoInitialCallback_ = false;
61        this->bAddedDescription_ = false;
62    }
[1505]63
[1747]64    /**
65        @brief Does some special initialization for single config-values.
66    */
67    void ConfigValueContainer::initValue(const MultiType& defvalue)
68    {
[1505]69        this->value_ = defvalue;
70        this->bIsVector_ = false;
71
[1747]72        this->defvalueString_ = this->value_.getString();
[1505]73        this->update();
74    }
75
76    /**
[1747]77        @brief Does some special initialization for vector config-values.
[1505]78    */
[1747]79    void ConfigValueContainer::initVector()
[1505]80    {
81        this->bIsVector_ = true;
82
[1747]83        for (unsigned int i = 0; i < this->valueVector_.size(); i++)
[1505]84        {
[1795]85            ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_string));
[1747]86            this->defvalueStringVector_.push_back(this->valueVector_[i]);
87        }
[1505]88
[1747]89        this->update();
90    }
[1505]91
[1747]92    /**
93        @brief Destructor: Deletes the callback object if necessary.
94    */
95    ConfigValueContainer::~ConfigValueContainer()
96    {
97        if (this->callback_)
98            delete this->callback_;
[1505]99    }
100
101    /**
102        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
103        @param input The new value
104        @return True if the new value was successfully assigned
105    */
[1747]106    bool ConfigValueContainer::set(const MultiType& input)
[1505]107    {
108        if (this->bIsVector_)
109        {
[1747]110            return this->callFunctionWithIndex(&ConfigValueContainer::set, input);
[1505]111        }
112        else
113        {
114            if (this->tset(input))
115            {
[1795]116                ConfigFileManager::getInstance().setValue(this->type_, this->sectionname_, this->varname_, input, this->value_.isType(MT_string));
[1505]117                return true;
118            }
119        }
120        return false;
121    }
122
123    /**
124        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
125        @param index The index in the vector
126        @param input The new value
127        @return True if the new value was successfully assigned
128    */
[1747]129    bool ConfigValueContainer::set(unsigned int index, const MultiType& input)
[1505]130    {
131        if (this->bIsVector_)
132        {
133            if (this->tset(index, input))
134            {
[1795]135                ConfigFileManager::getInstance().setValue(this->type_, this->sectionname_, this->varname_, index, input, this->value_.isType(MT_string));
[1505]136                return true;
137            }
138        }
139        else
140        {
141            COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
142        }
143        return false;
144    }
145
146    /**
147        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
148        @param input The new value. If bIsVector_ then write "index value"
149        @return True if the new value was successfully assigned
150    */
[1747]151    bool ConfigValueContainer::tset(const MultiType& input)
[1505]152    {
153        if (this->bIsVector_)
154        {
[1747]155            return this->callFunctionWithIndex(&ConfigValueContainer::tset, input);
156            return false;
[1505]157        }
158        else
159        {
[1747]160            this->value_ = input;
[1505]161
[1747]162            if (this->identifier_)
163                this->identifier_->updateConfigValues();
164
165            return true;
[1505]166        }
167    }
168
169    /**
170        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
171        @param index The index in the vector
172        @param input The new value
173        @return True if the new value was successfully assigned
174    */
[1747]175    bool ConfigValueContainer::tset(unsigned int index, const MultiType& input)
[1505]176    {
177        if (this->bIsVector_)
178        {
179            if (index > MAX_VECTOR_INDEX)
180            {
181                COUT(1) << "Error: Index " << index << " is too large." << std::endl;
182                return false;
183            }
184
185            if (index >= this->valueVector_.size())
186            {
187                for (unsigned int i = this->valueVector_.size(); i <= index; i++)
188                {
[1747]189                    this->valueVector_.push_back(MultiType());
[1505]190                }
191            }
192
[1747]193            this->valueVector_[index] = input;
[1505]194
[1747]195            if (this->identifier_)
196                this->identifier_->updateConfigValues();
[1505]197
[1747]198            return true;
[1505]199        }
200        else
201        {
202            COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
[1747]203            return false;
[1505]204        }
205    }
206
207    /**
208        @brief Adds a new entry to the end of the vector.
209        @param input The new entry
210        @return True if the new entry was successfully added
211    */
[1747]212    bool ConfigValueContainer::add(const MultiType& input)
[1505]213    {
214        if (this->bIsVector_)
215            return this->set(this->valueVector_.size(), input);
216
217        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
218        return false;
219    }
220
221    /**
222        @brief Removes an existing entry from the vector.
223        @param index The index of the entry
224        @return True if the entry was removed
225    */
226    bool ConfigValueContainer::remove(unsigned int index)
227    {
228        if (this->bIsVector_)
229        {
230            if (index < this->valueVector_.size())
231            {
232                // Erase the entry from the vector, change (shift) all entries beginning with index in the config file, remove the last entry from the file
233                this->valueVector_.erase(this->valueVector_.begin() + index);
234                for (unsigned int i = index; i < this->valueVector_.size(); i++)
[1795]235                    ConfigFileManager::getInstance().setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isType(MT_string));
236                ConfigFileManager::getInstance().deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->valueVector_.size());
[1505]237
238                return true;
239            }
240            COUT(1) << "Error: Invalid vector-index." << std::endl;
241        }
242
243        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
244        return false;
245    }
246
247    /**
248        @brief Sets the value of the variable back to the default value and resets the config-file entry.
249    */
250    bool ConfigValueContainer::reset()
251    {
252        if (!this->bIsVector_)
253            return this->set(this->defvalueString_);
254        else
255        {
256            bool success = true;
257            for (unsigned int i = 0; i < this->defvalueStringVector_.size(); i++)
258                if (!this->set(i, this->defvalueStringVector_[i]))
259                    success = false;
[1795]260            ConfigFileManager::getInstance().deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->defvalueStringVector_.size());
[1505]261            return success;
262        }
263    }
264
265    /**
266        @brief Retrieves the configured value from the currently loaded config-file.
267    */
268    void ConfigValueContainer::update()
269    {
270        if (!this->bIsVector_)
[1795]271            this->value_ = ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_, this->value_.isType(MT_string));
[1505]272        else
273        {
274            this->valueVector_.clear();
[2662]275            unsigned int vectorSize = ConfigFileManager::getInstance().getVectorSize(this->type_, this->sectionname_, this->varname_);
276            for (unsigned int i = 0; i < vectorSize; i++)
[1505]277            {
278                if (i < this->defvalueStringVector_.size())
279                {
[1795]280                    this->value_ = ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isType(MT_string));
[1505]281                }
282                else
283                {
[1795]284                    this->value_ = ConfigFileManager::getInstance().getValue(this->type_, this->sectionname_, this->varname_, i, MultiType(), this->value_.isType(MT_string));
[1505]285                }
286
287                this->valueVector_.push_back(this->value_);
288            }
289        }
290    }
291
292    /**
293        @brief Calls the given function with parsed index and the parsed argument from the input string.
294        @param function The function to call
295        @param input The input string
296        @return The returnvalue of the functioncall
297    */
[1747]298    bool ConfigValueContainer::callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiType&), const std::string& input)
[1505]299    {
300        SubString token(input, " ", SubString::WhiteSpaces, true, '\\', false, '"', false, '(', ')', false, '\0');
301        int index = -1;
302        bool success = false;
303
304        if (token.size() > 0)
305            success = ConvertValue(&index, token[0]);
306
[1791]307        if (!success || index < 0 || index > (signed int)MAX_VECTOR_INDEX)
[1505]308        {
309            if (!success)
310            {
311                COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl;
312            }
313            else
314            {
315                COUT(1) << "Error: Invalid vector-index." << std::endl;
316            }
317            return false;
318        }
319
320        if (token.size() >= 2)
321            return (this->*function)(index, token.subSet(1).join());
322        else
323            return (this->*function)(index, "");
324    }
325
326    /**
327        @brief Adds a description to the config-value.
328        @param description The description
329    */
[1747]330    ConfigValueContainer& ConfigValueContainer::description(const std::string& description)
[1505]331    {
332        if (!this->bAddedDescription_)
333        {
[1887]334            this->description_ = std::string("ConfigValueDescription::" + this->sectionname_ + "::" + this->varname_);
[1505]335            AddLanguageEntry(this->description_, description);
336            this->bAddedDescription_ = true;
337        }
[1747]338        return (*this);
[1505]339    }
340
341    /**
342        @brief Returns the description of the config-value.
343        @return The description
344    */
345    const std::string& ConfigValueContainer::getDescription() const
346    {
347        return GetLocalisation(this->description_);
348    }
349}
Note: See TracBrowser for help on using the repository browser.