From ff0881df3e1a97d02f7a1e1c06f4a2c778a94143 Mon Sep 17 00:00:00 2001 From: doitux Date: Mon, 22 Jan 2007 21:29:51 +0000 Subject: [PATCH] --- src/config/configfile.cpp | 216 +++++++++++++++----------------------- src/config/configfile.h | 8 +- 2 files changed, 87 insertions(+), 137 deletions(-) diff --git a/src/config/configfile.cpp b/src/config/configfile.cpp index 88a49dc2..1ec16e70 100644 --- a/src/config/configfile.cpp +++ b/src/config/configfile.cpp @@ -52,8 +52,8 @@ ConfigFile::ConfigFile() TiXmlDocument doc(configFileName); if(!doc.LoadFile()){ createDefaultConfig(); } - cout << "readConfigInt Value: " << readConfigInt("NumberOfPlayers", 2) << "\n"; - cout << "1\n"; +// cout << "readConfigString Value: " << readConfigString("Opponent1Name", "doitux") << "\n"; + writeConfigString(string("MyName"), string("Ret")); } @@ -62,8 +62,8 @@ ConfigFile::~ConfigFile() } void ConfigFile::createDefaultConfig() { - //Anlegen! + //Anlegen! TiXmlDocument doc; TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", ""); doc.LinkEndChild( decl ); @@ -113,161 +113,111 @@ void ConfigFile::createDefaultConfig() { } -string ConfigFile::readConfigString(string varName, string defaultvalue) +string ConfigFile::readConfigString(string varName, string defaultValue) { - string tempString; + string tempString(""); TiXmlDocument doc(configFileName); - - if(!doc.LoadFile()) { cout << "nö" << "\n"; } - + 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 << "ich war hier" << "\n"; - conf->QueryValueAttribute("value", &tempString ); - } -// cout << tempString << "\n"; - return tempString; -// QDir configDir; -// configDir.setPath(QDir::home().absPath()+"/.pokerth/"); -// -// QFile configFile (configDir.absPath()+"/pokerth.conf"); -// -// if ( !configFile.exists() ) { -// configFile.open( QIODevice::WriteOnly ); -// QTextStream stream( &configFile ); -// stream << "##################################################################################### \n"; -// stream << "# # \n"; -// stream << "# This is the config-file for Pokerth - Please do not edit # \n"; -// stream << "# # \n"; -// stream << "##################################################################################### \n"; -// stream << "\n"; -// stream << "numberofplayers=5\n"; -// stream << "startcash=2000\n"; -// stream << "smallblind=10\n"; -// stream << "gamespeed=4\n"; -// stream << "showgamesettingsdialogonnewgame=1\n"; -// stream << "myname=Human Player\n"; -// stream << "opponent1name=Player 1\n"; -// stream << "opponent2name=Player 2\n"; -// stream << "opponent3name=Player 3\n"; -// stream << "opponent4name=Player 4\n"; -// stream << "showtoolbox=1\n"; -// configFile.close(); -// } -// -// QString tempstring; -// QString line; -// int foundvarname(0); -// -// configFile.open( QIODevice::ReadOnly ); -// QTextStream readStream( &configFile ); -// while ( !readStream.atEnd() ) { -// line = readStream.readLine(); -// if ( line.section( "=", 0, 0 ) == varName ) { -// tempstring = line.section( "=", 1, 1 ); -// foundvarname++; -// } -// } -// configFile.close(); -// -// if (foundvarname == 0) { -// -// QDir configDir; -// configDir.setPath(QDir::home().absPath()+"/.pokerth/"); -// -// QFile configFile (configDir.absPath()+"/pokerth.conf"); -// -// QStringList lines; -// QString line; -// QString listtemp; -// -// if ( configFile.open( QIODevice::ReadOnly ) ) { -// QTextStream stream( &configFile ); -// while ( !stream.atEnd() ) { -// line = stream.readLine(); -// lines += line; -// -// } -// configFile.close(); -// } -// -// if ( configFile.open( QIODevice::WriteOnly ) ) { -// -// QTextStream stream( &configFile ); -// -// for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it ) { -// stream << *it << "\n"; -// } -// stream << varName+"="+defaultvalue+"\n"; -// configFile.close(); -// } -// -// tempstring = defaultvalue; -// -// } -// - + TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement(); + if ( conf ) { + conf->QueryValueAttribute("value", &tempString ); + } 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; } -int ConfigFile::readConfigInt(string varName, int defaultvalue) +int ConfigFile::readConfigInt(string varName, int defaultValue) { int tempInt=0; - cout << "2\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 << "ich war hier" << "\n"; conf->QueryValueAttribute("value", &tempInt ); - } + } 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 tempInt; } -void ConfigFile::writeConfigInt(string setVarName, int setVarCont) - { -// QDir configDir; -// configDir.setPath(QDir::home().absPath()+"/.pokerth/"); -// QFile configFile (configDir.absPath()+"/pokerth.conf"); -// -// QStringList lines; -// QString line; -// QString listtemp; -// -// if ( configFile.open( QIODevice::ReadOnly ) ) { -// QTextStream stream( &configFile ); -// while ( !stream.atEnd() ) { -// line = stream.readLine(); -// lines += line; -// } -// configFile.close(); -// } -// -// listtemp = lines.grep(QRegExp("^"+setVarName)).join(""); -// lines.gres(listtemp,setVarName+"="+setVarCont); -// -// if ( configFile.open( QIODevice::WriteOnly ) ) { -// QTextStream stream( &configFile ); -// for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it ) { -// stream << *it << "\n"; -// } -// configFile.close(); -// } - +void ConfigFile::writeConfigInt(string varName, int varCont) + { + + 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 ) { + conf->SetAttribute("value", varCont ); + if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\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", varCont); + + if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } + } + } + if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } } -void ConfigFile::writeConfigString(string setVarName, string setVarCont) +void ConfigFile::writeConfigString(string varName, string varCont) { + + 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 ) { + conf->SetAttribute("value", varCont ); + } 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", varCont); + if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } + } + } + if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; } } \ No newline at end of file diff --git a/src/config/configfile.h b/src/config/configfile.h index d4e6c8d1..1fbe96e1 100644 --- a/src/config/configfile.h +++ b/src/config/configfile.h @@ -31,11 +31,11 @@ public: void createDefaultConfig(); - std::string readConfigString(std::string varName, std::string defaultvalue); - void writeConfigString(std::string setVarName, std::string setVarCont); + std::string readConfigString(std::string varName, std::string defaultValue); + void writeConfigString(std::string varName, std::string varCont); - int readConfigInt(std::string varName, int defaultvalue); - void writeConfigInt(std::string setVarName, int setVarCont); + int readConfigInt(std::string varName, int defaultValue); + void writeConfigInt(std::string varName, int varCont); private: