From 92ba846d300908a4855ce84f3b33771540f8f132 Mon Sep 17 00:00:00 2001 From: lotodore Date: Mon, 9 Jan 2012 18:40:51 +0000 Subject: [PATCH] When loading the configuration, verify that the player names are unique. If they are not, use the default ones. (#96) --- src/config/configfile.cpp | 35 ++++++++++++++++++++++++++++++++++- src/config/configfile.h | 4 ++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/config/configfile.cpp b/src/config/configfile.cpp index 3796ca43..8156be76 100644 --- a/src/config/configfile.cpp +++ b/src/config/configfile.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -322,6 +323,7 @@ ConfigFile::ConfigFile(char *argv0, bool readonly) : noWriteAccess(readonly) } fillBuffer(); + checkAndCorrectBuffer(); } } @@ -384,6 +386,37 @@ void ConfigFile::fillBuffer() } } +void ConfigFile::checkAndCorrectBuffer() +{ + boost::recursive_mutex::scoped_lock lock(m_configMutex); + // For now, only the player names are checked. + checkAndCorrectPlayerNames(); +} + +void ConfigFile::checkAndCorrectPlayerNames() +{ + // Verify that the player names are uniquely set. + set playerNames; + playerNames.insert(readConfigString("MyName")); + for(int i = 1; i <= 9; i++) { + ostringstream opponentVar; + opponentVar << "Opponent" << i << "Name"; + playerNames.insert(readConfigString(opponentVar.str())); + } + if (playerNames.size() < 10 || playerNames.find("") != playerNames.end()) { + // The set contains less than 10 players or an empty player name. + // Reset to default player names. + writeConfigString("MyName", "Human Player"); + for(int i = 1; i <= 9; i++) { + ostringstream opponentVar; + ostringstream opponentName; + opponentVar << "Opponent" << i << "Name"; + opponentName << "Player " << i; + writeConfigString(opponentVar.str(), opponentName.str()); + } + } +} + void ConfigFile::writeBuffer() const { @@ -527,7 +560,7 @@ void ConfigFile::updateConfig(ConfigState myConfigState) //test - bool contains = noUpdateElemtsList.find(value) != mylist.end(); + //bool contains = noUpdateElemtsList.find(value) != mylist.end(); /////// HIER GEHTS WEITER LOTHAR ;) /////// diff --git a/src/config/configfile.h b/src/config/configfile.h index c306af66..e6c142cf 100644 --- a/src/config/configfile.h +++ b/src/config/configfile.h @@ -36,6 +36,7 @@ public: ~ConfigFile(); void fillBuffer(); + void checkAndCorrectBuffer(); void writeBuffer() const; void updateConfig(ConfigState); @@ -51,6 +52,9 @@ public: void writeConfigIntList(std::string varName, std::list varCont); void deleteConfigFile(); +protected: + void checkAndCorrectPlayerNames(); + private: mutable boost::recursive_mutex m_configMutex;