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, comparestrings> 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 = "test2.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);
204 // add all the letters
205 for (int i = 'A'; i <= 'Z'; i++) // uppercase
206 keyMap["" + (char)i] = (SDLKey)i;
207 for (int i = 'a'; i <= 'z'; i++) // lowercase
208 keyMap["" + (char)i] = (SDLKey)i;
210 // add all the numbers
211 for (int i = '0'; i <= '9'; i++)
212 keyMap["" + (char)i] = (SDLKey)i;
214 keyMap["k"] = (SDLKey)'k';
217 // add the function keys
218 int F1 = (int)Key.F1;
219 for (int i = F1; i <= (int)Key.F15; i++)
221 keyMap.Add("F" + (i - F1 + 1), (Key)i);
222 keyMap.Add("f" + (i - F1 + 1), (Key)i);
226 keyMap["LCtrl"] = SDLK_LCTRL;
227 keyMap["LeftControl"] = SDLK_LCTRL;
228 keyMap["LAlt"] = SDLK_LALT;
229 keyMap["LeftAlt"] = SDLK_LALT;
230 keyMap["LShift"] = SDLK_LSHIFT;
231 keyMap["LeftShift"] = SDLK_LSHIFT;
232 keyMap["LWin"] = SDLK_LSUPER;
233 keyMap["LeftWindows"] = SDLK_LSUPER;
234 keyMap["LeftMeta"] = SDLK_LMETA;
235 keyMap["LMeta"] = SDLK_LMETA;
237 keyMap["RCtrl"] = SDLK_RCTRL;
238 keyMap["RightControl"] = SDLK_RCTRL;
239 keyMap["RAlt"] = SDLK_RALT;
240 keyMap["RightAlt"] = SDLK_RALT;
241 keyMap["RShift"] = SDLK_RSHIFT;
242 keyMap["RightShift"] = SDLK_RSHIFT;
243 keyMap["RWin"] = SDLK_RSUPER;
244 keyMap["RightWindows"] = SDLK_RSUPER;
245 keyMap["RightMeta"] = SDLK_RMETA;
246 keyMap["RMeta"] = SDLK_RMETA;
248 keyMap["Esc"] = SDLK_ESCAPE;
249 keyMap["Escape"] = SDLK_ESCAPE;
251 keyMap["Return"] = SDLK_RETURN;
252 keyMap["Enter"] = SDLK_RETURN;
254 keyMap["Insert"] = SDLK_INSERT;
255 keyMap["Home"] = SDLK_HOME;
256 keyMap["Delete"] = SDLK_DELETE;
257 keyMap["End"] = SDLK_END;
258 keyMap["PageUp"] = SDLK_PAGEUP;
259 keyMap["PageDown"] = SDLK_PAGEDOWN;
261 keyMap["Minus"] = SDLK_MINUS;
262 keyMap["Equal"] = SDLK_EQUALS;
263 keyMap["Equals"] = SDLK_EQUALS;
264 keyMap["LeftBracket"] = SDLK_LEFTBRACKET;
265 keyMap["LBracket"] = SDLK_LEFTBRACKET;
266 keyMap["RightBracket"] = SDLK_RIGHTBRACKET;
267 keyMap["RBracket"] = SDLK_RIGHTBRACKET;
268 keyMap["Backslash"] = SDLK_BACKSLASH;
269 keyMap["Slash"] = SDLK_SLASH;
270 keyMap["Semicolon"] = SDLK_SEMICOLON;
271 keyMap["Semi"] = SDLK_SEMICOLON;
272 keyMap["Quote"] = SDLK_QUOTE;
273 keyMap["Comma"] = SDLK_COMMA;
274 keyMap["Period"] = SDLK_PERIOD;
275 keyMap["Space"] = SDLK_SPACE;
276 keyMap["BSpace"] = SDLK_BACKSPACE;
277 keyMap["Backspace"] = SDLK_BACKSPACE;
278 keyMap["BackSpace"] = SDLK_BACKSPACE;
280 keyMap["Tab"] = SDLK_TAB;
281 keyMap["BackQuote"] = SDLK_BACKQUOTE;
282 keyMap["BQuote"] = SDLK_BACKQUOTE;
283 keyMap["CapsLock"] = SDLK_CAPSLOCK;
284 keyMap["Caps"] = SDLK_CAPSLOCK;
286 keyMap["Up"] = SDLK_UP;
287 keyMap["Down"] = SDLK_DOWN;
288 keyMap["Left"] = SDLK_LEFT;
289 keyMap["Right"] = SDLK_RIGHT;
291 cout << "done" << endl;
292 cout << keyMap["k"] << endl;