From a685b436ec3dbb36b4cf4726240289d453ab7bfd Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Sun, 23 Oct 2011 10:38:39 +0200 Subject: [PATCH] InputManager: improved config file parsing. Do not test whether config file exists; just open it and handle failure. Config file was not closed on errors, fixed now. Make sure unconfigured entries of the button map are properly initialized. Ignore lines with invalid keywords instead of aborting parsing. Made log messages more specific. --- src/inputmanager.cpp | 66 +++++++++++++++++++++----------------------- src/inputmanager.h | 4 +-- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index 3dfad58..ca98acf 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -29,9 +29,10 @@ using namespace std; void InputManager::init(const string &conffile) { - if (!readConfFile(conffile)) { - ERROR("InputManager initialization from config file failed.\n"); + for (int i = 0; i < BUTTON_TYPE_SIZE; i++) { + buttonMap[i].source = UNMAPPED; } + readConfFile(conffile); } InputManager::InputManager() @@ -53,25 +54,20 @@ InputManager::~InputManager() { #endif } -bool InputManager::readConfFile(const string &conffile) { - if (!fileExists(conffile)) { - return false; - } - +void InputManager::readConfFile(const string &conffile) { ifstream inf(conffile.c_str(), ios_base::in); - if (!(inf.is_open())) { - return false; + if (inf.fail()) { + ERROR("InputManager: failed to open config file\n"); + return; } - string line, name, source; - string::size_type pos; - Button button; - - while(getline(inf, line, '\n')) { - pos = line.find("="); - name = trim(line.substr(0,pos)); + string line; + while (getline(inf, line, '\n')) { + string::size_type pos = line.find("="); + string name = trim(line.substr(0,pos)); line = trim(line.substr(pos+1,line.length())); + Button button; if (name == "up") button = UP; else if (name == "down") button = DOWN; else if (name == "left") button = LEFT; @@ -88,31 +84,33 @@ bool InputManager::readConfFile(const string &conffile) { else if (name == "voldown") button = VOLDOWN; else if (name == "power") button = POWER; else if (name == "lock") button = LOCK; - else return false; - - pos = line.find(","); - source = trim(line.substr(0,pos)); - line = trim(line.substr(pos+1, line.length())); - - if (source == "keyboard") { - buttonMap[button].source = KEYBOARD; - } else if (source == "joystick") { -#ifdef SDL_JOYSTICK_DISABLED - WARNING("Ignoring joystick button mapping " - "because SDL was compiled without joystick support\n"); + else { + WARNING("InputManager: Ignoring unknown button name \"%s\"\n", + name.c_str()); continue; -#else - buttonMap[button].source = JOYSTICK; -#endif - } else { - return false; } + pos = line.find(","); + string sourceStr = trim(line.substr(0,pos)); + line = trim(line.substr(pos+1, line.length())); + + ButtonSource source; + if (sourceStr == "keyboard") { + source = KEYBOARD; +#ifndef SDL_JOYSTICK_DISABLED + } else if (sourceStr == "joystick") { + source = JOYSTICK; +#endif + } else { + WARNING("InputManager: Ignoring unknown button source \"%s\"\n", + sourceStr.c_str()); + continue; + } + buttonMap[button].source = source; buttonMap[button].code = atoi(line.c_str()); } inf.close(); - return true; } InputManager::Button InputManager::waitForPressedButton() { diff --git a/src/inputmanager.h b/src/inputmanager.h index 246d634..e52c112 100644 --- a/src/inputmanager.h +++ b/src/inputmanager.h @@ -53,13 +53,13 @@ public: bool pollEvent(ButtonEvent *event); private: - enum ButtonSource { KEYBOARD, JOYSTICK }; + enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK }; struct ButtonMapEntry { ButtonSource source; unsigned int code; }; - bool readConfFile(const std::string &conffile); + void readConfFile(const std::string &conffile); bool getEvent(ButtonEvent *bevent, bool wait); Button waitForButton(ButtonState state);