Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 29, 2009, 10:30:19 PM (14 years ago)
Author:
rgrieder
Message:

Changed the way config values associated with general settings (ConfigFileType::Settings) are handled:

  • ConfigFileManager only handles config files listed in the ConfigFileType enum (normal enum again)
  • ConfigFileManager only takes care of ConfigFiles and returns a pointer to the right one, just two functions left. —> use like: ConfigFileManager::getInstance().getConfigFile(myType)→doSomething();
  • Moved all code (except for the argument completion functions) relating to ConfigFileType::Settings to a new class: SettingsConfigFile, which is a Singleton (it doesn't make sense to have multiple instances unless you start coding a lot more)
  • SettingsConfigFile handles config value containers according to their section and entry in the ini file, not according to class and variables names. (In most cases it will be class and variable names though)
  • SettingsConfigFile supports:
    • clear() (removes any file entries not associated to a config value container)
    • updateConfigValues() (does exactly that through the identifier)
    • config, tconfig and getConfig
    • commands listed above are exported to tolua, and tconfig, config and getConfig were given shortcuts in Lua (e.g. orxonox.config)
  • If you need to organise ConfigFiles yourself, just do it without the ConfigFileManager, like the KeyBinder does.
  • All getValue() functions have been split into getOrCreateValue() and getValue(), which is const
  • Removed obsolete config value management code in the Identifier (it still stores and destroys them and provides access to them)

All of that leads to one HUGE advantage:
"config OutputHandler softDebugLevelInGameConsole"
works now :D (any further implications are up to the reader…)
(it didn't work before because the actual config value container is in the InGameConsole singleton)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gamestate/src/libraries/core/ConfigFileManager.cc

    r6425 r6432  
    4040namespace orxonox
    4141{
    42     SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
    43     SetConsoleCommandShortcutExtern(tconfig).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
    44     SetConsoleCommandShortcutExtern(reloadConfig);
    45     SetConsoleCommandShortcutExtern(cleanConfig);
    46     SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files());
    47 
    48     bool config(const std::string& classname, const std::string& varname, const std::string& value)
    49     {
    50         std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseStringIdentifierMap().find(getLowercase(classname));
    51         if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())
    52         {
    53             std::map<std::string, ConfigValueContainer*>::const_iterator variable = identifier->second->getLowercaseConfigValueMap().find(getLowercase(varname));
    54             if (variable != identifier->second->getLowercaseConfigValueMapEnd())
    55                 return variable->second->set(value);
    56         }
    57         return false;
    58     }
    59 
    60     const std::string& getConfig(const std::string& classname, const std::string& varname)
    61     {
    62         return ConfigFileManager::getInstance().getValue(ConfigFileType::Settings, classname, varname, "", true);
    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::getLowercaseStringIdentifierMap().find(getLowercase(classname));
    68         if (identifier != Identifier::getLowercaseStringIdentifierMapEnd())
    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 
    9242    //////////////////////////
    9343    // ConfigFileEntryValue //
     
    10151        // Assemble the entry line
    10252        this->fileEntry_ = this->getKeyString() + " = ";
    103         if (this->bString_)
     53        if (this->bString_ && !this->value_.empty())
    10454            this->fileEntry_ += '"' + addSlashes(this->value_) + '"';
    10555        else
     
    14595    }
    14696
    147     unsigned int ConfigFileSection::getVectorSize(const std::string& name)
     97    unsigned int ConfigFileSection::getVectorSize(const std::string& name) const
    14898    {
    14999        unsigned int size = 0;
     
    166116    }
    167117
    168     std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, const std::string& fallback, bool bString)
     118    std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const
     119    {
     120        for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     121        {
     122            if ((*it)->getName() == name)
     123                return it;
     124        }
     125        return this->entries_.end();
     126    }
     127
     128    std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString)
    169129    {
    170130        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     
    179139        this->bUpdated_ = true;
    180140
    181         return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryValue(name, fallback, bString)));
    182     }
    183 
    184     std::list<ConfigFileEntry*>::iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
     141        return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString));
     142    }
     143
     144    std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index) const
     145    {
     146        for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     147        {
     148            if (((*it)->getName() == name) && ((*it)->getIndex() == index))
     149                return it;
     150        }
     151        return this->entries_.end();
     152    }
     153
     154    std::list<ConfigFileEntry*>::iterator ConfigFileSection::getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
    185155    {
    186156        for (std::list<ConfigFileEntry*>::iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)
     
    196166
    197167        if (index == 0)
    198             return this->entries_.insert(this->entries_.end(), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
     168            return this->entries_.insert(this->entries_.end(), new ConfigFileEntryVectorValue(name, index, fallback, bString));
    199169        else
    200             return this->entries_.insert(++this->getEntryIterator(name, index - 1, "", bString), static_cast<ConfigFileEntry*>(new ConfigFileEntryVectorValue(name, index, fallback, bString)));
     170            return this->entries_.insert(++this->getOrCreateEntryIterator(name, index - 1, "", bString), new ConfigFileEntryVectorValue(name, index, fallback, bString));
    201171    }
    202172
     
    205175    // ConfigFile //
    206176    ////////////////
     177    ConfigFile::ConfigFile(const std::string& filename)
     178        : filename_(filename)
     179        , bUpdated_(false)
     180    {
     181    }
     182
    207183    ConfigFile::~ConfigFile()
    208184    {
     
    210186    }
    211187
    212     void ConfigFile::load(bool bCreateIfNotExisting)
     188    void ConfigFile::load()
    213189    {
    214190        // Be sure we start from new in the memory
     
    300276                                {
    301277                                    // New array
    302                                     std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
     278                                    std::list<ConfigFileEntry*>::iterator it = newsection->getOrCreateEntryIterator(getStripped(line.substr(0, pos2)), index, value, false);
    303279                                    (*it)->setValue(value);
    304280                                    (*it)->setComment(comment);
     
    319295            COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
    320296
    321             // Save the file in case something changed (like stripped whitespaces)
     297            // Save the file in case something changed (like stripped white space)
    322298            this->save();
    323 
    324             // Update all ConfigValueContainers
    325             this->updateConfigValues();
    326299        } // end file.is_open()
    327300    }
     
    329302    void ConfigFile::save() const
    330303    {
     304        this->saveAs(this->filename_);
     305    }
     306
     307    void ConfigFile::saveAs(const std::string& filename) const
     308    {
    331309        std::ofstream file;
    332         file.open((PathConfig::getConfigPathString() + filename_).c_str(), std::fstream::out);
     310        file.open((PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out);
    333311        file.setf(std::ios::fixed, std::ios::floatfield);
    334312        file.precision(6);
     
    336314        if (!file.is_open())
    337315        {
    338             COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl;
    339             COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl;
     316            COUT(1) << "Error: Couldn't open config-file \"" << filename << "\"." << std::endl;
    340317            return;
    341318        }
     
    346323
    347324            for (std::list<ConfigFileEntry*>::const_iterator it_entries = (*it)->getEntriesBegin(); it_entries != (*it)->getEntriesEnd(); ++it_entries)
    348             {
    349325                file << (*it_entries)->getFileEntry() << std::endl;
    350             }
    351326
    352327            file << std::endl;
     
    355330        file.close();
    356331
    357         COUT(4) << "Saved config file \"" << this->filename_ << "\"." << std::endl;
    358     }
    359 
    360     void ConfigFile::saveAs(const std::string& filename)
    361     {
    362         std::string temp = this->filename_;
    363         this->filename_ = filename;
    364         this->save();
    365         this->filename_ = temp;
    366     }
    367 
    368     void ConfigFile::clean(bool bCleanComments)
    369     {
    370         for (std::list<ConfigFileSection*>::iterator it1 = this->sections_.begin(); it1 != this->sections_.end(); )
    371         {
    372             std::map<std::string, Identifier*>::const_iterator it2 = Identifier::getStringIdentifierMap().find((*it1)->getName());
    373             if (it2 != Identifier::getStringIdentifierMapEnd() && it2->second->hasConfigValues())
    374             {
    375                 // The section exists, delete comment
    376                 if (bCleanComments)
    377                     (*it1)->setComment("");
    378                 for (std::list<ConfigFileEntry*>::iterator it3 = (*it1)->entries_.begin(); it3 != (*it1)->entries_.end(); )
    379                 {
    380                     std::map<std::string, ConfigValueContainer*>::const_iterator it4 = it2->second->getConfigValueMap().find((*it3)->getName());
    381                     if (it4 != it2->second->getConfigValueMapEnd())
    382                     {
    383                         // The config-value exists, delete comment
    384                         if (bCleanComments)
    385                             (*it3)->setComment("");
    386                         ++it3;
    387                     }
    388                     else
    389                     {
    390                         // The config-value doesn't exist
    391                         delete (*it3);
    392                         (*it1)->entries_.erase(it3++);
    393                     }
    394                 }
    395                 ++it1;
    396             }
    397             else
    398             {
    399                 // The section doesn't exist
    400                 delete (*it1);
    401                 this->sections_.erase(it1++);
    402             }
    403         }
    404 
    405         // Save the file
    406         this->save();
     332        COUT(4) << "Saved config file \"" << filename << "\"." << std::endl;
    407333    }
    408334
     
    414340    }
    415341
    416     ConfigFileSection* ConfigFile::getSection(const std::string& section)
     342    const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString)
     343    {
     344        const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString);
     345        this->saveIfUpdated();
     346        return output;
     347    }
     348
     349    const std::string& ConfigFile::getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
     350    {
     351        const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString);
     352        this->saveIfUpdated();
     353        return output;
     354    }
     355
     356    void ConfigFile::deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex)
     357    {
     358        if (ConfigFileSection* sectionPtr = this->getSection(section))
     359        {
     360            sectionPtr->deleteVectorEntries(name, startindex);
     361            this->save();
     362        }
     363    }
     364
     365    ConfigFileSection* ConfigFile::getSection(const std::string& section) const
     366    {
     367        for (std::list<ConfigFileSection*>::const_iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
     368            if ((*it)->getName() == section)
     369                return (*it);
     370        return NULL;
     371    }
     372
     373    ConfigFileSection* ConfigFile::getOrCreateSection(const std::string& section)
    417374    {
    418375        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); ++it)
     
    445402    }
    446403
    447     void ConfigFile::updateConfigValues()
    448     {
    449         if (this->type_ == ConfigFileType::Settings)
    450         {
    451             for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapBegin(); it != Identifier::getStringIdentifierMapEnd(); ++it)
    452             {
    453                 if (it->second->hasConfigValues())
     404
     405    ////////////////////////
     406    // SettingsConfigFile //
     407    ////////////////////////
     408
     409    SettingsConfigFile* SettingsConfigFile::singletonPtr_s = 0;
     410
     411    SettingsConfigFile::SettingsConfigFile(const std::string& filename)
     412        : ConfigFile(filename)
     413    {
     414        ConsoleCommand* command = createConsoleCommand(createFunctor(&ConfigFile::load, this), "reloadSettings");
     415        CommandExecutor::addConsoleCommandShortcut(command);
     416        command = createConsoleCommand(createFunctor(&SettingsConfigFile::setFilename, this), "setSettingsFile");
     417        CommandExecutor::addConsoleCommandShortcut(command);
     418        command = createConsoleCommand(createFunctor(&SettingsConfigFile::config, this), "config");
     419        CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue());
     420        command = createConsoleCommand(createFunctor(&SettingsConfigFile::tconfig, this), "tconfig");
     421        CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries()).argumentCompleter(2, autocompletion::settingsvalue());
     422        command = createConsoleCommand(createFunctor(&SettingsConfigFile::getConfig, this), "getConfig");
     423        CommandExecutor::addConsoleCommandShortcut(command).argumentCompleter(0, autocompletion::settingssections()).argumentCompleter(1, autocompletion::settingsentries());
     424    }
     425
     426    SettingsConfigFile::~SettingsConfigFile()
     427    {
     428    }
     429
     430    void SettingsConfigFile::load()
     431    {
     432        ConfigFile::load();
     433        this->updateConfigValues();
     434    }
     435
     436    void SettingsConfigFile::setFilename(const std::string& filename)
     437    {
     438        ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);
     439    }
     440
     441    void SettingsConfigFile::addConfigValueContainer(ConfigValueContainer* container)
     442    {
     443        if (container == NULL)
     444            return;
     445        std::pair<std::string, ConfigValueContainer*> second(getLowercase(container->getName()), container);
     446        this->containers_.insert(std::make_pair(getLowercase(container->getSectionName()), second));
     447        this->sectionNames_.insert(container->getSectionName());
     448    }
     449
     450    void SettingsConfigFile::removeConfigValueContainer(ConfigValueContainer* container)
     451    {
     452        if (container == NULL)
     453            return;
     454        const std::string& sectionLC = getLowercase(container->getSectionName());
     455        ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
     456        for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
     457        {
     458            if (it->second.second == container)
     459            {
     460                // Remove entry from section name set this was the last container for that section
     461                if (upper == this->containers_.lower_bound(sectionLC))
     462                    this->sectionNames_.erase(container->getSectionName());
     463                this->containers_.erase(it);
     464                break;
     465            }
     466        }
     467    }
     468
     469    void SettingsConfigFile::updateConfigValues()
     470    {
     471        for (ContainerMap::const_iterator it = this->containers_.begin(); it != this->containers_.end(); ++it)
     472        {
     473            it->second.second->update();
     474            it->second.second->getIdentifier()->updateConfigValues();
     475        }
     476    }
     477
     478    void SettingsConfigFile::clean(bool bCleanComments)
     479    {
     480        for (std::list<ConfigFileSection*>::iterator itSection = this->sections_.begin(); itSection != this->sections_.end(); )
     481        {
     482            const std::string& sectionLC = getLowercase((*itSection)->getName());
     483            ContainerMap::const_iterator lower = this->containers_.lower_bound(sectionLC);
     484            ContainerMap::const_iterator upper = this->containers_.upper_bound(sectionLC);
     485            if (lower != upper)
     486            {
     487                // The section exists, delete comment
     488                if (bCleanComments)
     489                    (*itSection)->setComment("");
     490                for (std::list<ConfigFileEntry*>::iterator itEntry = (*itSection)->entries_.begin(); itEntry != (*itSection)->entries_.end(); )
    454491                {
    455                     for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = it->second->getConfigValueMapBegin(); it2 != it->second->getConfigValueMapEnd(); ++it2)
    456                         it2->second->update();
    457 
    458                     it->second->updateConfigValues();
     492                    const std::string& entryLC = getLowercase((*itEntry)->getName());
     493                    bool bFound = false;
     494                    for (ContainerMap::const_iterator itContainer = lower; itContainer != upper; ++itContainer)
     495                    {
     496                        if (itContainer->second.first == entryLC)
     497                        {
     498                            // The config-value exists, delete comment
     499                            if (bCleanComments)
     500                                (*itEntry)->setComment("");
     501                            ++itEntry;
     502                            bFound = true;
     503                            break;
     504                        }
     505                    }
     506                    if (!bFound)
     507                    {
     508                        // The config-value doesn't exist
     509                        delete (*itEntry);
     510                        (*itSection)->entries_.erase(itEntry++);
     511                    }
    459512                }
    460             }
    461         }
     513                ++itSection;
     514            }
     515            else
     516            {
     517                // The section doesn't exist
     518                delete (*itSection);
     519                this->sections_.erase(itSection++);
     520            }
     521        }
     522
     523        // Save the file
     524        this->save();
     525    }
     526
     527    bool SettingsConfigFile::config(const std::string& section, const std::string& entry, const std::string& value)
     528    {
     529        return this->configImpl(section, entry, value, &ConfigValueContainer::set);
     530    }
     531
     532    bool SettingsConfigFile::tconfig(const std::string& section, const std::string& entry, const std::string& value)
     533    {
     534        return this->configImpl(section, entry, value, &ConfigValueContainer::tset);
     535    }
     536
     537    bool SettingsConfigFile::configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&))
     538    {
     539        const std::string& sectionLC = getLowercase(section);
     540        const std::string& entryLC = getLowercase(entry);
     541        ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
     542        for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
     543        {
     544            // Note: Config value vectors cannot be supported
     545            if (it->second.first == entryLC && !it->second.second->isVector())
     546            {
     547                return (it->second.second->*function)(value);
     548            }
     549        }
     550        return false;
     551    }
     552
     553    std::string SettingsConfigFile::getConfig(const std::string& section, const std::string& entry)
     554    {
     555        const std::string& sectionLC = getLowercase(section);
     556        const std::string& entryLC = getLowercase(entry);
     557        ContainerMap::iterator upper = this->containers_.upper_bound(sectionLC);
     558        for (ContainerMap::iterator it = this->containers_.lower_bound(sectionLC); it != upper; ++it)
     559        {
     560            // Note: Config value vectors cannot be supported
     561            if (it->second.first == entryLC && ! it->second.second->isVector())
     562            {
     563                std::string value;
     564                it->second.second->getValue<std::string, OrxonoxClass>(&value, NULL);
     565                return value;
     566            }
     567        }
     568        return "";
    462569    }
    463570
     
    469576    ConfigFileManager* ConfigFileManager::singletonPtr_s = 0;
    470577
    471     std::string ConfigFileManager::DEFAULT_CONFIG_FILE = "default.ini";
    472 
    473578    ConfigFileManager::ConfigFileManager()
    474          : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)
    475     {
     579    {
     580        this->configFiles_.assign(NULL);
    476581    }
    477582
    478583    ConfigFileManager::~ConfigFileManager()
    479584    {
    480         for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); )
    481             delete (it++)->second;
    482     }
    483 
    484     void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename)
    485     {
    486         std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
    487         if (it != this->configFiles_.end())
    488         {
    489             assert(it->second);
    490             delete it->second;
    491         }
    492         this->configFiles_[type] = new ConfigFile(filename, type);
    493         this->load(type);
    494     }
    495 
    496     void ConfigFileManager::load()
    497     {
    498         for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    499             it->second->load();
    500     }
    501 
    502     void ConfigFileManager::save()
    503     {
    504         for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    505             it->second->save();
    506     }
    507 
    508     void ConfigFileManager::clean(bool bCleanComments)
    509     {
    510         for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    511             this->clean(it->first, bCleanComments);
    512     }
    513 
    514     void ConfigFileManager::load(ConfigFileType type)
    515     {
    516         this->getFile(type)->load();
    517     }
    518 
    519     void ConfigFileManager::save(ConfigFileType type)
    520     {
    521         this->getFile(type)->save();
    522     }
    523 
    524     void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename)
    525     {
    526         this->getFile(type)->saveAs(saveFilename);
    527     }
    528 
    529     void ConfigFileManager::clean(ConfigFileType type, bool bCleanComments)
    530     {
    531         this->getFile(type)->clean(bCleanComments);
    532     }
    533 
    534     void ConfigFileManager::updateConfigValues()
    535     {
    536         for (std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
    537             it->second->updateConfigValues();
    538     }
    539 
    540     void ConfigFileManager::updateConfigValues(ConfigFileType type)
    541     {
    542         this->getFile(type)->updateConfigValues();
    543     }
    544 
    545     const std::string& ConfigFileManager::getFilename(ConfigFileType type)
    546     {
    547         std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
    548         if (it != this->configFiles_.end())
    549             return it->second->getFilename();
    550         else
    551             return BLANKSTRING;
    552     }
    553 
    554     ConfigFile* ConfigFileManager::getFile(ConfigFileType type)
    555     {
    556         std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
    557         if (it != this->configFiles_.end())
    558             return it->second;
    559         else
    560         {
    561             COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << static_cast<int>(type) << std::endl;
    562             COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl;
    563             this->setFilename(type, DEFAULT_CONFIG_FILE);
    564             return getFile(type);
    565         }
     585        for (boost::array<ConfigFile*, 3>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
     586            if (*it)
     587                delete (*it);
     588    }
     589
     590    void ConfigFileManager::setFilename(ConfigFileType::Value type, const std::string& filename)
     591    {
     592        if (this->getConfigFile(type))
     593            delete this->configFiles_[type];
     594        // Create and load config file
     595        switch (type)
     596        {
     597        case ConfigFileType::Settings:
     598            this->configFiles_[type] = new SettingsConfigFile(filename);
     599            break;
     600        case ConfigFileType::JoyStickCalibration:
     601        case ConfigFileType::CommandHistory:
     602            this->configFiles_[type] = new ConfigFile(filename);
     603            break;
     604        }
     605        this->configFiles_[type]->load();
    566606    }
    567607}
Note: See TracChangeset for help on using the changeset viewer.