update thread setup with a good cap
[physics.git] / src / config / reader.cpp
index ce35ab8..a981719 100644 (file)
 #include "../debug.h"
 
 #include <iostream>
+using std::cerr;
+using std::cout;
+using std::endl;
+
 #include <fstream>
 #include <string>
 
+#include "keys.h"
+
 using std::ifstream;
 using std::string;
 
-char* configDir = "../configs/";
+typedef std::map<string, SDLKey> kMap;
+kMap keyMap;
 
 /// ***** Private Method Headers *****
 
-void processLine(const string& str, keyMap* map);
+void processLine(const string& str);
+bool extractLine(const string& str, string* name, string* value);
+void createKeyMap();
 
 /// ***** Private Variables *****
 
+const char* configDir = "configs/";
+const char* testFile = "keys.cfg";
+
 /// ***** Public Methods *****
 
-void readConfigs(keyMap* map)
+void readConfigs()
 {
     char fileName[64];
 
+    createKeyMap();
+
     strcpy(fileName, configDir);
-    strcat(fileName, "test.cfg");
+    strcat(fileName, testFile);
 
     ifstream file(fileName);
 
@@ -58,7 +72,7 @@ void readConfigs(keyMap* map)
         if(file.eof())
             break;
 
-        processLine(line, map);
+        processLine(line);
     }
 
     file.close();
@@ -66,7 +80,221 @@ void readConfigs(keyMap* map)
 
 /// ***** Private Methods *****
 
-void processLine(const string& str, keyMap* map)
+void processLine(const string& str)
+{
+    string name;
+    string value;
+
+    bool extracted;
+
+    extracted = extractLine(str, &name, &value);
+
+    if(extracted)
+    {
+        SDLKey key = keyMap[value];
+
+        if(0 != key)
+        {
+            *(key::sdlMap[name]) = key;
+        }
+    }
+}
+
+/*
+ * Return true iff the string str, had a name and value extracted and saved in
+ * name and value. Otherwise return false and leave name and value alone.
+ */
+bool extractLine(const string& str, string* name, string* value)
+{
+    /*
+     * state    := 0 if eating leading name whitespace
+     *          := 1 if saving name
+     *          := 2 if eating trailing name whitespace
+     *          := 3 if eating leading value whitespace
+     *          := 4 if saving value
+     *          := 5 if all is done and well
+     */
+    int state = 0;
+
+    int char_pos = 0;
+
+    int name_start = 0;
+    int name_end = 0;
+
+    int value_start = 0;
+    int value_end = 0;
+
+    const char* c_str = str.c_str();
+
+    while(true)
+    {
+        if(c_str[char_pos] == 0 && state != 4)
+            return false;
+
+        switch (state)
+        {
+            case 0:
+                if(c_str[char_pos] != ' '
+                && c_str[char_pos] != '\t')
+                {
+                    state++;
+                    name_start = char_pos;
+                }
+                break;
+            case 1:
+                if(c_str[char_pos] == ' '
+                || c_str[char_pos] == '\t'
+                || c_str[char_pos] == '=')
+                {
+                    state++;
+                    name_end = char_pos;
+                }
+
+                if(c_str[char_pos] == '=')
+                    char_pos--; // decrement to stay on this char
+                break;
+            case 2:
+                if(c_str[char_pos] != ' '
+                && c_str[char_pos] != '\t'
+                && c_str[char_pos] != '=')
+                {
+                    return false;
+                }
+
+                if(c_str[char_pos] == '=')
+                {
+                    state++;
+                }
+                break;
+            case 3:
+                if(c_str[char_pos] != ' '
+                && c_str[char_pos] != '\t')
+                {
+                    state++;
+                    value_start = char_pos;
+                }
+                break;
+            case 4:
+                if(c_str[char_pos] == ' '
+                || c_str[char_pos] == '\t'
+                || c_str[char_pos] == 0)
+                {
+                    state++;
+                    value_end = char_pos;
+                }
+                break;
+        }
+
+        if(state == 5)
+            break;
+
+        char_pos++;
+    }
+
+    name->clear();
+    value->clear();
+
+    name->replace   (0, 0, c_str, name_start,   name_end  - name_start);
+    value->replace  (0, 0, c_str, value_start,  value_end - value_start);
+
+    return true;
+}
+
+void createKeyMap()
 {
-    cout << str << endl;
+    char buf[2] = {0,0};
+
+    // add all the letters
+    for (int i = 'A'; i <= 'Z'; i++)   // uppercase
+    {
+        buf[0] = (char)i;
+        keyMap[buf] = (SDLKey)i;
+    }
+    for (int i = 'a'; i <= 'z'; i++)   // lowercase
+    {
+        buf[0] = (char)i;
+        keyMap[buf] = (SDLKey)i;
+    }
+
+    // add all the numbers
+    for (int i = '0'; i <= '9'; i++)
+    {
+        buf[0] = (char)i;
+        keyMap[buf] = (SDLKey)i;
+    }
+
+    /*
+    // add the function keys
+    int F1 = (int)Key.F1;
+    for (int i = F1; i <= (int)Key.F15; i++)
+    {
+        keyMap.Add("F" + (i - F1 + 1), (Key)i);
+        keyMap.Add("f" + (i - F1 + 1), (Key)i);
+    }
+    */
+
+    keyMap["LCtrl"]         = SDLK_LCTRL;
+    keyMap["LeftControl"]   = SDLK_LCTRL;
+    keyMap["LAlt"]          = SDLK_LALT;
+    keyMap["LeftAlt"]       = SDLK_LALT;
+    keyMap["LShift"]        = SDLK_LSHIFT;
+    keyMap["LeftShift"]     = SDLK_LSHIFT;
+    keyMap["LWin"]          = SDLK_LSUPER;
+    keyMap["LeftWindows"]   = SDLK_LSUPER;
+    keyMap["LeftMeta"]      = SDLK_LMETA;
+    keyMap["LMeta"]         = SDLK_LMETA;
+
+    keyMap["RCtrl"]         = SDLK_RCTRL;
+    keyMap["RightControl"]  = SDLK_RCTRL;
+    keyMap["RAlt"]          = SDLK_RALT;
+    keyMap["RightAlt"]      = SDLK_RALT;
+    keyMap["RShift"]        = SDLK_RSHIFT;
+    keyMap["RightShift"]    = SDLK_RSHIFT;
+    keyMap["RWin"]          = SDLK_RSUPER;
+    keyMap["RightWindows"]  = SDLK_RSUPER;
+    keyMap["RightMeta"]     = SDLK_RMETA;
+    keyMap["RMeta"]         = SDLK_RMETA;
+
+    keyMap["Esc"]           = SDLK_ESCAPE;
+    keyMap["Escape"]        = SDLK_ESCAPE;
+
+    keyMap["Return"]        = SDLK_RETURN;
+    keyMap["Enter"]         = SDLK_RETURN;
+
+    keyMap["Insert"]        = SDLK_INSERT;
+    keyMap["Home"]          = SDLK_HOME;
+    keyMap["Delete"]        = SDLK_DELETE;
+    keyMap["End"]           = SDLK_END;
+    keyMap["PageUp"]        = SDLK_PAGEUP;
+    keyMap["PageDown"]      = SDLK_PAGEDOWN;
+
+    keyMap["Minus"]         = SDLK_MINUS;
+    keyMap["Equal"]         = SDLK_EQUALS;
+    keyMap["Equals"]        = SDLK_EQUALS;
+    keyMap["LeftBracket"]   = SDLK_LEFTBRACKET;
+    keyMap["LBracket"]      = SDLK_LEFTBRACKET;
+    keyMap["RightBracket"]  = SDLK_RIGHTBRACKET;
+    keyMap["RBracket"]      = SDLK_RIGHTBRACKET;
+    keyMap["Backslash"]     = SDLK_BACKSLASH;
+    keyMap["Slash"]         = SDLK_SLASH;
+    keyMap["Semicolon"]     = SDLK_SEMICOLON;
+    keyMap["Semi"]          = SDLK_SEMICOLON;
+    keyMap["Quote"]         = SDLK_QUOTE;
+    keyMap["Comma"]         = SDLK_COMMA;
+    keyMap["Period"]        = SDLK_PERIOD;
+    keyMap["Space"]         = SDLK_SPACE;
+    keyMap["BSpace"]        = SDLK_BACKSPACE;
+    keyMap["Backspace"]     = SDLK_BACKSPACE;
+    keyMap["BackSpace"]     = SDLK_BACKSPACE;
+
+    keyMap["Tab"]           = SDLK_TAB;
+    keyMap["BackQuote"]     = SDLK_BACKQUOTE;
+    keyMap["BQuote"]        = SDLK_BACKQUOTE;
+    keyMap["CapsLock"]      = SDLK_CAPSLOCK;
+    keyMap["Caps"]          = SDLK_CAPSLOCK;
+
+    keyMap["Up"]            = SDLK_UP;
+    keyMap["Down"]          = SDLK_DOWN;
+    keyMap["Left"]          = SDLK_LEFT;
+    keyMap["Right"]         = SDLK_RIGHT;
 }