implement minimal disc access for config-file

This commit is contained in:
doitux
2007-05-16 01:08:01 +00:00
parent d213ee3ff2
commit 098349b572
4 changed files with 164 additions and 86 deletions
+136 -79
View File
@@ -21,11 +21,6 @@
#include <qttoolswrapper.h> #include <qttoolswrapper.h>
#include <cstdlib>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <boost/shared_ptr.hpp>
#define MODUS 0711 #define MODUS 0711
#ifdef _WIN32 #ifdef _WIN32
@@ -35,7 +30,6 @@
class QtToolsWrapper; class QtToolsWrapper;
using namespace std;
ConfigFile::ConfigFile(bool configFirstStart) ConfigFile::ConfigFile(bool configFirstStart)
{ {
@@ -108,41 +102,6 @@ ConfigFile::ConfigFile(bool configFirstStart)
} }
} }
#endif #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<ConfigInfo> configList;
ostringstream tempIntToString; ostringstream tempIntToString;
tempIntToString << configRev; tempIntToString << configRev;
@@ -193,7 +152,80 @@ void ConfigFile::updateConfig(ConfigState myConfigState) {
configList.push_back(ConfigInfo("LogInterval", CONFIG_TYPE_INT, "0")); configList.push_back(ConfigInfo("LogInterval", CONFIG_TYPE_INT, "0"));
configList.push_back(ConfigInfo("DataDir", CONFIG_TYPE_STRING, dataDir)); 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; i<configBufferList.size(); i++) {
TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).ToElement();
if ( conf ) {
const char *tmpStr = conf->Attribute("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) { if(myConfigState == NONEXISTING) {
//Create a new ConfigFile! //Create a new ConfigFile!
@@ -281,58 +313,79 @@ void ConfigFile::updateConfig(ConfigState myConfigState) {
string ConfigFile::readConfigString(string varName) string ConfigFile::readConfigString(string varName)
{ {
size_t i;
string tempString(""); string tempString("");
TiXmlDocument doc(configFileName); // TiXmlDocument doc(configFileName);
if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; } // if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; }
TiXmlHandle docHandle( &doc ); // 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; i<configBufferList.size(); i++) {
TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement(); if (configBufferList[i].name == varName) {
if ( conf ) { tempString = configBufferList[i].defaultValue;
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);
} }
}*/ }
return tempString; return tempString;
} }
int ConfigFile::readConfigInt(string varName) int ConfigFile::readConfigInt(string varName)
{ {
size_t i;
string tempString("");
int tempInt=0; int tempInt=0;
// cout << varName << " : " << tempInt << "\n"; // cout << varName << " : " << tempInt << "\n";
TiXmlDocument doc(configFileName); // TiXmlDocument doc(configFileName);
if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; } // if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; }
TiXmlHandle docHandle( &doc ); // TiXmlHandle docHandle( &doc );
//
TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement(); // TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement();
if ( conf ) { // if ( conf ) {
// cout << varName << " : " << tempInt << "\n"; // // cout << varName << " : " << tempInt << "\n";
conf->QueryIntAttribute("value", &tempInt ); // conf->QueryIntAttribute("value", &tempInt );
// cout << varName << " : " << tempInt << "\n"; // // cout << varName << " : " << tempInt << "\n";
} /*else { // } /*else {
// Wenn nicht gefunden eines neues Anlegen // // Wenn nicht gefunden eines neues Anlegen
TiXmlElement* config = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).ToElement(); // TiXmlElement* config = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).ToElement();
if ( config ) { // if ( config ) {
TiXmlElement * confElement1 = new TiXmlElement( varName ); // TiXmlElement * confElement1 = new TiXmlElement( varName );
config->LinkEndChild( confElement1 ); // config->LinkEndChild( confElement1 );
confElement1->SetAttribute("value", defaultValue); // confElement1->SetAttribute("value", defaultValue);
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } // if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
//
// return readConfigInt(varName, defaultValue);
// }
// }*/
return readConfigInt(varName, defaultValue); for (i=0; i<configBufferList.size(); i++) {
if (configBufferList[i].name == varName) {
tempString = configBufferList[i].defaultValue;
} }
}*/ }
istringstream isst;
isst.str (tempString);
isst >> tempInt;
return 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"; } if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
fillBuffer();
} }
void ConfigFile::writeConfigString(string varName, string varCont) 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"; } if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
fillBuffer();
} }
+16
View File
@@ -24,6 +24,17 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <boost/shared_ptr.hpp>
using namespace std;
enum ConfigState { NONEXISTING, OLD }; enum ConfigState { NONEXISTING, OLD };
enum ConfigType { CONFIG_TYPE_INT, CONFIG_TYPE_STRING }; enum ConfigType { CONFIG_TYPE_INT, CONFIG_TYPE_STRING };
@@ -35,6 +46,8 @@ public:
~ConfigFile(); ~ConfigFile();
void fillBuffer();
void updateConfig(ConfigState); void updateConfig(ConfigState);
std::string readConfigString(std::string varName); std::string readConfigString(std::string varName);
@@ -54,6 +67,9 @@ private:
ConfigType type; ConfigType type;
std::string defaultValue; std::string defaultValue;
}; };
vector<ConfigInfo> configList;
vector<ConfigInfo> configBufferList;
std::string configFileName; std::string configFileName;
std::string logDir; std::string logDir;
+11 -6
View File
@@ -27,7 +27,7 @@
using namespace std; 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); myW->setLog(this);
@@ -1138,10 +1138,15 @@ QStringList Log::translateCardsValueCode(int cardsValueCode) {
void Log::writeLogFileStream(QString streamString) { void Log::writeLogFileStream(QString streamString) {
myLogFile->open( QIODevice::ReadWrite ); if(myLogFile) {
QTextStream stream( myLogFile ); if(myLogFile->open( QIODevice::ReadWrite )) {
stream.readAll(); QTextStream stream( myLogFile );
stream << streamString; stream.readAll();
myLogFile->close(); 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; }
} }
+1 -1
View File
@@ -55,8 +55,8 @@ private:
mainWindowImpl *myW; mainWindowImpl *myW;
ConfigFile *myConfig; ConfigFile *myConfig;
QTextStream stream; QTextStream stream;
QFile *myLogFile;
QDir *myLogDir; QDir *myLogDir;
QFile *myLogFile;
QString logFileStreamString; QString logFileStreamString;
}; };