Files
pokerth/src/config/configfile.cpp
T

478 lines
16 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-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-05-22 22:40:44 +00:00
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
2007-04-21 20:42:50 +00:00
class QtToolsWrapper;
2007-01-21 16:01:37 +00:00
2007-05-16 08:55:49 +00:00
ConfigFile::ConfigFile(int argc, char **argv) : noWriteAccess(0)
2007-04-20 23:14:08 +00:00
{
2007-05-16 08:55:49 +00:00
int i;
2007-04-20 08:18:18 +00:00
2007-05-16 08:55:49 +00:00
for (i=0; i<argc; i++) {
if(strcmp(argv[i], "--nowriteaccess") == 0) { noWriteAccess = 1; }
2007-04-20 23:14:08 +00:00
}
2007-05-16 08:55:49 +00:00
// !!!! Revisionsnummer der Configdefaults !!!!!
2007-06-03 22:41:53 +00:00
configRev = 25;
2007-05-16 08:55:49 +00:00
//standard defaults
logOnOffDefault = "1";
claNoWriteAccess = "0";
if(!noWriteAccess) {
// Pfad und Dateinamen setzen
#ifdef _WIN32
const char *appDataPath = getenv("AppData");
if (appDataPath && appDataPath[0] != 0) {
configFileName = appDataPath;
2007-04-20 23:14:08 +00:00
}
else {
2007-05-16 08:55:49 +00:00
const int MaxPathSize = 1024;
char curDir[MaxPathSize + 1];
2007-04-20 23:14:08 +00:00
curDir[0] = 0;
2007-05-16 08:55:49 +00:00
_getcwd(curDir, MaxPathSize);
2007-04-20 23:14:08 +00:00
curDir[MaxPathSize] = 0;
configFileName = curDir;
2007-05-16 08:55:49 +00:00
// 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-04-20 23:14:08 +00:00
}
2007-05-16 08:55:49 +00:00
//define app-dir
configFileName += "\\pokerth\\";
////define log-dir
logDir = configFileName;
logDir += "log-files\\";
////define data-dir
dataDir = configFileName;
dataDir += "data\\";
//create directories on first start of app
2007-05-05 19:02:11 +00:00
mkdir(configFileName.c_str());
mkdir(logDir.c_str());
mkdir(dataDir.c_str());
2007-04-20 08:18:18 +00:00
2007-05-16 08:55:49 +00:00
2007-04-20 08:18:18 +00:00
#else
2007-05-16 08:55:49 +00:00
//define app-dir
const char *homePath = getenv("HOME");
if(homePath) {
configFileName = homePath;
configFileName += "/.pokerth/";
////define log-dir
logDir = configFileName;
logDir += "log-files/";
////define data-dir
dataDir = configFileName;
dataDir += "data/";
//create directories on first start of app
2007-05-05 19:02:11 +00:00
mkdir(configFileName.c_str(), MODUS) ;
mkdir(logDir.c_str(), MODUS);
mkdir(dataDir.c_str(), MODUS);
2007-05-16 08:55:49 +00:00
2007-05-05 19:02:11 +00:00
}
2007-05-16 08:55:49 +00:00
2007-04-20 08:18:18 +00:00
#endif
2007-05-16 08:55:49 +00:00
}
else {
//no writeaccess
configFileName = "";
logDir = "";
dataDir = "";
logOnOffDefault = "0";
claNoWriteAccess = "1";
}
2007-05-08 20:54:36 +00:00
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"));
2007-05-17 23:28:36 +00:00
configList.push_back(ConfigInfo("ShowBlindButtons", CONFIG_TYPE_INT, "1"));
2007-05-30 08:05:53 +00:00
configList.push_back(ConfigInfo("PlaySoundEffects", CONFIG_TYPE_INT, "1"));
2007-05-08 20:54:36 +00:00
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"));
2007-05-30 08:05:53 +00:00
configList.push_back(ConfigInfo("NetTimeOutPlayerAction", CONFIG_TYPE_INT, "20"));
2007-05-08 20:54:36 +00:00
configList.push_back(ConfigInfo("ServerPassword", CONFIG_TYPE_STRING, ""));
configList.push_back(ConfigInfo("ServerUseIpv6", CONFIG_TYPE_INT, "0"));
2007-06-03 22:41:53 +00:00
configList.push_back(ConfigInfo("ServerUseSctp", CONFIG_TYPE_INT, "0"));
2007-05-08 20:54:36 +00:00
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, ""));
2007-05-16 08:55:49 +00:00
configList.push_back(ConfigInfo("LogOnOff", CONFIG_TYPE_INT, logOnOffDefault));
2007-05-08 20:54:36 +00:00
configList.push_back(ConfigInfo("LogDir", CONFIG_TYPE_STRING, logDir));
configList.push_back(ConfigInfo("LogStoreDuration", CONFIG_TYPE_INT, "2"));
2007-05-15 21:22:16 +00:00
configList.push_back(ConfigInfo("LogInterval", CONFIG_TYPE_INT, "0"));
2007-05-08 20:54:36 +00:00
configList.push_back(ConfigInfo("DataDir", CONFIG_TYPE_STRING, dataDir));
2007-05-16 08:55:49 +00:00
configList.push_back(ConfigInfo("CLA_NoWriteAccess", CONFIG_TYPE_INT, claNoWriteAccess));
2007-05-08 20:54:36 +00:00
//fill tempList firstTime
configBufferList = configList;
2007-05-08 20:54:36 +00:00
// cout << configTempList[3].name << " " << configTempList[10].defaultValue << endl;
2007-05-16 08:55:49 +00:00
if(!noWriteAccess) {
configFileName += "config.xml";
//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) ;
}
}
2007-05-16 08:55:49 +00:00
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;
2007-05-21 20:11:23 +00:00
configBufferList[i].defaultValue = 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;
}
2007-06-09 09:45:04 +00:00
void ConfigFile::writeBuffer() {
//write buffer to disc if enabled
if(!noWriteAccess) {
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 );
size_t i;
for (i=0; i<configBufferList.size(); i++) {
TiXmlElement *tmpElement = new TiXmlElement(configBufferList[i].name);
config->LinkEndChild( tmpElement );
tmpElement->SetAttribute("value", configBufferList[i].defaultValue);
}
doc.SaveFile( configFileName );
}
}
void ConfigFile::updateConfig(ConfigState myConfigState) {
size_t i;
QtToolsInterface *myQtToolsInterface = new QtToolsWrapper;
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;
2007-05-28 11:39:02 +00:00
tmpElement->SetAttribute("value", tempString);
2007-05-08 22:49:38 +00:00
}
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
{
size_t i;
2007-01-22 21:29:51 +00:00
string tempString("");
2007-01-21 16:01:37 +00:00
// 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; i<configBufferList.size(); i++) {
2007-01-21 16:01:37 +00:00
if (configBufferList[i].name == varName) {
tempString = configBufferList[i].defaultValue;
2007-01-22 21:29:51 +00:00
}
}
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
{
size_t i;
string tempString("");
2007-01-22 12:04:47 +00:00
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"; }
//
// return readConfigInt(varName, defaultValue);
// }
// }*/
2007-01-22 21:29:51 +00:00
for (i=0; i<configBufferList.size(); i++) {
if (configBufferList[i].name == varName) {
tempString = configBufferList[i].defaultValue;
2007-01-22 21:29:51 +00:00
}
}
istringstream isst;
isst.str (tempString);
isst >> tempInt;
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-06-09 09:45:04 +00:00
size_t i;
string tempString;
ostringstream intToString;
2007-05-16 08:55:49 +00:00
2007-06-09 09:45:04 +00:00
for (i=0; i<configBufferList.size(); i++) {
2007-05-16 08:55:49 +00:00
2007-06-09 09:45:04 +00:00
if (configBufferList[i].name == varName) {
intToString << varCont;
configBufferList[i].defaultValue = intToString.str();
2007-01-22 21:29:51 +00:00
}
}
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-06-09 09:45:04 +00:00
size_t i;
for (i=0; i<configBufferList.size(); i++) {
if (configBufferList[i].name == varName) { configBufferList[i].defaultValue = varCont; }
2007-05-16 08:55:49 +00:00
}
2007-06-09 09:45:04 +00:00
}