From 098349b57225769de08f58510be389f6c3daa568 Mon Sep 17 00:00:00 2001 From: doitux Date: Wed, 16 May 2007 01:08:01 +0000 Subject: [PATCH] implement minimal disc access for config-file --- src/config/configfile.cpp | 215 ++++++++++++++++++++++++-------------- src/config/configfile.h | 16 +++ src/gui/qt/log/log.cpp | 17 +-- src/gui/qt/log/log.h | 2 +- 4 files changed, 164 insertions(+), 86 deletions(-) diff --git a/src/config/configfile.cpp b/src/config/configfile.cpp index 9de3e860..fe32fbf5 100644 --- a/src/config/configfile.cpp +++ b/src/config/configfile.cpp @@ -21,11 +21,6 @@ #include -#include -#include -#include -#include -#include #define MODUS 0711 #ifdef _WIN32 @@ -35,7 +30,6 @@ class QtToolsWrapper; -using namespace std; ConfigFile::ConfigFile(bool configFirstStart) { @@ -108,41 +102,6 @@ ConfigFile::ConfigFile(bool configFirstStart) } } #endif - configFileName += "config.xml"; - - if (configFirstStart) { - //Prüfen ob Configfile existiert --> sonst anlegen - TiXmlDocument doc(configFileName); - if(!doc.LoadFile()){ - myConfigState = NONEXISTING; - updateConfig(myConfigState); - } - else { - //Prüfen ob die Revision stimmt ansonsten löschen und neue anlegen - int temp = 0; - TiXmlHandle docHandle( &doc ); - TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( "ConfigRevision" ).ToElement(); - if ( conf ) { conf->QueryIntAttribute("value", &temp ); } - if (temp < configRev) { /*löschen()*/ - myConfigState = OLD; - updateConfig(myConfigState) ; - } - } - } -} - - -ConfigFile::~ConfigFile() -{ -} - -void ConfigFile::updateConfig(ConfigState myConfigState) { - - size_t i; - - QtToolsInterface *myQtToolsInterface = new QtToolsWrapper; - - vector configList; ostringstream tempIntToString; tempIntToString << configRev; @@ -193,7 +152,80 @@ void ConfigFile::updateConfig(ConfigState myConfigState) { configList.push_back(ConfigInfo("LogInterval", CONFIG_TYPE_INT, "0")); configList.push_back(ConfigInfo("DataDir", CONFIG_TYPE_STRING, dataDir)); + //fill tempList firstTime + configBufferList = configList; +// cout << configTempList[3].name << " " << configTempList[10].defaultValue << endl; + + configFileName += "config.xml"; + + if (configFirstStart) { + //Prüfen ob Configfile existiert --> sonst anlegen + TiXmlDocument doc(configFileName); + if(!doc.LoadFile()){ + myConfigState = NONEXISTING; + updateConfig(myConfigState); + } + else { + //Prüfen ob die Revision stimmt ansonsten löschen und neue anlegen + int temp = 0; + TiXmlHandle docHandle( &doc ); + TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( "ConfigRevision" ).ToElement(); + if ( conf ) { conf->QueryIntAttribute("value", &temp ); } + if (temp < configRev) { /*löschen()*/ + myConfigState = OLD; + updateConfig(myConfigState) ; + } + } + } + + fillBuffer(); +} + + +ConfigFile::~ConfigFile() +{ +} + + +void ConfigFile::fillBuffer() { + + QtToolsInterface *myQtToolsInterface = new QtToolsWrapper; + + size_t i; + string tempString(""); + + TiXmlDocument doc(configFileName); + + if(doc.LoadFile()) { + TiXmlHandle docHandle( &doc ); + + for (i=0; iAttribute("value"); + if (tmpStr) tempString = tmpStr; + configBufferList[i].defaultValue = myQtToolsInterface->stringToUtf8(tempString); + } + else { cout << "Could not find the element to fill the config-buffer with!"; } + +// cout << configBufferList[i].name << " " << configBufferList[i].defaultValue << endl; + } + } + + delete myQtToolsInterface; + myQtToolsInterface = 0; +} + +void ConfigFile::updateConfig(ConfigState myConfigState) { + + size_t i; + + QtToolsInterface *myQtToolsInterface = new QtToolsWrapper; + if(myConfigState == NONEXISTING) { //Create a new ConfigFile! @@ -281,58 +313,79 @@ void ConfigFile::updateConfig(ConfigState myConfigState) { string ConfigFile::readConfigString(string varName) { + size_t i; string tempString(""); - TiXmlDocument doc(configFileName); - if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; } - TiXmlHandle docHandle( &doc ); +// TiXmlDocument doc(configFileName); +// if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; } +// TiXmlHandle docHandle( &doc ); +// +// TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement(); +// if ( conf ) { +// const char *tmpStr = conf->Attribute("value"); +// if (tmpStr) tempString = tmpStr; +// } /*else { +// //Wenn nicht gefunden eines neues Anlegen +// TiXmlElement* config = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).ToElement(); +// if ( config ) { +// TiXmlElement * confElement1 = new TiXmlElement( varName ); +// config->LinkEndChild( confElement1 ); +// confElement1->SetAttribute("value", defaultValue); +// if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } +// +// return readConfigString(varName, defaultValue); +// } +// }*/ + for (i=0; iAttribute("value"); - if (tmpStr) tempString = tmpStr; - } /*else { - //Wenn nicht gefunden eines neues Anlegen - TiXmlElement* config = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).ToElement(); - if ( config ) { - TiXmlElement * confElement1 = new TiXmlElement( varName ); - config->LinkEndChild( confElement1 ); - confElement1->SetAttribute("value", defaultValue); - if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } - - return readConfigString(varName, defaultValue); + if (configBufferList[i].name == varName) { + tempString = configBufferList[i].defaultValue; } - }*/ + } return tempString; } int ConfigFile::readConfigInt(string varName) { + size_t i; + string tempString(""); int tempInt=0; + // cout << varName << " : " << tempInt << "\n"; - TiXmlDocument doc(configFileName); - if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; } - TiXmlHandle docHandle( &doc ); - - TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement(); - if ( conf ) { -// cout << varName << " : " << tempInt << "\n"; - conf->QueryIntAttribute("value", &tempInt ); -// cout << varName << " : " << tempInt << "\n"; - } /*else { -// Wenn nicht gefunden eines neues Anlegen - TiXmlElement* config = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).ToElement(); - if ( config ) { - TiXmlElement * confElement1 = new TiXmlElement( varName ); - config->LinkEndChild( confElement1 ); - confElement1->SetAttribute("value", defaultValue); - if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } +// TiXmlDocument doc(configFileName); +// if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; } +// TiXmlHandle docHandle( &doc ); +// +// TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement(); +// if ( conf ) { +// // cout << varName << " : " << tempInt << "\n"; +// conf->QueryIntAttribute("value", &tempInt ); +// // cout << varName << " : " << tempInt << "\n"; +// } /*else { +// // Wenn nicht gefunden eines neues Anlegen +// TiXmlElement* config = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).ToElement(); +// if ( config ) { +// TiXmlElement * confElement1 = new TiXmlElement( varName ); +// config->LinkEndChild( confElement1 ); +// confElement1->SetAttribute("value", defaultValue); +// if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } +// +// return readConfigInt(varName, defaultValue); +// } +// }*/ - return readConfigInt(varName, defaultValue); + for (i=0; i> tempInt; + return tempInt; } @@ -362,6 +415,9 @@ void ConfigFile::writeConfigInt(string varName, int varCont) } } if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } + + fillBuffer(); + } void ConfigFile::writeConfigString(string varName, string varCont) @@ -386,5 +442,6 @@ void ConfigFile::writeConfigString(string varName, string varCont) } if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } + fillBuffer(); } diff --git a/src/config/configfile.h b/src/config/configfile.h index 692fc6fd..66605f84 100644 --- a/src/config/configfile.h +++ b/src/config/configfile.h @@ -24,6 +24,17 @@ #include #include #include +#include +#include +#include + +#include +#include + +#include + + +using namespace std; enum ConfigState { NONEXISTING, OLD }; enum ConfigType { CONFIG_TYPE_INT, CONFIG_TYPE_STRING }; @@ -35,6 +46,8 @@ public: ~ConfigFile(); + void fillBuffer(); + void updateConfig(ConfigState); std::string readConfigString(std::string varName); @@ -54,6 +67,9 @@ private: ConfigType type; std::string defaultValue; }; + + vector configList; + vector configBufferList; std::string configFileName; std::string logDir; diff --git a/src/gui/qt/log/log.cpp b/src/gui/qt/log/log.cpp index 27bcd71c..ffedf293 100644 --- a/src/gui/qt/log/log.cpp +++ b/src/gui/qt/log/log.cpp @@ -27,7 +27,7 @@ using namespace std; -Log::Log(mainWindowImpl* w, ConfigFile *c) : myW(w), myConfig(c) +Log::Log(mainWindowImpl* w, ConfigFile *c) : myW(w), myConfig(c), myLogDir(0), myLogFile(0) { myW->setLog(this); @@ -1138,10 +1138,15 @@ QStringList Log::translateCardsValueCode(int cardsValueCode) { void Log::writeLogFileStream(QString streamString) { - myLogFile->open( QIODevice::ReadWrite ); - QTextStream stream( myLogFile ); - stream.readAll(); - stream << streamString; - myLogFile->close(); + if(myLogFile) { + if(myLogFile->open( QIODevice::ReadWrite )) { + QTextStream stream( myLogFile ); + stream.readAll(); + stream << streamString; + myLogFile->close(); + } + else { cout << "could not open log-file to write log-messages!" << endl; } + } + else { cout << "could not find log-file to write log-messages!" << endl; } } diff --git a/src/gui/qt/log/log.h b/src/gui/qt/log/log.h index 1a8b1bb9..e813c58f 100644 --- a/src/gui/qt/log/log.h +++ b/src/gui/qt/log/log.h @@ -55,8 +55,8 @@ private: mainWindowImpl *myW; ConfigFile *myConfig; QTextStream stream; - QFile *myLogFile; QDir *myLogDir; + QFile *myLogFile; QString logFileStreamString; };