Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 2, 2008, 12:22:42 AM (15 years ago)
Author:
rgrieder
Message:

Merged r2101 (objecthierarchy) to trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • code/trunk/src/core/ConfigFileManager.cc

    r1889 r2103  
    2828
    2929#include "ConfigFileManager.h"
    30 #include "ConfigValueContainer.h"
    31 #include "ConsoleCommand.h"
    32 #include "Identifier.h"
     30
     31#include <cassert>
    3332#include "util/Convert.h"
    3433#include "util/String.h"
    35 
     34#include "ConsoleCommand.h"
     35#include "ConfigValueContainer.h"
    3636
    3737namespace orxonox
     
    3939    const int CONFIG_FILE_MAX_LINELENGHT  = 1024;
    4040    const char* const DEFAULT_CONFIG_FILE = "default.ini";
     41
     42    ConfigFileManager* ConfigFileManager::singletonRef_s = 0;
    4143
    4244    SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
     
    4547    SetConsoleCommandShortcutExtern(cleanConfig);
    4648    SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files());
    47     SetConsoleCommandShortcutExtern(loadKeybindings).argumentCompleter(0, autocompletion::files());
    4849
    4950    bool config(const std::string& classname, const std::string& varname, const std::string& value)
     
    8384    void loadSettings(const std::string& filename)
    8485    {
    85         ConfigFileManager::getInstance().setFile(CFT_Settings, filename, false);
    86     }
    87 
    88     void loadKeybindings(const std::string& filename)
    89     {
    90         ConfigFileManager::getInstance().setFile(CFT_Keybindings, filename);
    91     }
    92 
     86        ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);
     87    }
    9388
    9489    //////////////////////////
     
    121116
    122117
    123     ///////////////////////////////
     118    ////////////////////////////////
    124119    // ConfigFileEntryVectorValue //
    125     ///////////////////////////////
     120    ////////////////////////////////
    126121    std::string ConfigFileEntryVectorValue::getFileEntry() const
    127122    {
     
    217212    ConfigFile::~ConfigFile()
    218213    {
    219         for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); )
    220             delete (*(it++));
     214        this->clear();
    221215    }
    222216
    223217    void ConfigFile::load(bool bCreateIfNotExisting)
    224218    {
    225         if (bCreateIfNotExisting)
    226         {
    227             // This creates the default config file if it's not existing
    228             std::ofstream createFile;
    229             createFile.open(this->filename_.c_str(), std::fstream::app);
    230             createFile.close();
    231         }
     219        // Be sure we start from new
     220        this->clear();
     221
     222        // This creates the config file if it's not existing
     223        std::ofstream createFile;
     224        createFile.open(this->filename_.c_str(), std::fstream::app);
     225        createFile.close();
    232226
    233227        // Open the file
     
    329323        file.close();
    330324
    331         COUT(0) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
     325        COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
    332326
    333327        // Save the file in case something changed (like stripped whitespaces)
    334328        this->save();
     329
     330        // Update all ConfigValueContainers
     331        this->updateConfigValues();
    335332    }
    336333
     
    366363    }
    367364
    368     void ConfigFile::save(const std::string& filename)
     365    void ConfigFile::saveAs(const std::string& filename)
    369366    {
    370367        std::string temp = this->filename_;
     
    415412    }
    416413
     414    void ConfigFile::clear()
     415    {
     416        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); )
     417            delete (*(it++));
     418        this->sections_.clear();
     419    }
     420
    417421    ConfigFileSection* ConfigFile::getSection(const std::string& section)
    418422    {
     
    446450    }
    447451
     452    void ConfigFile::updateConfigValues()
     453    {
     454        if (this->type_ == ConfigFileType::Settings)
     455        {
     456            for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it)
     457            {
     458                if (it->second->hasConfigValues())
     459                {
     460                    for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = (*it).second->getConfigValueMapBegin(); it2 != (*it).second->getConfigValueMapEnd(); ++it2)
     461                        it2->second->update();
     462
     463                    it->second->updateConfigValues();
     464                }
     465            }
     466        }
     467    }
     468
    448469
    449470    ///////////////////////
    450471    // ConfigFileManager //
    451472    ///////////////////////
     473
    452474    ConfigFileManager::ConfigFileManager()
    453     {
    454         this->setFile(CFT_Settings, DEFAULT_CONFIG_FILE);
     475         : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)
     476    {
     477        assert(singletonRef_s == 0);
     478        singletonRef_s = this;
    455479    }
    456480
     
    458482    {
    459483        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); )
    460             delete (*(it++)).second;
    461     }
    462 
    463     ConfigFileManager& ConfigFileManager::getInstance()
    464     {
    465         static ConfigFileManager instance;
    466         return instance;
    467     }
    468 
    469     void ConfigFileManager::setFile(ConfigFileType type, const std::string& filename, bool bCreateIfNotExisting)
     484            delete (it++)->second;
     485
     486        assert(singletonRef_s != 0);
     487        singletonRef_s = 0;
     488    }
     489
     490    void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename)
    470491    {
    471492        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
    472493        if (it != this->configFiles_.end())
    473             if ((*it).second != 0)
    474                 delete (*it).second;
    475 
    476         this->configFiles_[type] = new ConfigFile(this->getFilePath(filename));
    477         this->load(type, bCreateIfNotExisting);
    478     }
    479 
    480     void ConfigFileManager::load(bool bCreateIfNotExisting)
     494        {
     495            assert(it->second);
     496            delete it->second;
     497        }
     498        this->configFiles_[type] = new ConfigFile(filename, type);
     499        this->load(type);
     500    }
     501
     502    void ConfigFileManager::load()
    481503    {
    482504        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    483             (*it).second->load(bCreateIfNotExisting);
    484 
    485         this->updateConfigValues();
     505            it->second->load();
    486506    }
    487507
     
    489509    {
    490510        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    491             (*it).second->save();
     511            it->second->save();
    492512    }
    493513
     
    495515    {
    496516        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    497             this->clean((*it).first, bCleanComments);
    498     }
    499 
    500     void ConfigFileManager::load(ConfigFileType type, bool bCreateIfNotExisting)
    501     {
    502         this->getFile(type)->load(bCreateIfNotExisting);
    503         this->updateConfigValues(type);
     517            this->clean(it->first, bCleanComments);
     518    }
     519
     520    void ConfigFileManager::load(ConfigFileType type)
     521    {
     522        this->getFile(type)->load();
    504523    }
    505524
     
    509528    }
    510529
    511     void ConfigFileManager::save(ConfigFileType type, const std::string& filename)
    512     {
    513         this->getFile(type)->save(filename);
     530    void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename)
     531    {
     532        this->getFile(type)->saveAs(saveFilename);
    514533    }
    515534
     
    519538    }
    520539
    521     void ConfigFileManager::updateConfigValues() const
     540    void ConfigFileManager::updateConfigValues()
    522541    {
    523542        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    524             this->updateConfigValues((*it).first);
    525     }
    526 
    527     void ConfigFileManager::updateConfigValues(ConfigFileType type) const
    528     {
    529         if (type == CFT_Settings)
    530         {
    531             for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it)
    532             {
    533                 if ((*it).second->hasConfigValues() /* && (*it).second != ClassIdentifier<KeyBinder>::getIdentifier()*/)
    534                 {
    535                     for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = (*it).second->getConfigValueMapBegin(); it2 != (*it).second->getConfigValueMapEnd(); ++it2)
    536                         (*it2).second->update();
    537 
    538                     (*it).second->updateConfigValues();
    539                 }
    540             }
    541         }
    542         else if (type == CFT_Keybindings)
    543         {
    544             // todo
    545         }
     543            it->second->updateConfigValues();
     544    }
     545
     546    void ConfigFileManager::updateConfigValues(ConfigFileType type)
     547    {
     548        this->getFile(type)->updateConfigValues();
     549    }
     550
     551    const std::string& ConfigFileManager::getFilename(ConfigFileType type)
     552    {
     553        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
     554        if (it != this->configFiles_.end())
     555            return it->second->getFilename();
     556        else
     557            return BLANKSTRING;
    546558    }
    547559
    548560    ConfigFile* ConfigFileManager::getFile(ConfigFileType type)
    549561    {
    550         std::map<ConfigFileType, ConfigFile*>::iterator it = this->configFiles_.find(type);
     562        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
    551563        if (it != this->configFiles_.end())
    552             return (*it).second;
    553 
    554         if (type == CFT_Settings)
    555             return this->configFiles_[type] = new ConfigFile(DEFAULT_CONFIG_FILE);
    556         else
    557             return this->configFiles_[type] = new ConfigFile("");
    558     }
    559 
    560     std::string ConfigFileManager::getFilePath(const std::string& name) const
    561     {
    562         return name;
     564            return it->second;
     565        else
     566        {
     567            COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << (int)type << std::endl;
     568            COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl;
     569            this->setFilename(type, DEFAULT_CONFIG_FILE);
     570            return getFile(type);
     571        }
    563572    }
    564573}
Note: See TracChangeset for help on using the changeset viewer.