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/>.
30 typedef std::map<string, SDLKey> kMap;
33 /// ***** Private Method Headers *****
35 void processLine(const string& str);
36 bool extractLine(const string& str, string* name, string* value);
39 /// ***** Private Variables *****
41 const char* configDir = "configs/";
42 const char* testFile = "keys.cfg";
44 /// ***** Public Methods *****
52 strcpy(fileName, configDir);
53 strcat(fileName, testFile);
55 ifstream file(fileName);
59 cerr << "Unable to open file " << fileName << "." << endl;
77 /// ***** Private Methods *****
79 void processLine(const string& str)
86 extracted = extractLine(str, &name, &value);
90 SDLKey key = keyMap[value];
94 *(key::sdlMap[name]) = key;
97 cout << value << endl;
103 * Return true iff the string str, had a name and value extracted and saved in
104 * name and value. Otherwise return false and leave name and value alone.
106 bool extractLine(const string& str, string* name, string* value)
109 * state := 0 if eating leading name whitespace
110 * := 1 if saving name
111 * := 2 if eating trailing name whitespace
112 * := 3 if eating leading value whitespace
113 * := 4 if saving value
114 * := 5 if all is done and well
126 const char* c_str = str.c_str();
130 if(c_str[char_pos] == 0 && state != 4)
136 if(c_str[char_pos] != ' '
137 && c_str[char_pos] != '\t')
140 name_start = char_pos;
144 if(c_str[char_pos] == ' '
145 || c_str[char_pos] == '\t'
146 || c_str[char_pos] == '=')
152 if(c_str[char_pos] == '=')
153 char_pos--; // decrement to stay on this char
156 if(c_str[char_pos] != ' '
157 && c_str[char_pos] != '\t'
158 && c_str[char_pos] != '=')
163 if(c_str[char_pos] == '=')
169 if(c_str[char_pos] != ' '
170 && c_str[char_pos] != '\t')
173 value_start = char_pos;
177 if(c_str[char_pos] == ' '
178 || c_str[char_pos] == '\t'
179 || c_str[char_pos] == 0)
182 value_end = char_pos;
196 name->replace (0, 0, c_str, name_start, name_end - name_start);
197 value->replace (0, 0, c_str, value_start, value_end - value_start);
206 // add all the letters
207 for (int i = 'A'; i <= 'Z'; i++) // uppercase
210 keyMap[buf] = (SDLKey)i;
212 for (int i = 'a'; i <= 'z'; i++) // lowercase
215 keyMap[buf] = (SDLKey)i;
218 // add all the numbers
219 for (int i = '0'; i <= '9'; i++)
222 keyMap[buf] = (SDLKey)i;
226 // add the function keys
227 int F1 = (int)Key.F1;
228 for (int i = F1; i <= (int)Key.F15; i++)
230 keyMap.Add("F" + (i - F1 + 1), (Key)i);
231 keyMap.Add("f" + (i - F1 + 1), (Key)i);
235 keyMap["LCtrl"] = SDLK_LCTRL;
236 keyMap["LeftControl"] = SDLK_LCTRL;
237 keyMap["LAlt"] = SDLK_LALT;
238 keyMap["LeftAlt"] = SDLK_LALT;
239 keyMap["LShift"] = SDLK_LSHIFT;
240 keyMap["LeftShift"] = SDLK_LSHIFT;
241 keyMap["LWin"] = SDLK_LSUPER;
242 keyMap["LeftWindows"] = SDLK_LSUPER;
243 keyMap["LeftMeta"] = SDLK_LMETA;
244 keyMap["LMeta"] = SDLK_LMETA;
246 keyMap["RCtrl"] = SDLK_RCTRL;
247 keyMap["RightControl"] = SDLK_RCTRL;
248 keyMap["RAlt"] = SDLK_RALT;
249 keyMap["RightAlt"] = SDLK_RALT;
250 keyMap["RShift"] = SDLK_RSHIFT;
251 keyMap["RightShift"] = SDLK_RSHIFT;
252 keyMap["RWin"] = SDLK_RSUPER;
253 keyMap["RightWindows"] = SDLK_RSUPER;
254 keyMap["RightMeta"] = SDLK_RMETA;
255 keyMap["RMeta"] = SDLK_RMETA;
257 keyMap["Esc"] = SDLK_ESCAPE;
258 keyMap["Escape"] = SDLK_ESCAPE;
260 keyMap["Return"] = SDLK_RETURN;
261 keyMap["Enter"] = SDLK_RETURN;
263 keyMap["Insert"] = SDLK_INSERT;
264 keyMap["Home"] = SDLK_HOME;
265 keyMap["Delete"] = SDLK_DELETE;
266 keyMap["End"] = SDLK_END;
267 keyMap["PageUp"] = SDLK_PAGEUP;
268 keyMap["PageDown"] = SDLK_PAGEDOWN;
270 keyMap["Minus"] = SDLK_MINUS;
271 keyMap["Equal"] = SDLK_EQUALS;
272 keyMap["Equals"] = SDLK_EQUALS;
273 keyMap["LeftBracket"] = SDLK_LEFTBRACKET;
274 keyMap["LBracket"] = SDLK_LEFTBRACKET;
275 keyMap["RightBracket"] = SDLK_RIGHTBRACKET;
276 keyMap["RBracket"] = SDLK_RIGHTBRACKET;
277 keyMap["Backslash"] = SDLK_BACKSLASH;
278 keyMap["Slash"] = SDLK_SLASH;
279 keyMap["Semicolon"] = SDLK_SEMICOLON;
280 keyMap["Semi"] = SDLK_SEMICOLON;
281 keyMap["Quote"] = SDLK_QUOTE;
282 keyMap["Comma"] = SDLK_COMMA;
283 keyMap["Period"] = SDLK_PERIOD;
284 keyMap["Space"] = SDLK_SPACE;
285 keyMap["BSpace"] = SDLK_BACKSPACE;
286 keyMap["Backspace"] = SDLK_BACKSPACE;
287 keyMap["BackSpace"] = SDLK_BACKSPACE;
289 keyMap["Tab"] = SDLK_TAB;
290 keyMap["BackQuote"] = SDLK_BACKQUOTE;
291 keyMap["BQuote"] = SDLK_BACKQUOTE;
292 keyMap["CapsLock"] = SDLK_CAPSLOCK;
293 keyMap["Caps"] = SDLK_CAPSLOCK;
295 keyMap["Up"] = SDLK_UP;
296 keyMap["Down"] = SDLK_DOWN;
297 keyMap["Left"] = SDLK_LEFT;
298 keyMap["Right"] = SDLK_RIGHT;