2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 typedef std::map<string, SDLKey> kMap;
38 /// ***** Private Method Headers *****
40 void processLine(const string& str);
41 bool extractLine(const string& str, string* name, string* value);
44 /// ***** Private Variables *****
46 const char* configDir = "configs/";
47 const char* testFile = "keys.cfg";
49 /// ***** Public Methods *****
57 strcpy(fileName, configDir);
58 strcat(fileName, testFile);
60 ifstream file(fileName);
64 cerr << "Unable to open file " << fileName << "." << endl;
82 /// ***** Private Methods *****
84 void processLine(const string& str)
91 extracted = extractLine(str, &name, &value);
95 SDLKey key = keyMap[value];
99 *(key::sdlMap[name]) = key;
105 * Return true iff the string str, had a name and value extracted and saved in
106 * name and value. Otherwise return false and leave name and value alone.
108 bool extractLine(const string& str, string* name, string* value)
111 * state := 0 if eating leading name whitespace
112 * := 1 if saving name
113 * := 2 if eating trailing name whitespace
114 * := 3 if eating leading value whitespace
115 * := 4 if saving value
116 * := 5 if all is done and well
128 const char* c_str = str.c_str();
132 if(c_str[char_pos] == 0 && state != 4)
138 if(c_str[char_pos] != ' '
139 && c_str[char_pos] != '\t')
142 name_start = char_pos;
146 if(c_str[char_pos] == ' '
147 || c_str[char_pos] == '\t'
148 || c_str[char_pos] == '=')
154 if(c_str[char_pos] == '=')
155 char_pos--; // decrement to stay on this char
158 if(c_str[char_pos] != ' '
159 && c_str[char_pos] != '\t'
160 && c_str[char_pos] != '=')
165 if(c_str[char_pos] == '=')
171 if(c_str[char_pos] != ' '
172 && c_str[char_pos] != '\t')
175 value_start = char_pos;
179 if(c_str[char_pos] == ' '
180 || c_str[char_pos] == '\t'
181 || c_str[char_pos] == 0)
184 value_end = char_pos;
198 name->replace (0, 0, c_str, name_start, name_end - name_start);
199 value->replace (0, 0, c_str, value_start, value_end - value_start);
208 // add all the letters
209 for (int i = 'A'; i <= 'Z'; i++) // uppercase
212 keyMap[buf] = (SDLKey)(i + 'a' - 'A');
214 for (int i = 'a'; i <= 'z'; i++) // lowercase
217 keyMap[buf] = (SDLKey)i;
220 // add all the numbers
221 for (int i = '0'; i <= '9'; i++)
224 keyMap[buf] = (SDLKey)i;
228 // add the function keys
229 int F1 = (int)Key.F1;
230 for (int i = F1; i <= (int)Key.F15; i++)
232 keyMap.Add("F" + (i - F1 + 1), (Key)i);
233 keyMap.Add("f" + (i - F1 + 1), (Key)i);
237 keyMap["LCtrl"] = SDLK_LCTRL;
238 keyMap["LeftControl"] = SDLK_LCTRL;
239 keyMap["LAlt"] = SDLK_LALT;
240 keyMap["LeftAlt"] = SDLK_LALT;
241 keyMap["LShift"] = SDLK_LSHIFT;
242 keyMap["LeftShift"] = SDLK_LSHIFT;
243 keyMap["LWin"] = SDLK_LSUPER;
244 keyMap["LeftWindows"] = SDLK_LSUPER;
245 keyMap["LeftMeta"] = SDLK_LMETA;
246 keyMap["LMeta"] = SDLK_LMETA;
248 keyMap["RCtrl"] = SDLK_RCTRL;
249 keyMap["RightControl"] = SDLK_RCTRL;
250 keyMap["RAlt"] = SDLK_RALT;
251 keyMap["RightAlt"] = SDLK_RALT;
252 keyMap["RShift"] = SDLK_RSHIFT;
253 keyMap["RightShift"] = SDLK_RSHIFT;
254 keyMap["RWin"] = SDLK_RSUPER;
255 keyMap["RightWindows"] = SDLK_RSUPER;
256 keyMap["RightMeta"] = SDLK_RMETA;
257 keyMap["RMeta"] = SDLK_RMETA;
259 keyMap["Esc"] = SDLK_ESCAPE;
260 keyMap["Escape"] = SDLK_ESCAPE;
262 keyMap["Return"] = SDLK_RETURN;
263 keyMap["Enter"] = SDLK_RETURN;
265 keyMap["Insert"] = SDLK_INSERT;
266 keyMap["Home"] = SDLK_HOME;
267 keyMap["Delete"] = SDLK_DELETE;
268 keyMap["End"] = SDLK_END;
269 keyMap["PageUp"] = SDLK_PAGEUP;
270 keyMap["PageDown"] = SDLK_PAGEDOWN;
272 keyMap["Minus"] = SDLK_MINUS;
273 keyMap["Equal"] = SDLK_EQUALS;
274 keyMap["Equals"] = SDLK_EQUALS;
275 keyMap["LeftBracket"] = SDLK_LEFTBRACKET;
276 keyMap["LBracket"] = SDLK_LEFTBRACKET;
277 keyMap["RightBracket"] = SDLK_RIGHTBRACKET;
278 keyMap["RBracket"] = SDLK_RIGHTBRACKET;
279 keyMap["Backslash"] = SDLK_BACKSLASH;
280 keyMap["Slash"] = SDLK_SLASH;
281 keyMap["Semicolon"] = SDLK_SEMICOLON;
282 keyMap["Semi"] = SDLK_SEMICOLON;
283 keyMap["Quote"] = SDLK_QUOTE;
284 keyMap["Comma"] = SDLK_COMMA;
285 keyMap["Period"] = SDLK_PERIOD;
286 keyMap["Space"] = SDLK_SPACE;
287 keyMap["BSpace"] = SDLK_BACKSPACE;
288 keyMap["Backspace"] = SDLK_BACKSPACE;
289 keyMap["BackSpace"] = SDLK_BACKSPACE;
291 keyMap["Tab"] = SDLK_TAB;
292 keyMap["BackQuote"] = SDLK_BACKQUOTE;
293 keyMap["BQuote"] = SDLK_BACKQUOTE;
294 keyMap["CapsLock"] = SDLK_CAPSLOCK;
295 keyMap["Caps"] = SDLK_CAPSLOCK;
297 keyMap["Up"] = SDLK_UP;
298 keyMap["Down"] = SDLK_DOWN;
299 keyMap["Left"] = SDLK_LEFT;
300 keyMap["Right"] = SDLK_RIGHT;