00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00032 CXMLConfigFile::CXMLConfigFile()
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
00093 TiXmlDeclaration decl("1.0", "UTF-8", "Yes");
00094
00095
00096 TiXmlComment ver;
00097 sprintf(str, " Keos %s ", CRoot::Instance().GetVersion().c_str());
00098 ver.SetValue(str);
00099
00100
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
00111 TiXmlElement root("properties");
00112
00113
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 }