Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/ConfigValueContainer.cc @ 1320

Last change on this file since 1320 was 1313, checked in by landauf, 17 years ago
  • implemented Shell, but not yet linked with the graphical console
  • added new features (cursor, OIS::KeyCode listener) to InputBuffer
  • changed some includes to avoid circular header-dependencies in OrxonoxClass and Shell
File size: 15.1 KB
RevLine 
[513]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[513]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:
[670]23 *      Fabian 'x3n' Landau
[513]24 *   Co-authors:
25 *      ...
26 *
27 */
28
[871]29/**
30    @file ConfigValueContainer.cc
31    @brief Implementation of the ConfigValueContainer class.
32*/
33
[1062]34#include "ConfigValueContainer.h"
35
[497]36#include <fstream>
[682]37
[1062]38#include "util/SubString.h"
39#include "util/Convert.h"
[1052]40#include "Language.h"
41#include "Identifier.h"
[497]42
[1052]43#define MAX_VECTOR_INDEX 255 // to avoid up to 4*10^9 vector entries in the config file after accidentally using a wrong argument
[497]44
[1052]45
[497]46namespace orxonox
47{
48    /**
[667]49        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
[1052]50        @param type The type of the corresponding config-file
51        @param identifier The identifier of the class the variable belongs to
[497]52        @param varname The name of the variable
53        @param defvalue The default-value
54    */
[1052]55    ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue)
[497]56    {
[1052]57        this->type_ = type;
58        this->identifier_ = identifier;
59        this->sectionname_ = identifier->getName();
[667]60        this->varname_ = varname;
[497]61
[1052]62        this->value_ = defvalue;
63        this->bAddedDescription_ = false;
64        this->bIsVector_ = false;
[497]65
[1052]66        this->defvalueString_ = defvalue.toString();
67        this->update();
[497]68    }
69
70    /**
[1052]71        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
72        @param type The type of the corresponding config-file
73        @param identifier The identifier of the class the variable belongs to
74        @param varname The name of the variable
75        @param defvalue The default-value
[497]76    */
[1052]77    ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue)
[667]78    {
[1052]79        this->type_ = type;
80        this->identifier_ = identifier;
81        this->sectionname_ = identifier->getName();
82        this->varname_ = varname;
[667]83
[1052]84        this->valueVector_ = defvalue;
85        this->bAddedDescription_ = false;
86        this->bIsVector_ = true;
[667]87
[1052]88        if (defvalue.size() > 0)
89            this->value_ = defvalue[0];
[667]90
[1052]91        for (unsigned int i = 0; i < defvalue.size(); i++)
92            ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, defvalue[i].toString(), this->value_.isA(MT_string));
[667]93
[1052]94        for (unsigned int i = 0; i < defvalue.size(); i++)
95            this->defvalueStringVector_.push_back(defvalue[i].toString());
[667]96
[1052]97        this->update();
[667]98    }
99
100    /**
[1052]101        @brief Adds a new entry to the end of the vector.
102        @param input The new entry
103        @return True if the new entry was successfully added
[667]104    */
[1052]105    bool ConfigValueContainer::add(const std::string& input)
[667]106    {
[1052]107        if (this->bIsVector_)
108            return this->set(this->valueVector_.size(), input);
[667]109
[1052]110        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
111        return false;
[497]112    }
113
114    /**
[1052]115        @brief Removes an existing entry from the vector.
116        @param index The index of the entry
117        @return True if the entry was removed
[703]118    */
[1052]119    bool ConfigValueContainer::remove(unsigned int index)
[703]120    {
[1052]121        if (this->bIsVector_)
122        {
123            if (index < this->valueVector_.size())
124            {
[1313]125                // Erase the entry from the vector, change (shift) all entries beginning with index in the config file, remove the last entry from the file
[1052]126                this->valueVector_.erase(this->valueVector_.begin() + index);
127                for (unsigned int i = index; i < this->valueVector_.size(); i++)
128                    ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isA(MT_string));
129                ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->valueVector_.size());
[703]130
[1052]131                return true;
132            }
133            COUT(1) << "Error: Invalid vector-index." << std::endl;
134        }
[703]135
[1052]136        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
137        return false;
[703]138    }
139
140    /**
[1052]141        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
142        @param input The new value
143        @return True if the new value was successfully assigned
[703]144    */
[1052]145    bool ConfigValueContainer::set(const std::string& input)
[703]146    {
[1052]147        if (this->bIsVector_)
148        {
149            SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
150            int index = -1;
151            bool success = false;
[703]152
[1052]153            if (token.size() > 0)
154                success = ConvertValue(&index, token[0]);
[703]155
[1052]156            if (!success || index < 0 || index > MAX_VECTOR_INDEX)
157            {
158                if (!success)
159                {
160                    COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl;
161                }
162                else
163                {
164                    COUT(1) << "Error: Invalid vector-index." << std::endl;
165                }
166                return false;
167            }
[703]168
[1052]169            if (token.size() >= 2)
170                return this->set(index, token.subSet(1).join());
171            else
172                return this->set(index, "");
173        }
[703]174
[1052]175        bool success = this->tset(input);
176        ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input, this->value_.isA(MT_string));
[871]177        return success;
[703]178    }
179
180    /**
[1052]181        @brief Assigns a new value to the config-value of all objects and writes the change into the config-file.
182        @param index The index in the vector
183        @param input The new value
184        @return True if the new value was successfully assigned
[703]185    */
[1052]186    bool ConfigValueContainer::set(unsigned int index, const std::string& input)
[703]187    {
[1052]188        if (this->bIsVector_)
[703]189        {
[1052]190            bool success = this->tset(index, input);
191            ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, index, input, this->value_.isA(MT_string));
[871]192            return success;
[703]193        }
194
[1052]195        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
196        return false;
[703]197    }
198
199    /**
[1052]200        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
201        @param input The new value
202        @return True if the new value was successfully assigned
[703]203    */
[1052]204    bool ConfigValueContainer::tset(const std::string& input)
[703]205    {
[1052]206        bool success = this->parse(input);
207        if (this->identifier_)
208            this->identifier_->updateConfigValues();
209        return success;
[703]210    }
211
212    /**
[1052]213        @brief Assigns a new value to the config-value of all objects, but doesn't change the config-file (t stands for temporary).
214        @param index The index in the vector
215        @param input The new value
216        @return True if the new value was successfully assigned
[703]217    */
[1052]218    bool ConfigValueContainer::tset(unsigned int index, const std::string& input)
[703]219    {
[1052]220        if (this->bIsVector_)
[703]221        {
[1052]222            bool success = this->parse(index, input);
223            if (this->identifier_)
224                this->identifier_->updateConfigValues();
225            return success;
[703]226        }
227
[1052]228        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
[703]229        return false;
230    }
231
232    /**
[1052]233        @brief Sets the value of the variable back to the default value and resets the config-file entry.
[703]234    */
[1052]235    bool ConfigValueContainer::reset()
[703]236    {
[1052]237        if (!this->bIsVector_)
238            return this->set(this->defvalueString_);
239        else
[703]240        {
[1052]241            bool success = true;
242            for (unsigned int i = 0; i < this->defvalueStringVector_.size(); i++)
243                if (!this->set(i, this->defvalueStringVector_[i]))
244                    success = false;
245            ConfigFileManager::getSingleton()->deleteVectorEntries(this->type_, this->sectionname_, this->varname_, this->defvalueStringVector_.size());
246            return success;
[703]247        }
248    }
249
250    /**
[1052]251        @brief Retrieves the configured value from the currently loaded config-file.
[703]252    */
[1052]253    void ConfigValueContainer::update()
[703]254    {
[1052]255        if (!this->bIsVector_)
256            this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, this->defvalueString_, this->value_.isA(MT_string)));
257        else
[497]258        {
[1052]259            this->valueVector_.clear();
260            for (unsigned int i = 0; i < ConfigFileManager::getSingleton()->getVectorSize(this->type_, this->sectionname_, this->varname_); i++)
[703]261            {
[1052]262                this->value_.fromString(ConfigFileManager::getSingleton()->getValue(this->type_, this->sectionname_, this->varname_, i, this->defvalueStringVector_[i], this->value_.isA(MT_string)));
263                this->valueVector_.push_back(this->value_);
[703]264            }
265        }
266    }
267
268    /**
[1052]269        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
[703]270        @param input The string to convert
271        @return True if the string was successfully parsed
272    */
[1052]273    bool ConfigValueContainer::parse(const std::string& input)
[703]274    {
[1052]275        if (this->bIsVector_)
276        {
277            SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
278            int index = -1;
279            bool success = false;
[703]280
[1052]281            if (token.size() > 0)
282                success = ConvertValue(&index, token[0]);
283
284            if (!success || index < 0 || index > MAX_VECTOR_INDEX)
[703]285            {
[1052]286                if (!success)
287                {
288                    COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is a vector." << std::endl;
289                }
290                else
291                {
292                    COUT(1) << "Error: Invalid vector-index." << std::endl;
293                }
[703]294                return false;
295            }
296
[1052]297            if (token.size() >= 2)
298                return this->parse(index, token.subSet(1).join());
299            else
300                return this->parse(index, "");
[497]301        }
[703]302
[1052]303        MultiTypeMath temp = this->value_;
304        if (temp.fromString(input))
[871]305        {
[1052]306            this->value_ = temp;
[871]307            return true;
308        }
309        return false;
310    }
311
312    /**
[1052]313        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
314        @param index The index in the vector
[871]315        @param input The string to convert
316        @return True if the string was successfully parsed
317    */
[1052]318    bool ConfigValueContainer::parse(unsigned int index, const std::string& input)
[871]319    {
[1052]320        if (this->bIsVector_)
[497]321        {
[1052]322            if (index >= this->valueVector_.size())
[497]323            {
[1052]324                for (unsigned int i = this->valueVector_.size(); i <= index; i++)
[497]325                {
[1052]326                    this->valueVector_.push_back(MultiTypeMath());
327                    ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, i, this->valueVector_[i], this->value_.isA(MT_string));
[497]328                }
[1052]329            }
[497]330
[1052]331            MultiTypeMath temp = this->value_;
332            if (temp.fromString(input))
333            {
334                this->valueVector_[index] = temp;
335                return true;
[497]336            }
[1052]337            return false;
[497]338        }
339
[1052]340        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
341        return false;
[497]342    }
343
344    /**
[1052]345        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
346        @param input The string to convert
347        @param defvalue The default value to assign if the parsing fails
348        @return True if the string was successfully parsed
[497]349    */
[1052]350    bool ConfigValueContainer::parse(const std::string& input, const MultiTypeMath& defvalue)
[497]351    {
[1052]352        if (this->parse(input))
[497]353            return true;
354
[1052]355        this->value_ = defvalue;
[497]356        return false;
357    }
358
359    /**
[1052]360        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
361        @param index The index in the vector
362        @param input The string to convert
363        @param defvalue The default value to assign if the parsing fails
364        @return True if the string was successfully parsed
[497]365    */
[1052]366    bool ConfigValueContainer::parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue)
[497]367    {
[1052]368        if (this->bIsVector_)
[704]369        {
[1052]370            if (this->parse(index, input))
371                return true;
[704]372
[1052]373            this->valueVector_[index] = defvalue;
374            return false;
[497]375        }
376
[1052]377        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
378        return false;
[497]379    }
380
381    /**
[705]382        @brief Adds a description to the config-value.
383        @param description The description
384    */
[715]385    void ConfigValueContainer::description(const std::string& description)
[705]386    {
387        if (!this->bAddedDescription_)
388        {
[1052]389            this->description_ = std::string("ConfigValueDescription::" + this->identifier_->getName() + "::" + this->varname_);
[871]390            AddLanguageEntry(this->description_, description);
[705]391            this->bAddedDescription_ = true;
392        }
393    }
[871]394
395    /**
396        @brief Returns the description of the config-value.
397        @return The description
398    */
399    const std::string& ConfigValueContainer::getDescription() const
400    {
401        return GetLocalisation(this->description_);
402    }
[497]403}
Note: See TracBrowser for help on using the repository browser.