Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1056 was 1056, checked in by landauf, 16 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

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