From ab6286891234d4329c044402fa94257022e2299c Mon Sep 17 00:00:00 2001 From: stephena Date: Tue, 23 Sep 2003 17:27:11 +0000 Subject: [PATCH] Added error checking to the Settings class. Now, only settings specified in the program will be saved to the rcfile. Previously, *any* text in the rcfile was saved on exit, even bogus settings. Also, any settings given from the commandline which are not meant to be ever saved in the rcfile are now actually not saved. git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@187 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- stella/src/emucore/Settings.cxx | 43 ++++++++++++++++++++++----------- stella/src/emucore/Settings.hxx | 15 ++++++++---- stella/src/ui/sdl/mainSDL.cxx | 6 ++--- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/stella/src/emucore/Settings.cxx b/stella/src/emucore/Settings.cxx index 8f0b46157..3c0017fb4 100644 --- a/stella/src/emucore/Settings.cxx +++ b/stella/src/emucore/Settings.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Settings.cxx,v 1.7 2003-09-23 00:58:31 stephena Exp $ +// $Id: Settings.cxx,v 1.8 2003-09-23 17:27:11 stephena Exp $ //============================================================================ #include @@ -108,7 +108,11 @@ void Settings::loadConfig() if((key.length() == 0) || (value.length() == 0)) continue; - set(key, value); + // Only settings which have been previously set are valid + if(contains(key)) + set(key, value); + else + cerr << "Invalid setting: " << key << endl; } in.close(); @@ -128,7 +132,9 @@ bool Settings::loadCommandLine(Int32 argc, char** argv) key = key.substr(1, key.length()); string value = argv[++i]; - set(key, value); + // Settings read from the commandline must not be saved to + // the rc-file, unless they were previously set + set(key, value, false); } return true; @@ -164,20 +170,25 @@ void Settings::saveConfig() // Write out each of the key and value pairs for(uInt32 i = 0; i < mySize; ++i) - out << mySettings[i].key << " = " << mySettings[i].value << endl; + if(mySettings[i].save) + out << mySettings[i].key << " = " << mySettings[i].value << endl; out.close(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void Settings::set(const string& key, const string& value) +void Settings::set(const string& key, const string& value, bool save) { // See if the setting already exists for(uInt32 i = 0; i < mySize; ++i) { + // If a key is already present in the array, then we assume + // that it was set by the emulation core and must be saved + // to the rc-file. if(key == mySettings[i].key) { mySettings[i].value = value; + mySettings[i].save = true; return; } } @@ -200,8 +211,9 @@ void Settings::set(const string& key, const string& value) } // Add new property to the array - mySettings[mySize].key = key; + mySettings[mySize].key = key; mySettings[mySize].value = value; + mySettings[mySize].save = save; ++mySize; } @@ -211,12 +223,8 @@ Int32 Settings::getInt(const string& key) const { // Try to find the named setting and answer its value for(uInt32 i = 0; i < mySize; ++i) - { if(key == mySettings[i].key) - { return atoi(mySettings[i].value.c_str()); - } - } return -1; } @@ -246,16 +254,23 @@ string Settings::getString(const string& key) const { // Try to find the named setting and answer its value for(uInt32 i = 0; i < mySize; ++i) - { if(key == mySettings[i].key) - { return mySettings[i].value; - } - } return ""; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool Settings::contains(const string& key) +{ + // Try to find the named setting + for(uInt32 i = 0; i < mySize; ++i) + if(key == mySettings[i].key) + return true; + + return false; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Settings::Settings(const Settings&) { diff --git a/stella/src/emucore/Settings.hxx b/stella/src/emucore/Settings.hxx index ba6882570..d4f13e66a 100644 --- a/stella/src/emucore/Settings.hxx +++ b/stella/src/emucore/Settings.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Settings.hxx,v 1.6 2003-09-23 00:58:31 stephena Exp $ +// $Id: Settings.hxx,v 1.7 2003-09-23 17:27:11 stephena Exp $ //============================================================================ #ifndef SETTINGS_HXX @@ -32,7 +32,7 @@ class Console; This class provides an interface for accessing frontend specific settings. @author Stephen Anthony - @version $Id: Settings.hxx,v 1.6 2003-09-23 00:58:31 stephena Exp $ + @version $Id: Settings.hxx,v 1.7 2003-09-23 17:27:11 stephena Exp $ */ class Settings { @@ -93,10 +93,11 @@ class Settings /** Set the value associated with key to the given value. - @param key The key of the setting + @param key The key of the setting @param value The value to assign to the setting + @param save Whether this setting should be saved to the rc-file. */ - void set(const string& key, const string& value); + void set(const string& key, const string& value, bool save = true); public: ////////////////////////////////////////////////////////////////////// @@ -231,11 +232,15 @@ class Settings // Assignment operator isn't supported by this class so make it private Settings& operator = (const Settings&); - // Structure used for storing properties + // Test whether the given setting is present in the array + bool contains(const string& key); + + // Structure used for storing settings struct Setting { string key; string value; + bool save; }; // Pointer to a dynamically allocated array of settings diff --git a/stella/src/ui/sdl/mainSDL.cxx b/stella/src/ui/sdl/mainSDL.cxx index b73157b19..44caea2b1 100644 --- a/stella/src/ui/sdl/mainSDL.cxx +++ b/stella/src/ui/sdl/mainSDL.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: mainSDL.cxx,v 1.52 2003-09-23 00:58:31 stephena Exp $ +// $Id: mainSDL.cxx,v 1.53 2003-09-23 17:27:11 stephena Exp $ //============================================================================ #include @@ -1187,9 +1187,9 @@ void cleanup() if(SDL_WasInit(SDL_INIT_EVERYTHING)) { #ifdef HAVE_JOYSTICK - if(SDL_JoystickOpened(0)) + if(SDL_JoystickOpened(theLeftJoystickNumber)) SDL_JoystickClose(theLeftJoystick); - if(SDL_JoystickOpened(1)) + if(SDL_JoystickOpened(theRightJoystickNumber)) SDL_JoystickClose(theRightJoystick); #endif