Commit | Line | Data |
---|---|---|
87c9b12f PG |
1 | /* |
2 | * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. | |
3 | * | |
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. | |
8 | * | |
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. | |
13 | * | |
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/>. | |
16 | */ | |
17 | ||
18 | #include "reader.h" | |
19 | #include "../debug.h" | |
20 | ||
21 | #include <iostream> | |
22 | #include <fstream> | |
23 | #include <string> | |
24 | ||
338e728a PG |
25 | #include "keys.h" |
26 | ||
87c9b12f PG |
27 | using std::ifstream; |
28 | using std::string; | |
29 | ||
338e728a | 30 | typedef std::map<string, SDLKey, comparestrings> kMap; |
9d049dca | 31 | kMap keyMap; |
87c9b12f PG |
32 | |
33 | /// ***** Private Method Headers ***** | |
34 | ||
338e728a | 35 | void processLine(const string& str); |
07df2aea | 36 | bool extractLine(const string& str, string* name, string* value); |
338e728a | 37 | void createKeyMap(); |
87c9b12f PG |
38 | |
39 | /// ***** Private Variables ***** | |
40 | ||
b552aa3b | 41 | const char* configDir = "configs/"; |
338e728a | 42 | const char* testFile = "test2.cfg"; |
9d049dca | 43 | |
87c9b12f PG |
44 | /// ***** Public Methods ***** |
45 | ||
338e728a | 46 | void readConfigs() |
87c9b12f PG |
47 | { |
48 | char fileName[64]; | |
49 | ||
338e728a PG |
50 | createKeyMap(); |
51 | ||
87c9b12f | 52 | strcpy(fileName, configDir); |
7ffb6c2d | 53 | strcat(fileName, testFile); |
87c9b12f PG |
54 | |
55 | ifstream file(fileName); | |
56 | ||
57 | if( !file.is_open() ) | |
58 | { | |
59 | cerr << "Unable to open file " << fileName << "." << endl; | |
60 | exit(1); | |
61 | } | |
62 | ||
63 | while(true) | |
64 | { | |
65 | string line; | |
66 | getline(file, line); | |
67 | ||
68 | if(file.eof()) | |
69 | break; | |
70 | ||
338e728a | 71 | processLine(line); |
87c9b12f PG |
72 | } |
73 | ||
74 | file.close(); | |
75 | } | |
76 | ||
77 | /// ***** Private Methods ***** | |
78 | ||
338e728a | 79 | void processLine(const string& str) |
87c9b12f | 80 | { |
07df2aea PG |
81 | string name; |
82 | string value; | |
83 | ||
84 | bool extracted; | |
85 | ||
86 | extracted = extractLine(str, &name, &value); | |
87 | ||
88 | if(extracted) | |
89 | { | |
338e728a PG |
90 | SDLKey key = keyMap[value]; |
91 | ||
92 | if(0 != key) | |
93 | { | |
94 | *(key::sdlMap[name]) = key; | |
95 | } | |
07df2aea PG |
96 | cout << name << endl; |
97 | cout << value << endl; | |
338e728a | 98 | cout << key << endl; |
07df2aea PG |
99 | } |
100 | } | |
101 | ||
102 | /* | |
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. | |
105 | */ | |
106 | bool extractLine(const string& str, string* name, string* value) | |
107 | { | |
108 | /* | |
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 | |
115 | */ | |
116 | int state = 0; | |
117 | ||
118 | int char_pos = 0; | |
119 | ||
120 | int name_start; | |
121 | int name_end; | |
122 | ||
123 | int value_start; | |
124 | int value_end; | |
125 | ||
126 | const char* c_str = str.c_str(); | |
127 | ||
128 | while(true) | |
129 | { | |
130 | if(c_str[char_pos] == 0 && state != 4) | |
131 | return false; | |
132 | ||
133 | switch (state) | |
134 | { | |
135 | case 0: | |
136 | if(c_str[char_pos] != ' ' | |
137 | && c_str[char_pos] != '\t') | |
138 | { | |
139 | state++; | |
140 | name_start = char_pos; | |
141 | } | |
142 | break; | |
143 | case 1: | |
144 | if(c_str[char_pos] == ' ' | |
145 | || c_str[char_pos] == '\t' | |
146 | || c_str[char_pos] == '=') | |
147 | { | |
148 | state++; | |
149 | name_end = char_pos; | |
150 | } | |
9d049dca | 151 | |
07df2aea PG |
152 | if(c_str[char_pos] == '=') |
153 | char_pos--; // decrement to stay on this char | |
154 | break; | |
155 | case 2: | |
9d049dca | 156 | if(c_str[char_pos] != ' ' |
7ffb6c2d PG |
157 | && c_str[char_pos] != '\t' |
158 | && c_str[char_pos] != '=') | |
9d049dca PG |
159 | { |
160 | return false; | |
07df2aea | 161 | } |
7ffb6c2d PG |
162 | |
163 | if(c_str[char_pos] == '=') | |
164 | { | |
165 | state++; | |
166 | } | |
07df2aea PG |
167 | break; |
168 | case 3: | |
169 | if(c_str[char_pos] != ' ' | |
170 | && c_str[char_pos] != '\t') | |
171 | { | |
172 | state++; | |
173 | value_start = char_pos; | |
174 | } | |
175 | break; | |
176 | case 4: | |
177 | if(c_str[char_pos] == ' ' | |
178 | || c_str[char_pos] == '\t' | |
179 | || c_str[char_pos] == 0) | |
180 | { | |
181 | state++; | |
182 | value_end = char_pos; | |
183 | } | |
184 | break; | |
185 | } | |
186 | ||
187 | if(state == 5) | |
188 | break; | |
189 | ||
190 | char_pos++; | |
191 | } | |
192 | ||
193 | name->clear(); | |
194 | value->clear(); | |
195 | ||
338e728a PG |
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); | |
07df2aea PG |
198 | |
199 | return true; | |
87c9b12f | 200 | } |
9d049dca | 201 | |
9d049dca PG |
202 | void createKeyMap() |
203 | { | |
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; | |
209 | ||
210 | // add all the numbers | |
211 | for (int i = '0'; i <= '9'; i++) | |
212 | keyMap["" + (char)i] = (SDLKey)i; | |
213 | ||
338e728a PG |
214 | keyMap["k"] = (SDLKey)'k'; |
215 | ||
9d049dca PG |
216 | /* |
217 | // add the function keys | |
218 | int F1 = (int)Key.F1; | |
219 | for (int i = F1; i <= (int)Key.F15; i++) | |
220 | { | |
221 | keyMap.Add("F" + (i - F1 + 1), (Key)i); | |
222 | keyMap.Add("f" + (i - F1 + 1), (Key)i); | |
223 | } | |
224 | */ | |
225 | ||
338e728a PG |
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; | |
236 | ||
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; | |
247 | ||
248 | keyMap["Esc"] = SDLK_ESCAPE; | |
249 | keyMap["Escape"] = SDLK_ESCAPE; | |
250 | ||
251 | keyMap["Return"] = SDLK_RETURN; | |
252 | keyMap["Enter"] = SDLK_RETURN; | |
253 | ||
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; | |
260 | ||
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; | |
279 | ||
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; | |
285 | ||
286 | keyMap["Up"] = SDLK_UP; | |
287 | keyMap["Down"] = SDLK_DOWN; | |
288 | keyMap["Left"] = SDLK_LEFT; | |
289 | keyMap["Right"] = SDLK_RIGHT; | |
290 | ||
291 | cout << "done" << endl; | |
292 | cout << keyMap["k"] << endl; | |
9d049dca | 293 | } |