Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# KeosXMLConfigFile.cpp

Go to the documentation of this file.

    00001 /*
    00002  * This source file is part of KEOS (Free 3D Engine)
    00003  * For the latest info, see http://www.keosengine.org/
    00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it under
    00007  * the terms of the GNU Lesser General Public License as published by the Free Software
    00008  * Foundation; either version 2 of the License, or (at your option) any later
    00009  * version.
    00010  *
    00011  * This program is distributed in the hope that it will be useful, but WITHOUT
    00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Lesser General Public License along with
    00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00018  * http://www.gnu.org/copyleft/lesser.txt.
    00019  */
    00020
    00021 #include "KeosXMLConfigFile.h"
    00022 #include "KeosException.h"
    00023 #include "KeosString.h"
    00024 #include "KeosLogger.h"
    00025 #include "KeosRoot.h"
    00026 #include "../Extlibs/TinyXML/tinyxml.h"
    00027
    00028 namespace Keos {
    00029
    00030     //-----------------------------------------------------------------------
    00031     CXMLConfigFile::CXMLConfigFile()
    00032     {
    00033     }
    00034
    00035     //-----------------------------------------------------------------------
    00036     CXMLConfigFile::~CXMLConfigFile()
    00037     {
    00038         Clear();
    00039     }
    00040
    00041     //-----------------------------------------------------------------------
    00042     void CXMLConfigFile::Clear(void)
    00043     {
    00044         m_Settings.clear();
    00045     }
    00046
    00047     //-----------------------------------------------------------------------
    00048     bool CXMLConfigFile::Load(const String& strFilename)
    00049     {
    00050         TiXmlDocument doc( strFilename.c_str() );
    00051             bool bLoadOkay = doc.LoadFile();
    00052         if ( !bLoadOkay )
    00053             {
    00054             ILogger::Log("Cannot open config file \"%s\", default settings will be used !", strFilename.c_str());
    00055                     return false;
    00056             }
    00057
    00058         TiXmlNode* root = doc.RootElement();
    00059         if ( !root )
    00060             {
    00061             ILogger::Log("Invalid config file \"%s\" file, default settings will be used !", strFilename.c_str());
    00062                     return false;
    00063             }
    00064
    00065         TiXmlElement* element;
    00066
    00067             for( element = root->FirstChildElement();
    00068                     element;
    00069                     element = element->NextSiblingElement() )
    00070             {
    00071             if (strcmp(element->Value(), "property") == 0)
    00072             {
    00073                 String strName = element->Attribute("name");
    00074                 String strValue = element->Attribute("value");
    00075
    00076                 if (strName != "" && strValue != "")
    00077                 {
    00078                     m_Settings[strName] = strValue;
    00079                 }
    00080             }
    00081             }
    00082
    00083         return true;
    00084     }
    00085
    00086         //-----------------------------------------------------------------------
    00087     bool CXMLConfigFile::Save(const String& strFilename, ConfigOptionMap ConfigMap)
    00088     {
    00089                 char str[128];
    00090         TiXmlDocument doc(strFilename.c_str());
    00091
    00092                 // XML Declaration
    00093         TiXmlDeclaration decl("1.0", "UTF-8", "Yes");
    00094
    00095                 // XML Comment: Keos version
    00096                 TiXmlComment ver;
    00097                 sprintf(str," Keos %s ", CRoot::Instance().GetVersion().c_str());
    00098                 ver.SetValue(str);
    00099
    00100                 // XML Comment: Last save: date and time
    00101                 TiXmlComment lastmodified;
    00102         char sDate[24];
    00103         time_t CurrentTime = time(NULL);
    00104         strftime(sDate, sizeof(sDate), "%d/%m/%Y", localtime(&CurrentTime));
    00105                 char sTime[24];
    00106                 strftime(sTime, sizeof(sTime), "%H:%M:%S", localtime(&CurrentTime));
    00107                 sprintf(str," Last save: %s %s ", sDate, sTime);
    00108                 lastmodified.SetValue(str);
    00109
    00110                 // Root XML Element
    00111         TiXmlElement root("properties");
    00112
    00113                 // Insert XML Elements: property
    00114         for(
    00115             ConfigOptionMap::iterator it = ConfigMap.begin();
    00116             it != ConfigMap.end();
    00117             ++it )
    00118         {
    00119             TiXmlElement node("property");
    00120             node.SetAttribute("name", it->second.name.c_str());
    00121             node.SetAttribute("value", it->second.currentValue.c_str());
    00122             root.InsertEndChild(node);
    00123         }
    00124
    00125                 doc.InsertEndChild(decl);
    00126                 doc.InsertEndChild(ver);
    00127                 doc.InsertEndChild(lastmodified);
    00128                 doc.InsertEndChild(root);
    00129
    00130                 return doc.SaveFile();
    00131     }
    00132
    00133     //-----------------------------------------------------------------------
    00134     String CXMLConfigFile::GetSetting(const String& strKey) const
    00135     {
    00136         SettingsMap::const_iterator i = m_Settings.find(strKey);
    00137         if (i == m_Settings.end())
    00138         {
    00139             return CStringUtil::BLANK;
    00140         }
    00141         else
    00142         {
    00143             return i->second;
    00144         }
    00145     }
    00146 } // namespace Keos