Files
pokerth/src/config/configfile.cpp
T

390 lines
14 KiB
C++
Raw Normal View History

2007-01-21 16:01:37 +00:00
/***************************************************************************
* Copyright (C) 2006 by Felix Hammer *
* f.hammer@web.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "configfile.h"
2007-04-21 20:42:50 +00:00
#include <qttoolswrapper.h>
2007-01-21 16:01:37 +00:00
#include <cstdlib>
#include <fstream>
2007-02-02 09:16:27 +00:00
#include <sys/types.h>
#include <sys/stat.h>
2007-05-08 20:54:36 +00:00
#include <boost/shared_ptr.hpp>
2007-02-02 09:16:27 +00:00
#define MODUS 0711
2007-01-21 16:01:37 +00:00
#ifdef _WIN32
2007-04-13 22:10:55 +00:00
#include <windows.h>
2007-01-21 16:01:37 +00:00
#include <direct.h>
#endif
2007-04-21 20:42:50 +00:00
class QtToolsWrapper;
2007-01-21 16:01:37 +00:00
using namespace std;
2007-05-05 19:02:11 +00:00
ConfigFile::ConfigFile(bool configFirstStart)
2007-04-20 23:14:08 +00:00
{
2007-04-20 08:18:18 +00:00
// !!!! Revisionsnummer der Configdefaults !!!!!
configRev = 17;
2007-04-20 23:14:08 +00:00
// Pfad und Dateinamen setzen
2007-04-20 08:18:18 +00:00
#ifdef _WIN32
2007-04-20 23:14:08 +00:00
const char *appDataPath = getenv("AppData");
if (appDataPath && appDataPath[0] != 0) {
configFileName = appDataPath;
}
else {
const int MaxPathSize = 1024;
char curDir[MaxPathSize + 1];
curDir[0] = 0;
_getcwd(curDir, MaxPathSize);
curDir[MaxPathSize] = 0;
configFileName = curDir;
// Testen ob das Verzeichnis beschreibbar ist
ofstream tmpFile;
const char *tmpFileName = "pokerth_test.tmp";
tmpFile.open((configFileName + "\\" + tmpFileName).c_str());
if (tmpFile) {
// Erfolgreich, Verzeichnis beschreibbar.
// Datei wieder loeschen.
tmpFile.close();
remove((configFileName + "\\" + tmpFileName).c_str());
}
else {
// Fehlgeschlagen, Verzeichnis nicht beschreibbar
curDir[0] = 0;
GetTempPathA(MaxPathSize, curDir);
curDir[MaxPathSize] = 0;
configFileName = curDir;
}
}
2007-05-05 19:02:11 +00:00
//define app-dir
2007-04-20 23:14:08 +00:00
configFileName += "\\pokerth\\";
2007-05-05 19:02:11 +00:00
////define log-dir
2007-04-20 23:14:08 +00:00
logDir = configFileName;
2007-04-20 08:18:18 +00:00
logDir += "log-files\\";
2007-05-05 19:02:11 +00:00
////define data-dir
2007-04-20 23:14:08 +00:00
dataDir = configFileName;
2007-04-20 08:18:18 +00:00
dataDir += "data\\";
2007-05-05 19:02:11 +00:00
//create directories on first start of app
if (configFirstStart) {
mkdir(configFileName.c_str());
mkdir(logDir.c_str());
mkdir(dataDir.c_str());
}
2007-04-20 08:18:18 +00:00
#else
2007-05-05 19:02:11 +00:00
//define app-dir
2007-04-20 23:14:08 +00:00
const char *homePath = getenv("HOME");
if(homePath) {
configFileName = homePath;
configFileName += "/.pokerth/";
2007-05-05 19:02:11 +00:00
////define log-dir
2007-04-20 23:14:08 +00:00
logDir = configFileName;
logDir += "log-files/";
2007-05-05 19:02:11 +00:00
////define data-dir
2007-04-20 23:14:08 +00:00
dataDir = configFileName;
dataDir += "data/";
2007-05-05 19:02:11 +00:00
//create directories on first start of app
if (configFirstStart) {
mkdir(configFileName.c_str(), MODUS) ;
mkdir(logDir.c_str(), MODUS);
mkdir(dataDir.c_str(), MODUS);
}
2007-04-20 23:14:08 +00:00
}
2007-04-20 08:18:18 +00:00
#endif
2007-04-20 23:14:08 +00:00
configFileName += "config.xml";
2007-05-05 19:02:11 +00:00
if (configFirstStart) {
//Prüfen ob Configfile existiert --> sonst anlegen
TiXmlDocument doc(configFileName);
2007-05-05 20:11:49 +00:00
if(!doc.LoadFile()){
2007-05-08 20:54:36 +00:00
myConfigState = NONEXISTING;
updateConfig(myConfigState);
2007-05-05 20:11:49 +00:00
}
2007-05-05 19:02:11 +00:00
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 ); }
2007-05-05 20:11:49 +00:00
if (temp < configRev) { /*löschen()*/
2007-05-08 20:54:36 +00:00
myConfigState = OLD;
updateConfig(myConfigState) ;
2007-05-05 20:11:49 +00:00
}
2007-05-05 19:02:11 +00:00
}
2007-04-20 08:18:18 +00:00
}
2007-01-21 16:01:37 +00:00
}
2007-04-20 23:14:08 +00:00
2007-01-21 16:01:37 +00:00
ConfigFile::~ConfigFile()
{
}
2007-05-08 20:54:36 +00:00
void ConfigFile::updateConfig(ConfigState myConfigState) {
2007-04-20 08:18:18 +00:00
2007-05-08 21:05:45 +00:00
size_t i;
2007-05-08 20:54:36 +00:00
2007-04-21 20:42:50 +00:00
QtToolsInterface *myQtToolsInterface = new QtToolsWrapper;
2007-05-08 20:54:36 +00:00
vector<ConfigInfo> configList;
ostringstream tempIntToString;
tempIntToString << configRev;
configList.push_back(ConfigInfo("ConfigRevision", CONFIG_TYPE_INT, tempIntToString.str()));
configList.push_back(ConfigInfo("ShowLeftToolBox", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("ShowRightToolBox", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("ShowStatusbarMessages", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("ShowIntro", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("ShowFadeOutCardsAnimation", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("ShowFlipCardsAnimation", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("FlipsideTux", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("FlipsideOwn", CONFIG_TYPE_INT, "0"));
configList.push_back(ConfigInfo("FlipsideOwnFile", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("NumberOfPlayers", CONFIG_TYPE_INT, "7"));
configList.push_back(ConfigInfo("StartCash", CONFIG_TYPE_INT, "3000"));
configList.push_back(ConfigInfo("SmallBlind", CONFIG_TYPE_INT, "10"));
configList.push_back(ConfigInfo("HandsBeforeRaiseSmallBlind", CONFIG_TYPE_INT, "8"));
configList.push_back(ConfigInfo("GameSpeed", CONFIG_TYPE_INT, "4"));
configList.push_back(ConfigInfo("EngineVersion", CONFIG_TYPE_INT, "0"));
configList.push_back(ConfigInfo("PauseBetweenHands", CONFIG_TYPE_INT, "0"));
configList.push_back(ConfigInfo("ShowGameSettingsDialogOnNewGame", CONFIG_TYPE_INT, "1"));
configList.push_back(ConfigInfo("NetNumberOfPlayers", CONFIG_TYPE_INT, "7"));
configList.push_back(ConfigInfo("NetStartCash", CONFIG_TYPE_INT, "3000"));
configList.push_back(ConfigInfo("NetSmallBlind", CONFIG_TYPE_INT, "10"));
configList.push_back(ConfigInfo("NetHandsBeforeRaiseSmallBlind", CONFIG_TYPE_INT, "8"));
configList.push_back(ConfigInfo("NetGameSpeed", CONFIG_TYPE_INT, "4"));
configList.push_back(ConfigInfo("NetEngineVersion", CONFIG_TYPE_INT, "0"));
configList.push_back(ConfigInfo("ServerPassword", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("ServerUseIpv6", CONFIG_TYPE_INT, "0"));
configList.push_back(ConfigInfo("ServerPort", CONFIG_TYPE_INT, "7234"));
configList.push_back(ConfigInfo("MyName", CONFIG_TYPE_STRING, "Human Player"));
configList.push_back(ConfigInfo("MyAvatar", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("Opponent1Name", CONFIG_TYPE_STRING, "Player 1"));
configList.push_back(ConfigInfo("Opponent1Avatar", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("Opponent2Name", CONFIG_TYPE_STRING, "Player 2"));
configList.push_back(ConfigInfo("Opponent2Avatar", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("Opponent3Name", CONFIG_TYPE_STRING, "Player 3"));
configList.push_back(ConfigInfo("Opponent3Avatar", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("Opponent4Name", CONFIG_TYPE_STRING, "Player 4"));
configList.push_back(ConfigInfo("Opponent4Avatar", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("Opponent5Name", CONFIG_TYPE_STRING, "Player 5"));
configList.push_back(ConfigInfo("Opponent5Avatar", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("Opponent6Name", CONFIG_TYPE_STRING, "Player 6"));
configList.push_back(ConfigInfo("Opponent6Avatar", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("LogDir", CONFIG_TYPE_STRING, logDir));
configList.push_back(ConfigInfo("LogStoreDuration", CONFIG_TYPE_INT, "2"));
configList.push_back(ConfigInfo("DataDir", CONFIG_TYPE_STRING, dataDir));
2007-05-08 22:49:38 +00:00
configList.push_back(ConfigInfo("LogStoreDuration", CONFIG_TYPE_INT, "2"));
2007-05-08 20:54:36 +00:00
if(myConfigState == NONEXISTING) {
2007-05-05 20:38:43 +00:00
2007-05-08 22:49:38 +00:00
//Create a new ConfigFile!
2007-05-05 21:25:50 +00:00
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "");
doc.LinkEndChild( decl );
TiXmlElement * root = new TiXmlElement( "PokerTH" );
doc.LinkEndChild( root );
TiXmlElement * config;
config = new TiXmlElement( "Configuration" );
root->LinkEndChild( config );
2007-05-08 20:54:36 +00:00
for (i=0; i<configList.size(); i++) {
2007-05-08 21:05:45 +00:00
TiXmlElement *tmpElement = new TiXmlElement(configList[i].name);
config->LinkEndChild( tmpElement );
tmpElement->SetAttribute("value", myQtToolsInterface->stringToUtf8(configList[i].defaultValue));
2007-05-08 20:54:36 +00:00
}
2007-05-05 21:25:50 +00:00
doc.SaveFile( configFileName );
2007-05-05 20:38:43 +00:00
}
2007-05-06 21:45:04 +00:00
2007-05-08 20:54:36 +00:00
if(myConfigState == OLD) {
2007-05-08 22:49:38 +00:00
TiXmlDocument oldDoc(configFileName);
//load the old one
if(oldDoc.LoadFile()) {
string tempString("");
TiXmlDocument newDoc;
//Create the new one
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "");
newDoc.LinkEndChild( decl );
TiXmlElement * root = new TiXmlElement( "PokerTH" );
newDoc.LinkEndChild( root );
TiXmlElement * config;
config = new TiXmlElement( "Configuration" );
root->LinkEndChild( config );
TiXmlElement * confElement0 = new TiXmlElement( "ConfigRevision" );
config->LinkEndChild( confElement0 );
confElement0->SetAttribute("value", configRev);
TiXmlHandle docHandle( &oldDoc );
// not i=0 because ConfigRevision is already set ^^
for (i=1; i<configList.size(); i++) {
TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).ToElement();
if ( conf ) {
// if element is already there --> take over the saved values
TiXmlElement *tmpElement = new TiXmlElement(configList[i].name);
config->LinkEndChild( tmpElement );
const char *tmpStr = conf->Attribute("value");
if (tmpStr) tempString = tmpStr;
tmpElement->SetAttribute("value", myQtToolsInterface->stringToUtf8(tempString));
}
else {
// if element is not there --> set it with defaultValue
TiXmlElement *tmpElement = new TiXmlElement(configList[i].name);
config->LinkEndChild( tmpElement );
tmpElement->SetAttribute("value", myQtToolsInterface->stringToUtf8(configList[i].defaultValue));
}
}
newDoc.SaveFile( configFileName );
}
else { cout << "cannot update config file. did not found it" << endl; }
2007-05-06 21:45:04 +00:00
}
2007-04-21 20:42:50 +00:00
delete myQtToolsInterface;
myQtToolsInterface = 0;
2007-04-20 08:18:18 +00:00
}
2007-02-03 22:47:16 +00:00
string ConfigFile::readConfigString(string varName)
2007-01-21 16:01:37 +00:00
{
2007-01-22 21:29:51 +00:00
string tempString("");
2007-01-21 16:01:37 +00:00
2007-04-20 23:14:08 +00:00
TiXmlDocument doc(configFileName);
if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; }
2007-01-21 16:01:37 +00:00
TiXmlHandle docHandle( &doc );
2007-01-22 21:29:51 +00:00
TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement();
if ( conf ) {
const char *tmpStr = conf->Attribute("value");
if (tmpStr) tempString = tmpStr;
} /*else {
2007-01-22 21:29:51 +00:00
//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);
2007-04-20 23:14:08 +00:00
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
2007-01-22 21:29:51 +00:00
return readConfigString(varName, defaultValue);
}
}*/
2007-01-22 21:29:51 +00:00
return tempString;
2007-01-21 16:01:37 +00:00
}
2007-02-03 22:47:16 +00:00
int ConfigFile::readConfigInt(string varName)
2007-01-21 22:44:11 +00:00
{
2007-01-22 12:04:47 +00:00
int tempInt=0;
// cout << varName << " : " << tempInt << "\n";
2007-04-20 23:14:08 +00:00
TiXmlDocument doc(configFileName);
if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; }
2007-01-21 22:44:11 +00:00
TiXmlHandle docHandle( &doc );
2007-01-21 22:44:11 +00:00
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
2007-01-22 21:29:51 +00:00
TiXmlElement* config = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).ToElement();
if ( config ) {
TiXmlElement * confElement1 = new TiXmlElement( varName );
config->LinkEndChild( confElement1 );
confElement1->SetAttribute("value", defaultValue);
2007-04-20 23:14:08 +00:00
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
2007-01-22 21:29:51 +00:00
return readConfigInt(varName, defaultValue);
}
}*/
2007-01-21 22:44:11 +00:00
return tempInt;
}
2007-01-22 21:29:51 +00:00
void ConfigFile::writeConfigInt(string varName, int varCont)
{
2007-04-20 23:14:08 +00:00
TiXmlDocument doc(configFileName);
if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; }
2007-01-22 21:29:51 +00:00
TiXmlHandle docHandle( &doc );
TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( varName ).ToElement();
if ( conf ) {
conf->SetAttribute("value", varCont );
2007-04-20 23:14:08 +00:00
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
2007-01-22 21:29:51 +00:00
} 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);
2007-04-20 23:14:08 +00:00
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
2007-01-22 21:29:51 +00:00
}
}
2007-04-20 23:14:08 +00:00
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
2007-01-21 16:01:37 +00:00
}
2007-01-21 22:44:11 +00:00
2007-01-22 21:29:51 +00:00
void ConfigFile::writeConfigString(string varName, string varCont)
2007-01-21 22:44:11 +00:00
{
2007-01-22 21:29:51 +00:00
2007-04-20 23:14:08 +00:00
TiXmlDocument doc(configFileName);
if(!doc.LoadFile()) { cout << "Could Not Load Config-File!!! " << configFileName << "\n"; }
2007-01-22 21:29:51 +00:00
TiXmlHandle docHandle( &doc );
2007-01-21 22:44:11 +00:00
2007-01-22 21:29:51 +00:00
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);
2007-04-20 23:14:08 +00:00
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
2007-01-22 21:29:51 +00:00
}
}
2007-04-20 23:14:08 +00:00
if(!doc.SaveFile()) { cout << "Could Not Save Config-File!!! " << configFileName << "\n"; }
2007-01-21 22:44:11 +00:00
}