Index: code/trunk/src/core/ConfigFileManager.cc
===================================================================
--- code/trunk/src/core/ConfigFileManager.cc	(revision 1889)
+++ code/trunk/src/core/ConfigFileManager.cc	(revision 2103)
@@ -28,10 +28,10 @@
 
 #include "ConfigFileManager.h"
-#include "ConfigValueContainer.h"
-#include "ConsoleCommand.h"
-#include "Identifier.h"
+
+#include <cassert>
 #include "util/Convert.h"
 #include "util/String.h"
-
+#include "ConsoleCommand.h"
+#include "ConfigValueContainer.h"
 
 namespace orxonox
@@ -39,4 +39,6 @@
     const int CONFIG_FILE_MAX_LINELENGHT  = 1024;
     const char* const DEFAULT_CONFIG_FILE = "default.ini";
+
+    ConfigFileManager* ConfigFileManager::singletonRef_s = 0;
 
     SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
@@ -45,5 +47,4 @@
     SetConsoleCommandShortcutExtern(cleanConfig);
     SetConsoleCommandShortcutExtern(loadSettings).argumentCompleter(0, autocompletion::files());
-    SetConsoleCommandShortcutExtern(loadKeybindings).argumentCompleter(0, autocompletion::files());
 
     bool config(const std::string& classname, const std::string& varname, const std::string& value)
@@ -83,12 +84,6 @@
     void loadSettings(const std::string& filename)
     {
-        ConfigFileManager::getInstance().setFile(CFT_Settings, filename, false);
-    }
-
-    void loadKeybindings(const std::string& filename)
-    {
-        ConfigFileManager::getInstance().setFile(CFT_Keybindings, filename);
-    }
-
+        ConfigFileManager::getInstance().setFilename(ConfigFileType::Settings, filename);
+    }
 
     //////////////////////////
@@ -121,7 +116,7 @@
 
 
-    ///////////////////////////////
+    ////////////////////////////////
     // ConfigFileEntryVectorValue //
-    ///////////////////////////////
+    ////////////////////////////////
     std::string ConfigFileEntryVectorValue::getFileEntry() const
     {
@@ -217,17 +212,16 @@
     ConfigFile::~ConfigFile()
     {
-        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); )
-            delete (*(it++));
+        this->clear();
     }
 
     void ConfigFile::load(bool bCreateIfNotExisting)
     {
-        if (bCreateIfNotExisting)
-        {
-            // This creates the default config file if it's not existing
-            std::ofstream createFile;
-            createFile.open(this->filename_.c_str(), std::fstream::app);
-            createFile.close();
-        }
+        // Be sure we start from new
+        this->clear();
+
+        // This creates the config file if it's not existing
+        std::ofstream createFile;
+        createFile.open(this->filename_.c_str(), std::fstream::app);
+        createFile.close();
 
         // Open the file
@@ -329,8 +323,11 @@
         file.close();
 
-        COUT(0) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
+        COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl;
 
         // Save the file in case something changed (like stripped whitespaces)
         this->save();
+
+        // Update all ConfigValueContainers
+        this->updateConfigValues();
     }
 
@@ -366,5 +363,5 @@
     }
 
-    void ConfigFile::save(const std::string& filename)
+    void ConfigFile::saveAs(const std::string& filename)
     {
         std::string temp = this->filename_;
@@ -415,4 +412,11 @@
     }
 
+    void ConfigFile::clear()
+    {
+        for (std::list<ConfigFileSection*>::iterator it = this->sections_.begin(); it != this->sections_.end(); )
+            delete (*(it++));
+        this->sections_.clear();
+    }
+
     ConfigFileSection* ConfigFile::getSection(const std::string& section)
     {
@@ -446,11 +450,31 @@
     }
 
+    void ConfigFile::updateConfigValues()
+    {
+        if (this->type_ == ConfigFileType::Settings)
+        {
+            for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it)
+            {
+                if (it->second->hasConfigValues())
+                {
+                    for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = (*it).second->getConfigValueMapBegin(); it2 != (*it).second->getConfigValueMapEnd(); ++it2)
+                        it2->second->update();
+
+                    it->second->updateConfigValues();
+                }
+            }
+        }
+    }
+
 
     ///////////////////////
     // ConfigFileManager //
     ///////////////////////
+
     ConfigFileManager::ConfigFileManager()
-    {
-        this->setFile(CFT_Settings, DEFAULT_CONFIG_FILE);
+         : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)
+    {
+        assert(singletonRef_s == 0);
+        singletonRef_s = this;
     }
 
@@ -458,30 +482,26 @@
     {
         for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); )
-            delete (*(it++)).second;
-    }
-
-    ConfigFileManager& ConfigFileManager::getInstance()
-    {
-        static ConfigFileManager instance;
-        return instance;
-    }
-
-    void ConfigFileManager::setFile(ConfigFileType type, const std::string& filename, bool bCreateIfNotExisting)
+            delete (it++)->second;
+
+        assert(singletonRef_s != 0);
+        singletonRef_s = 0;
+    }
+
+    void ConfigFileManager::setFilename(ConfigFileType type, const std::string& filename)
     {
         std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
         if (it != this->configFiles_.end())
-            if ((*it).second != 0)
-                delete (*it).second;
-
-        this->configFiles_[type] = new ConfigFile(this->getFilePath(filename));
-        this->load(type, bCreateIfNotExisting);
-    }
-
-    void ConfigFileManager::load(bool bCreateIfNotExisting)
+        {
+            assert(it->second);
+            delete it->second;
+        }
+        this->configFiles_[type] = new ConfigFile(filename, type);
+        this->load(type);
+    }
+
+    void ConfigFileManager::load()
     {
         for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
-            (*it).second->load(bCreateIfNotExisting);
-
-        this->updateConfigValues();
+            it->second->load();
     }
 
@@ -489,5 +509,5 @@
     {
         for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
-            (*it).second->save();
+            it->second->save();
     }
 
@@ -495,11 +515,10 @@
     {
         for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
-            this->clean((*it).first, bCleanComments);
-    }
-
-    void ConfigFileManager::load(ConfigFileType type, bool bCreateIfNotExisting)
-    {
-        this->getFile(type)->load(bCreateIfNotExisting);
-        this->updateConfigValues(type);
+            this->clean(it->first, bCleanComments);
+    }
+
+    void ConfigFileManager::load(ConfigFileType type)
+    {
+        this->getFile(type)->load();
     }
 
@@ -509,7 +528,7 @@
     }
 
-    void ConfigFileManager::save(ConfigFileType type, const std::string& filename)
-    {
-        this->getFile(type)->save(filename);
+    void ConfigFileManager::saveAs(ConfigFileType type, const std::string& saveFilename)
+    {
+        this->getFile(type)->saveAs(saveFilename);
     }
 
@@ -519,46 +538,36 @@
     }
 
-    void ConfigFileManager::updateConfigValues() const
+    void ConfigFileManager::updateConfigValues()
     {
         for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); ++it)
-            this->updateConfigValues((*it).first);
-    }
-
-    void ConfigFileManager::updateConfigValues(ConfigFileType type) const
-    {
-        if (type == CFT_Settings)
-        {
-            for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it)
-            {
-                if ((*it).second->hasConfigValues() /* && (*it).second != ClassIdentifier<KeyBinder>::getIdentifier()*/)
-                {
-                    for (std::map<std::string, ConfigValueContainer*>::const_iterator it2 = (*it).second->getConfigValueMapBegin(); it2 != (*it).second->getConfigValueMapEnd(); ++it2)
-                        (*it2).second->update();
-
-                    (*it).second->updateConfigValues();
-                }
-            }
-        }
-        else if (type == CFT_Keybindings)
-        {
-            // todo
-        }
+            it->second->updateConfigValues();
+    }
+
+    void ConfigFileManager::updateConfigValues(ConfigFileType type)
+    {
+        this->getFile(type)->updateConfigValues();
+    }
+
+    const std::string& ConfigFileManager::getFilename(ConfigFileType type)
+    {
+        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
+        if (it != this->configFiles_.end())
+            return it->second->getFilename();
+        else
+            return BLANKSTRING;
     }
 
     ConfigFile* ConfigFileManager::getFile(ConfigFileType type)
     {
-        std::map<ConfigFileType, ConfigFile*>::iterator it = this->configFiles_.find(type);
+        std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.find(type);
         if (it != this->configFiles_.end())
-            return (*it).second;
-
-        if (type == CFT_Settings)
-            return this->configFiles_[type] = new ConfigFile(DEFAULT_CONFIG_FILE);
-        else
-            return this->configFiles_[type] = new ConfigFile("");
-    }
-
-    std::string ConfigFileManager::getFilePath(const std::string& name) const
-    {
-        return name;
+            return it->second;
+        else
+        {
+            COUT(1) << "ConfigFileManager: Can't find a config file for type with ID " << (int)type << std::endl;
+            COUT(1) << "Using " << DEFAULT_CONFIG_FILE << " file." << std::endl;
+            this->setFilename(type, DEFAULT_CONFIG_FILE);
+            return getFile(type);
+        }
     }
 }
