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/>.
34 typedef std::map<string, SDLKey> kMap;
37 /// ***** Private Method Headers *****
39 void processLine(const string& str);
40 bool extractLine(const string& str, string* name, string* value);
43 /// ***** Private Variables *****
45 const char* configDir = "configs/";
46 const char* testFile = "keys.cfg";
48 /// ***** Public Methods *****
56 strcpy(fileName, configDir);
57 strcat(fileName, testFile);
59 ifstream file(fileName);
63 cerr << "Unable to open file " << fileName << "." << endl;
81 /// ***** Private Methods *****
83 void processLine(const string& str)
90 extracted = extractLine(str, &name, &value);
94 SDLKey key = keyMap[value];
98 *(key::sdlMap[name]) = key;
100 cout << name << endl;
101 cout << value << endl;
107 * Return true iff the string str, had a name and value extracted and saved in
108 * name and value. Otherwise return false and leave name and value alone.
110 bool extractLine(const string& str, string* name, string* value)
113 * state := 0 if eating leading name whitespace
114 * := 1 if saving name
115 * := 2 if eating trailing name whitespace
116 * := 3 if eating leading value whitespace
117 * := 4 if saving value
118 * := 5 if all is done and well
130 const char* c_str = str.c_str();
134 if(c_str[char_pos] == 0 && state != 4)
140 if(c_str[char_pos] != ' '
141 && c_str[char_pos] != '\t')
144 name_start = char_pos;
148 if(c_str[char_pos] == ' '
149 || c_str[char_pos] == '\t'
150 || c_str[char_pos] == '=')
156 if(c_str[char_pos] == '=')
157 char_pos--; // decrement to stay on this char
160 if(c_str[char_pos] != ' '
161 && c_str[char_pos] != '\t'
162 && c_str[char_pos] != '=')
167 if(c_str[char_pos] == '=')
173 if(c_str[char_pos] != ' '
174 && c_str[char_pos] != '\t')
177 value_start = char_pos;
181 if(c_str[char_pos] == ' '
182 || c_str[char_pos] == '\t'
183 || c_str[char_pos] == 0)
186 value_end = char_pos;
200 name->replace (0, 0, c_str, name_start, name_end - name_start);
201 value->replace (0, 0, c_str, value_start, value_end - value_start);
210 // add all the letters
211 for (int i = 'A'; i <= 'Z'; i++) // uppercase
214 keyMap[buf] = (SDLKey)i;
216 for (int i = 'a'; i <= 'z'; i++) // lowercase
219 keyMap[buf] = (SDLKey)i;
222 // add all the numbers
223 for (int i = '0'; i <= '9'; i++)
226 keyMap[buf] = (SDLKey)i;
230 // add the function keys
231 int F1 = (int)Key.F1;
232 for (int i = F1; i <= (int)Key.F15; i++)
234 keyMap.Add("F" + (i - F1 + 1), (Key)i);
235 keyMap.Add("f" + (i - F1 + 1), (Key)i);
239 keyMap["LCtrl"] = SDLK_LCTRL;
240 keyMap["LeftControl"] = SDLK_LCTRL;
241 keyMap["LAlt"] = SDLK_LALT;
242 keyMap["LeftAlt"] = SDLK_LALT;
243 keyMap["LShift"] = SDLK_LSHIFT;
244 keyMap["LeftShift"] = SDLK_LSHIFT;
245 keyMap["LWin"] = SDLK_LSUPER;
246 keyMap["LeftWindows"] = SDLK_LSUPER;
247 keyMap["LeftMeta"] = SDLK_LMETA;
248 keyMap["LMeta"] = SDLK_LMETA;
250 keyMap["RCtrl"] = SDLK_RCTRL;
251 keyMap["RightControl"] = SDLK_RCTRL;
252 keyMap["RAlt"] = SDLK_RALT;
253 keyMap["RightAlt"] = SDLK_RALT;
254 keyMap["RShift"] = SDLK_RSHIFT;
255 keyMap["RightShift"] = SDLK_RSHIFT;
256 keyMap["RWin"] = SDLK_RSUPER;
257 keyMap["RightWindows"] = SDLK_RSUPER;
258 keyMap["RightMeta"] = SDLK_RMETA;
259 keyMap["RMeta"] = SDLK_RMETA;
261 keyMap["Esc"] = SDLK_ESCAPE;
262 keyMap["Escape"] = SDLK_ESCAPE;
264 keyMap["Return"] = SDLK_RETURN;
265 keyMap["Enter"] = SDLK_RETURN;
267 keyMap["Insert"] = SDLK_INSERT;
268 keyMap["Home"] = SDLK_HOME;
269 keyMap["Delete"] = SDLK_DELETE;
270 keyMap["End"] = SDLK_END;
271 keyMap["PageUp"] = SDLK_PAGEUP;
272 keyMap["PageDown"] = SDLK_PAGEDOWN;
274 keyMap["Minus"] = SDLK_MINUS;
275 keyMap["Equal"] = SDLK_EQUALS;
276 keyMap["Equals"] = SDLK_EQUALS;
277 keyMap["LeftBracket"] = SDLK_LEFTBRACKET;
278 keyMap["LBracket"] = SDLK_LEFTBRACKET;
279 keyMap["RightBracket"] = SDLK_RIGHTBRACKET;
280 keyMap["RBracket"] = SDLK_RIGHTBRACKET;
281 keyMap["Backslash"] = SDLK_BACKSLASH;
282 keyMap["Slash"] = SDLK_SLASH;
283 keyMap["Semicolon"] = SDLK_SEMICOLON;
284 keyMap["Semi"] = SDLK_SEMICOLON;
285 keyMap["Quote"] = SDLK_QUOTE;
286 keyMap["Comma"] = SDLK_COMMA;
287 keyMap["Period"] = SDLK_PERIOD;
288 keyMap["Space"] = SDLK_SPACE;
289 keyMap["BSpace"] = SDLK_BACKSPACE;
290 keyMap["Backspace"] = SDLK_BACKSPACE;
291 keyMap["BackSpace"] = SDLK_BACKSPACE;
293 keyMap["Tab"] = SDLK_TAB;
294 keyMap["BackQuote"] = SDLK_BACKQUOTE;
295 keyMap["BQuote"] = SDLK_BACKQUOTE;
296 keyMap["CapsLock"] = SDLK_CAPSLOCK;
297 keyMap["Caps"] = SDLK_CAPSLOCK;
299 keyMap["Up"] = SDLK_UP;
300 keyMap["Down"] = SDLK_DOWN;
301 keyMap["Left"] = SDLK_LEFT;
302 keyMap["Right"] = SDLK_RIGHT;