Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1313 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
Line 
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 ConfigValueContainer.cc
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#define MAX_VECTOR_INDEX 255 // to avoid up to 4*10^9 vector entries in the config file after accidentally using a wrong argument
44
45
46namespace orxonox
47{
48    /**
49        @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
50        @param type The type of the corresponding config-file
51        @param identifier The identifier of the class the variable belongs to
52        @param varname The name of the variable
53        @param defvalue The default-value
54    */
55    ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue)
56    {
57        this->type_ = type;
58        this->identifier_ = identifier;
59        this->sectionname_ = identifier->getName();
60        this->varname_ = varname;
61
62        this->value_ = defvalue;
63        this->bAddedDescription_ = false;
64        this->bIsVector_ = false;
65
66        this->defvalueString_ = defvalue.toString();
67        this->update();
68    }
69
70    /**
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
76    */
77    ConfigValueContainer::ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue)
78    {
79        this->type_ = type;
80        this->identifier_ = identifier;
81        this->sectionname_ = identifier->getName();
82        this->varname_ = varname;
83
84        this->valueVector_ = defvalue;
85        this->bAddedDescription_ = false;
86        this->bIsVector_ = true;
87
88        if (defvalue.size() > 0)
89            this->value_ = defvalue[0];
90
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));
93
94        for (unsigned int i = 0; i < defvalue.size(); i++)
95            this->defvalueStringVector_.push_back(defvalue[i].toString());
96
97        this->update();
98    }
99
100    /**
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
104    */
105    bool ConfigValueContainer::add(const std::string& input)
106    {
107        if (this->bIsVector_)
108            return this->set(this->valueVector_.size(), input);
109
110        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
111        return false;
112    }
113
114    /**
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
118    */
119    bool ConfigValueContainer::remove(unsigned int index)
120    {
121        if (this->bIsVector_)
122        {
123            if (index < this->valueVector_.size())
124            {
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
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());
130
131                return true;
132            }
133            COUT(1) << "Error: Invalid vector-index." << std::endl;
134        }
135
136        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
137        return false;
138    }
139
140    /**
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
144    */
145    bool ConfigValueContainer::set(const std::string& input)
146    {
147        if (this->bIsVector_)
148        {
149            SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
150            int index = -1;
151            bool success = false;
152
153            if (token.size() > 0)
154                success = ConvertValue(&index, token[0]);
155
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            }
168
169            if (token.size() >= 2)
170                return this->set(index, token.subSet(1).join());
171            else
172                return this->set(index, "");
173        }
174
175        bool success = this->tset(input);
176        ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, input, this->value_.isA(MT_string));
177        return success;
178    }
179
180    /**
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
185    */
186    bool ConfigValueContainer::set(unsigned int index, const std::string& input)
187    {
188        if (this->bIsVector_)
189        {
190            bool success = this->tset(index, input);
191            ConfigFileManager::getSingleton()->setValue(this->type_, this->sectionname_, this->varname_, index, input, this->value_.isA(MT_string));
192            return success;
193        }
194
195        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
196        return false;
197    }
198
199    /**
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
203    */
204    bool ConfigValueContainer::tset(const std::string& input)
205    {
206        bool success = this->parse(input);
207        if (this->identifier_)
208            this->identifier_->updateConfigValues();
209        return success;
210    }
211
212    /**
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
217    */
218    bool ConfigValueContainer::tset(unsigned int index, const std::string& input)
219    {
220        if (this->bIsVector_)
221        {
222            bool success = this->parse(index, input);
223            if (this->identifier_)
224                this->identifier_->updateConfigValues();
225            return success;
226        }
227
228        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
229        return false;
230    }
231
232    /**
233        @brief Sets the value of the variable back to the default value and resets the config-file entry.
234    */
235    bool ConfigValueContainer::reset()
236    {
237        if (!this->bIsVector_)
238            return this->set(this->defvalueString_);
239        else
240        {
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;
247        }
248    }
249
250    /**
251        @brief Retrieves the configured value from the currently loaded config-file.
252    */
253    void ConfigValueContainer::update()
254    {
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
258        {
259            this->valueVector_.clear();
260            for (unsigned int i = 0; i < ConfigFileManager::getSingleton()->getVectorSize(this->type_, this->sectionname_, this->varname_); i++)
261            {
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_);
264            }
265        }
266    }
267
268    /**
269        @brief Parses a given std::string into a value of the type of the associated variable and assigns it.
270        @param input The string to convert
271        @return True if the string was successfully parsed
272    */
273    bool ConfigValueContainer::parse(const std::string& input)
274    {
275        if (this->bIsVector_)
276        {
277            SubString token(input, " ", "", true, '"', false, '(', ')', false, '\0');
278            int index = -1;
279            bool success = false;
280
281            if (token.size() > 0)
282                success = ConvertValue(&index, token[0]);
283
284            if (!success || index < 0 || index > MAX_VECTOR_INDEX)
285            {
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                }
294                return false;
295            }
296
297            if (token.size() >= 2)
298                return this->parse(index, token.subSet(1).join());
299            else
300                return this->parse(index, "");
301        }
302
303        MultiTypeMath temp = this->value_;
304        if (temp.fromString(input))
305        {
306            this->value_ = temp;
307            return true;
308        }
309        return false;
310    }
311
312    /**
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
315        @param input The string to convert
316        @return True if the string was successfully parsed
317    */
318    bool ConfigValueContainer::parse(unsigned int index, const std::string& input)
319    {
320        if (this->bIsVector_)
321        {
322            if (index >= this->valueVector_.size())
323            {
324                for (unsigned int i = this->valueVector_.size(); i <= index; i++)
325                {
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));
328                }
329            }
330
331            MultiTypeMath temp = this->value_;
332            if (temp.fromString(input))
333            {
334                this->valueVector_[index] = temp;
335                return true;
336            }
337            return false;
338        }
339
340        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
341        return false;
342    }
343
344    /**
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
349    */
350    bool ConfigValueContainer::parse(const std::string& input, const MultiTypeMath& defvalue)
351    {
352        if (this->parse(input))
353            return true;
354
355        this->value_ = defvalue;
356        return false;
357    }
358
359    /**
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
365    */
366    bool ConfigValueContainer::parse(unsigned int index, const std::string& input, const MultiTypeMath& defvalue)
367    {
368        if (this->bIsVector_)
369        {
370            if (this->parse(index, input))
371                return true;
372
373            this->valueVector_[index] = defvalue;
374            return false;
375        }
376
377        COUT(1) << "Error: Config-value '" << this->varname_ << "' in " << this->sectionname_ << " is not a vector." << std::endl;
378        return false;
379    }
380
381    /**
382        @brief Adds a description to the config-value.
383        @param description The description
384    */
385    void ConfigValueContainer::description(const std::string& description)
386    {
387        if (!this->bAddedDescription_)
388        {
389            this->description_ = std::string("ConfigValueDescription::" + this->identifier_->getName() + "::" + this->varname_);
390            AddLanguageEntry(this->description_, description);
391            this->bAddedDescription_ = true;
392        }
393    }
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    }
403}
Note: See TracBrowser for help on using the repository browser.