Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/ConfigFileManager.cc @ 2726

Last change on this file since 2726 was 2726, checked in by rgrieder, 15 years ago

Still fixing a bug.

  • Property svn:eol-style set to native
File size: 21.6 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#include "ConfigFileManager.h"
30
31#include <cassert>
32#include <boost/filesystem.hpp>
33
34#include "util/Convert.h"
35#include "util/String.h"
36#include "ConsoleCommand.h"
37#include "ConfigValueContainer.h"
38#include "Core.h"
39
40namespace orxonox
41{
42    const int CONFIG_FILE_MAX_LINELENGHT  = 1024;
43    const char* const DEFAULT_CONFIG_FILE = "default.ini";
44
45    ConfigFileManager* ConfigFileManager::singletonRef_s = 0;
46
47    SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
48    SetConsoleCommandShortcutExtern(tconfig).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
49    SetConsoleCommandShortcutExtern(reloadConfig);
50    SetConsoleCommandShortcutExtern(cleanConfig);
51    SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files());
52
53    bool config(const std::string& classname, const std::string& varname, const std::string& value)
54    {
55        std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseIdentifierMap().find(getLowercase(classname));
56        if (identifier != Identifier::getLowercaseIdentifierMapEnd())
57        {
58            std::map<std::string, ConfigValueContainer*>::const_iterator variable = (*identifier).second->getLowercaseConfigValueMap().find(getLowercase(varname));
59            if (variable != (*identifier).second->getLowercaseConfigValueMapEnd())
60                return (*variable).second->set(value);
61        }
62        return false;
63    }
64
65    bool tconfig(const std::string& classname, const std::string& varname, const std::string& value)
66    {
67        std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseIdentifierMap().find(getLowercase(classname));
68        if (identifier != Identifier::getLowercaseIdentifierMapEnd())
69        {
70            std::map<std::string, ConfigValueContainer*>::const_iterator variable = (*identifier).second->getLowercaseConfigValueMap().find(getLowercase(varname));
71            if (variable != (*identifier).second->getLowercaseConfigValueMapEnd())
72                return (*variable).second->tset(value);
73        }
74        return false;
75    }
76
77    void reloadConfig()
78    {
79        ConfigFileManager::getInstance().load();
80    }
81
82    void cleanConfig()
83    {
84        ConfigFileManager::getInstance().clean(false);
85    }
86
87    void loadSettings(const std::string& filename)
88    {
89        ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);
90    }
91
92    //////////////////////////
93    // ConfigFileEntryValue //
94    //////////////////////////
95
96    void ConfigFileEntryValue::setValue(const std::string& value)
97    {
98        if (!this->bString_)
99            this->value_ = value;
100        else
101            this->value_ = "\"" + addSlashes(stripEnclosingQuotes(value)) + "\"";
102    }
103
104    std::string ConfigFileEntryValue::getValue() const
105    {
106        if (!this->bString_)
107            return this->value_;
108        else
109            return removeSlashes(stripEnclosingQuotes(this->value_));
110    }
111
112    std::string ConfigFileEntryValue::getFileEntry() const
113    {
114        if (this->additionalComment_ == "" || this->additionalComment_.size() == 0)
115            return (this->name_ + "=" + this->value_);
116        else
117            return (this->name_ + "=" + this->value_ + " " + this->additionalComment_);
118    }
119
120
121    ////////////////////////////////
122    // ConfigFileEntryVectorValue //
123    ////////////////////////////////
124    std::string ConfigFileEntryVectorValue::getFileEntry() const
125    {
126        if (this->additionalComment_ == "" || this->additionalComment_.size() == 0)
127            return (this->name_ + "[" + getConvertedValue<unsigned int, std::string>(this->index_, "0") + "]" + "=" + this->value_);
128        else
129            return (this->name_ + "[" + getConvertedValue<unsigned int, std::string>(this->index_, "0") + "]=" + this->value_ + " " + this->additionalComment_);
130    }
131
132
133    ///////////////////////
134    // ConfigFileSection //
135    ///////////////////////
136    ConfigFileSection::~ConfigFileSection()
137    {
138        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); )
139            delete (*(it++));
140    }
141
142    void ConfigFileSection::deleteVectorEntries(const std::string& name, unsigned int startindex)
143    {
144        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); )
145        {
146            if (((*it)->getName() == name) && ((*it)->getIndex() >= startindex))
147            {
148                delete (*it);
149                this->entries_.erase(it++);
150            }
151            else
152            {
153                ++it;
154            }
155        }
156    }
157
158    unsigned int ConfigFileSection::getVectorSize(const std::string& name)
159    {
160        unsigned int size = 0;
161        for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
162            if ((*it)->getName() == name)
163                if ((*it)->getIndex() > size)
164                    size = (*it)->getIndex();
165        if (size == 0)
166            return 0;
167        else
168            return (size + 1);
169    }
170
171    std::string ConfigFileSection::getFileEntry() const
172    {
173        if (this->additionalComment_ == "" || this->additionalComment_.size() == 0)
174            return ("[" + this->name_ + "]");
175        else
176            return ("[" + this->name_ + "] " + this->additionalComment_);
177    }
178
179    std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, const std::string& fallback, bool bString)
180    {
181        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
182        {
183            if ((*it)->getName() == name)
184            {
185                (*it)->setString(bString);
186                return it;
187            }
188        }
189
190        this->bUpdated_ = true;
191
192        return this->entries_.insert(this->entries_.end(), (ConfigFileEntry*)(new ConfigFileEntryValue(name, fallback, bString)));
193    }
194
195    std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
196    {
197        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
198        {
199            if (((*it)->getName() == name) && ((*it)->getIndex() == index))
200            {
201                (*it)->setString(bString);
202                return it;
203            }
204        }
205
206        this->bUpdated_ = true;
207
208        if (index == 0)
209            return this->entries_.insert(this->entries_.end(), (ConfigFileEntry*)(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
210        else
211            return this->entries_.insert(++this->getEntryIterator(name, index - 1, "", bString), (ConfigFileEntry*)(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
212    }
213
214
215    ////////////////
216    // ConfigFile //
217    ////////////////
218    ConfigFile::~ConfigFile()
219    {
220        this->clear();
221    }
222
223    void ConfigFile::load(bool bCreateIfNotExisting)
224    {
225        // Be sure we start from new in the memory
226        this->clear();
227
228        // Get default file if necessary and available
229        boost::filesystem::path filepath(Core::getConfigPath() / this->filename_);
230        COUT(0) << "config filepath: " << filepath.string() << std::endl;
231        if (!boost::filesystem::exists(filepath))
232        {
233            // Try to get default one from the media folder
234            boost::filesystem::path defaultFilepath(Core::getMediaPath() / "defaultConfig" / this->filename_);
235            COUT(0) << "default filepath: " << defaultFilepath.string() << std::endl;
236            if (boost::filesystem::exists(defaultFilepath))
237            {
238                COUT(3) << "Copied " << this->filename_ << " from the defaultConfig folder." << std::endl;
239                boost::filesystem::copy_file(defaultFilepath, filepath);
240            }
241        }
242
243        // Open the file
244        std::ifstream file;
245        file.open(filepath.file_string().c_str(), std::fstream::in);
246        if (file.is_open())
247        {
248
249            char linearray[CONFIG_FILE_MAX_LINELENGHT];
250
251            ConfigFileSection* newsection = 0;
252
253            while (file.good() && !file.eof())
254            {
255                file.getline(linearray, CONFIG_FILE_MAX_LINELENGHT);
256
257                std::string line = std::string(linearray);
258
259                std::string temp = getStripped(line);
260                if (!isEmpty(temp) && !isComment(temp))
261                {
262                    size_t   pos1 = temp.find('[');
263                    if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos;
264                    size_t   pos2 = line.find(']');
265
266                    if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1)
267                    {
268                        // New section
269                        std::string comment = line.substr(pos2 + 1);
270                        if (isComment(comment))
271                            newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment);
272                        else
273                            newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1));
274                        this->sections_.insert(this->sections_.end(), newsection);
275                        continue;
276                    }
277                }
278
279                if (newsection != 0)
280                {
281                    if (isComment(line))
282                    {
283                        // New comment
284                        newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line)));
285                        continue;
286                    }
287                    else
288                    {
289                        size_t pos1 = line.find('=');
290
291                        if (pos1 != std::string::npos && pos1 > 0)
292                        {
293                            // New entry
294                            size_t pos2 = line.find('[');
295                            size_t pos3 = line.find(']');
296                            size_t commentposition = getNextCommentPosition(line, pos1 + 1);
297                            while (isBetweenQuotes(line, commentposition))
298                            {
299                                commentposition = getNextCommentPosition(line, commentposition + 1);
300                            }
301                            std::string value = "", comment = "";
302                            if (commentposition == std::string::npos)
303                            {
304                                value = removeTrailingWhitespaces(line.substr(pos1 + 1));
305                            }
306                            else
307                            {
308                                value = removeTrailingWhitespaces(line.substr(pos1 + 1, commentposition - pos1 - 1));
309                                comment = removeTrailingWhitespaces(line.substr(commentposition));
310                            }
311
312                            if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1)
313                            {
314                                // There might be an array index
315                                unsigned int index = 0;
316                                if (ConvertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1)))
317                                {
318                                    // New array
319                                    std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
320                                    (*it)->setValue(value);
321                                    (*it)->setComment(comment);
322                                    continue;
323                                }
324                            }
325
326                            // New value
327                            newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment));
328                            continue;
329                        }
330                    }
331                }
332            }
333
334            file.close();
335
336            COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
337
338            // Save the file in case something changed (like stripped whitespaces)
339            this->save();
340
341            // Update all ConfigValueContainers
342            this->updateConfigValues();
343        } // end file.is_open()
344    }
345
346    void ConfigFile::save() const
347    {
348        boost::filesystem::path filepath(Core::getConfigPath() / this->filename_);
349
350        std::ofstream file;
351        file.open(filepath.file_string().c_str(), std::fstream::out);
352        file.setf(std::ios::fixed, std::ios::floatfield);
353        file.precision(6);
354
355        if (!file.is_open())
356        {
357            COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl;
358            COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl;
359            return;
360        }
361
362        for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
363        {
364            file << (*it)->getFileEntry() << std::endl;
365
366            for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries)
367            {
368                file << (*it_entries)->getFileEntry() << std::endl;
369            }
370
371            file << std::endl;
372        }
373
374        file.close();
375
376        COUT(4) << "Saved config file \"" << this->filename_ << "\"." << std::endl;
377    }
378
379    void ConfigFile::saveAs(const std::string& filename)
380    {
381        std::string temp = this->filename_;
382        this->filename_ = filename;
383        this->save();
384        this->filename_ = temp;
385    }
386
387    void ConfigFile::clean(bool bCleanComments)
388    {
389        for (std::list<ConfigFileSection*>::iterator it1 = this->sections_.begin(); it1 != this->sections_.end(); )
390        {
391            std::map<std::string, Identifier*>::const_iterator it2 = Identifier::getIdentifierMap().find((*it1)->getName());
392            if (it2 != Identifier::getIdentifierMapEnd() && (*it2).second->hasConfigValues())
393            {
394                // The section exists, delete comment
395                if (bCleanComments)
396                    (*it1)->setComment("");
397                for (std::list<ConfigFileEntry*>::iterator it3 = (*it1)->entries_.begin(); it3 != (*it1)->entries_.end(); )
398                {
399                    std::map<std::string, ConfigValueContainer*>::const_iterator it4 = (*it2).second->getConfigValueMap().find((*it3)->getName());
400                    if (it4 != (*it2).second->getConfigValueMapEnd())
401                    {
402                        // The config-value exists, delete comment
403                        if (bCleanComments)
404                            (*it3)->setComment("");
405                        ++it3;
406                    }
407                    else
408                    {
409                        // The config-value doesn't exist
410                        delete (*it3);
411                        (*it1)->entries_.erase(it3++);
412                    }
413                }
414                ++it1;
415            }
416            else
417            {
418                // The section doesn't exist
419                delete (*it1);
420                this->sections_.erase(it1++);
421            }
422        }
423
424        // Save the file
425        this->save();
426    }
427
428    void ConfigFile::clear()
429    {
430        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); )
431            delete (*(it++));
432        this->sections_.clear();
433    }
434
435    ConfigFileSection* ConfigFile::getSection(const std::string& section)
436    {
437        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
438            if ((*it)->getName() == section)
439                return (*it);
440
441        this->bUpdated_ = true;
442
443        return (*this->sections_.insert(this->sections_.end(), new ConfigFileSection(section)));
444    }
445
446    void ConfigFile::saveIfUpdated()
447    {
448        bool sectionsUpdated = false;
449
450        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
451        {
452            if ((*it)->bUpdated_)
453            {
454                sectionsUpdated = true;
455                (*it)->bUpdated_ = false;
456            }
457        }
458
459        if (this->bUpdated_ || sectionsUpdated)
460        {
461            this->bUpdated_ = false;
462            this->save();
463        }
464    }
465
466    void ConfigFile::updateConfigValues()
467    {
468        if (this->type_ == ConfigFileType::Settings)
469        {
470            for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it)
471            {
472                if (it->second->hasConfigValues())
473                {
474                    for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = (*it).second->getConfigValueMapBegin(); it2 != (*it).second->getConfigValueMapEnd(); ++it2)
475                        it2->second->update();
476
477                    it->second->updateConfigValues();
478                }
479            }
480        }
481    }
482
483
484    ///////////////////////
485    // ConfigFileManager //
486    ///////////////////////
487
488    ConfigFileManager::ConfigFileManager()
489         : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)
490    {
491        assert(singletonRef_s == 0);
492        singletonRef_s = this;
493    }
494
495    ConfigFileManager::~ConfigFileManager()
496    {
497        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); )
498            delete (it++)->second;
499
500        assert(singletonRef_s != 0);
501        singletonRef_s = 0;
502    }
503
504    void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename)
505    {
506        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
507        if (it != this->configFiles_.end())
508        {
509            assert(it->second);
510            delete it->second;
511        }
512        this->configFiles_[type] = new ConfigFile(filename, type);
513        this->load(type);
514    }
515
516    void ConfigFileManager::load()
517    {
518        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
519            it->second->load();
520    }
521
522    void ConfigFileManager::save()
523    {
524        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
525            it->second->save();
526    }
527
528    void ConfigFileManager::clean(bool bCleanComments)
529    {
530        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
531            this->clean(it->first, bCleanComments);
532    }
533
534    void ConfigFileManager::load(ConfigFileType type)
535    {
536        this->getFile(type)->load();
537    }
538
539    void ConfigFileManager::save(ConfigFileType type)
540    {
541        this->getFile(type)->save();
542    }
543
544    void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename)
545    {
546        this->getFile(type)->saveAs(saveFilename);
547    }
548
549    void ConfigFileManager::clean(ConfigFileType type, bool bCleanComments)
550    {
551        this->getFile(type)->clean(bCleanComments);
552    }
553
554    void ConfigFileManager::updateConfigValues()
555    {
556        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
557            it->second->updateConfigValues();
558    }
559
560    void ConfigFileManager::updateConfigValues(ConfigFileType type)
561    {
562        this->getFile(type)->updateConfigValues();
563    }
564
565    const std::string& ConfigFileManager::getFilename(ConfigFileType type)
566    {
567        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
568        if (it != this->configFiles_.end())
569            return it->second->getFilename();
570        else
571            return BLANKSTRING;
572    }
573
574    ConfigFile* ConfigFileManager::getFile(ConfigFileType type)
575    {
576        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
577        if (it != this->configFiles_.end())
578            return it->second;
579        else
580        {
581            COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << (int)type << std::endl;
582            COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl;
583            this->setFilename(type, DEFAULT_CONFIG_FILE);
584            return getFile(type);
585        }
586    }
587}
Note: See TracBrowser for help on using the repository browser.