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 | ||
25 | using std::ifstream; | |
26 | using std::string; | |
27 | ||
9d049dca PG |
28 | typedef std::map<string,SDLKey> kMap; |
29 | kMap keyMap; | |
87c9b12f PG |
30 | |
31 | /// ***** Private Method Headers ***** | |
32 | ||
9d049dca | 33 | void processLine(const string& str, inputMap* map); |
07df2aea | 34 | bool extractLine(const string& str, string* name, string* value); |
87c9b12f PG |
35 | |
36 | /// ***** Private Variables ***** | |
37 | ||
b552aa3b PG |
38 | const char* configDir = "configs/"; |
39 | const char* testFile = "test.cfg"; | |
9d049dca | 40 | |
87c9b12f PG |
41 | /// ***** Public Methods ***** |
42 | ||
9d049dca | 43 | void readConfigs(inputMap* map) |
87c9b12f PG |
44 | { |
45 | char fileName[64]; | |
46 | ||
47 | strcpy(fileName, configDir); | |
7ffb6c2d | 48 | strcat(fileName, testFile); |
87c9b12f PG |
49 | |
50 | ifstream file(fileName); | |
51 | ||
52 | if( !file.is_open() ) | |
53 | { | |
54 | cerr << "Unable to open file " << fileName << "." << endl; | |
55 | exit(1); | |
56 | } | |
57 | ||
58 | while(true) | |
59 | { | |
60 | string line; | |
61 | getline(file, line); | |
62 | ||
63 | if(file.eof()) | |
64 | break; | |
65 | ||
66 | processLine(line, map); | |
67 | } | |
68 | ||
69 | file.close(); | |
70 | } | |
71 | ||
72 | /// ***** Private Methods ***** | |
73 | ||
9d049dca | 74 | void processLine(const string& str, inputMap* map) |
87c9b12f | 75 | { |
07df2aea PG |
76 | string name; |
77 | string value; | |
78 | ||
79 | bool extracted; | |
80 | ||
81 | extracted = extractLine(str, &name, &value); | |
82 | ||
83 | if(extracted) | |
84 | { | |
9d049dca | 85 | //map->insert(name, value); |
07df2aea PG |
86 | cout << name << endl; |
87 | cout << value << endl; | |
88 | } | |
89 | } | |
90 | ||
91 | /* | |
92 | * Return true iff the string str, had a name and value extracted and saved in | |
93 | * name and value. Otherwise return false and leave name and value alone. | |
94 | */ | |
95 | bool extractLine(const string& str, string* name, string* value) | |
96 | { | |
97 | /* | |
98 | * state := 0 if eating leading name whitespace | |
99 | * := 1 if saving name | |
100 | * := 2 if eating trailing name whitespace | |
101 | * := 3 if eating leading value whitespace | |
102 | * := 4 if saving value | |
103 | * := 5 if all is done and well | |
104 | */ | |
105 | int state = 0; | |
106 | ||
107 | int char_pos = 0; | |
108 | ||
109 | int name_start; | |
110 | int name_end; | |
111 | ||
112 | int value_start; | |
113 | int value_end; | |
114 | ||
115 | const char* c_str = str.c_str(); | |
116 | ||
117 | while(true) | |
118 | { | |
119 | if(c_str[char_pos] == 0 && state != 4) | |
120 | return false; | |
121 | ||
122 | switch (state) | |
123 | { | |
124 | case 0: | |
125 | if(c_str[char_pos] != ' ' | |
126 | && c_str[char_pos] != '\t') | |
127 | { | |
128 | state++; | |
129 | name_start = char_pos; | |
130 | } | |
131 | break; | |
132 | case 1: | |
133 | if(c_str[char_pos] == ' ' | |
134 | || c_str[char_pos] == '\t' | |
135 | || c_str[char_pos] == '=') | |
136 | { | |
137 | state++; | |
138 | name_end = char_pos; | |
139 | } | |
9d049dca | 140 | |
07df2aea PG |
141 | if(c_str[char_pos] == '=') |
142 | char_pos--; // decrement to stay on this char | |
143 | break; | |
144 | case 2: | |
9d049dca | 145 | if(c_str[char_pos] != ' ' |
7ffb6c2d PG |
146 | && c_str[char_pos] != '\t' |
147 | && c_str[char_pos] != '=') | |
9d049dca PG |
148 | { |
149 | return false; | |
07df2aea | 150 | } |
7ffb6c2d PG |
151 | |
152 | if(c_str[char_pos] == '=') | |
153 | { | |
154 | state++; | |
155 | } | |
07df2aea PG |
156 | break; |
157 | case 3: | |
158 | if(c_str[char_pos] != ' ' | |
159 | && c_str[char_pos] != '\t') | |
160 | { | |
161 | state++; | |
162 | value_start = char_pos; | |
163 | } | |
164 | break; | |
165 | case 4: | |
166 | if(c_str[char_pos] == ' ' | |
167 | || c_str[char_pos] == '\t' | |
168 | || c_str[char_pos] == 0) | |
169 | { | |
170 | state++; | |
171 | value_end = char_pos; | |
172 | } | |
173 | break; | |
174 | } | |
175 | ||
176 | if(state == 5) | |
177 | break; | |
178 | ||
179 | char_pos++; | |
180 | } | |
181 | ||
182 | name->clear(); | |
183 | value->clear(); | |
184 | ||
185 | name->replace(0, 0, c_str, name_start, name_end-name_start); | |
186 | value->replace(0, 0, c_str, value_start, value_end-value_start); | |
187 | ||
188 | return true; | |
87c9b12f | 189 | } |
9d049dca PG |
190 | |
191 | bool extractKey(const string& str, Uint8* key) | |
192 | { | |
193 | // if(str.length() != 1) | |
194 | // return false; | |
195 | ||
196 | return false; | |
197 | } | |
198 | ||
199 | void createKeyMap() | |
200 | { | |
201 | // add all the letters | |
202 | for (int i = 'A'; i <= 'Z'; i++) // uppercase | |
203 | keyMap["" + (char)i] = (SDLKey)i; | |
204 | for (int i = 'a'; i <= 'z'; i++) // lowercase | |
205 | keyMap["" + (char)i] = (SDLKey)i; | |
206 | ||
207 | // add all the numbers | |
208 | for (int i = '0'; i <= '9'; i++) | |
209 | keyMap["" + (char)i] = (SDLKey)i; | |
210 | ||
211 | /* | |
212 | // add the function keys | |
213 | int F1 = (int)Key.F1; | |
214 | for (int i = F1; i <= (int)Key.F15; i++) | |
215 | { | |
216 | keyMap.Add("F" + (i - F1 + 1), (Key)i); | |
217 | keyMap.Add("f" + (i - F1 + 1), (Key)i); | |
218 | } | |
219 | */ | |
220 | ||
221 | keyMap["LCtrl"] = SDLK_LCTRL; | |
222 | keyMap["LeftControl"] = SDLK_LCTRL; | |
223 | keyMap["LAlt"] = SDLK_LALT; | |
224 | keyMap["LeftAlt"] = SDLK_LALT; | |
225 | keyMap["LShift"] = SDLK_LSHIFT; | |
226 | keyMap["LeftShift"] = SDLK_LSHIFT; | |
227 | keyMap["LWin"] = SDLK_LSUPER; | |
228 | keyMap["LeftWindows"] = SDLK_LSUPER; | |
229 | keyMap["LeftMeta"] = SDLK_LMETA; | |
230 | keyMap["LMeta"] = SDLK_LMETA; | |
231 | ||
232 | keyMap["RCtrl"] = SDLK_RCTRL; | |
233 | keyMap["RightControl"] = SDLK_RCTRL; | |
234 | keyMap["RAlt"] = SDLK_RALT; | |
235 | keyMap["RightAlt"] = SDLK_RALT; | |
236 | keyMap["RShift"] = SDLK_RSHIFT; | |
237 | keyMap["RightShift"] = SDLK_RSHIFT; | |
238 | keyMap["RWin"] = SDLK_RSUPER; | |
239 | keyMap["RightWindows"] = SDLK_RSUPER; | |
240 | keyMap["RightMeta"] = SDLK_RMETA; | |
241 | keyMap["RMeta"] = SDLK_RMETA; | |
242 | ||
243 | keyMap["Esc"] = SDLK_ESCAPE; | |
244 | keyMap["Escape"] = SDLK_ESCAPE; | |
245 | ||
246 | keyMap["Return"] = SDLK_RETURN; | |
247 | keyMap["Enter"] = SDLK_RETURN; | |
248 | ||
249 | keyMap["Insert"] = SDLK_INSERT; | |
250 | keyMap["Home"] = SDLK_HOME; | |
251 | keyMap["Delete"] = SDLK_DELETE; | |
252 | keyMap["End"] = SDLK_END; | |
253 | keyMap["PageUp"] = SDLK_PAGEUP; | |
254 | keyMap["PageDown"] = SDLK_PAGEDOWN; | |
255 | ||
256 | keyMap["Minus"] = SDLK_MINUS; | |
257 | keyMap["Equal"] = SDLK_EQUALS; | |
258 | keyMap["Equals"] = SDLK_EQUALS; | |
259 | keyMap["LeftBracket"] = SDLK_LEFTBRACKET; | |
260 | keyMap["LBracket"] = SDLK_LEFTBRACKET; | |
261 | keyMap["RightBracket"] = SDLK_RIGHTBRACKET; | |
262 | keyMap["RBracket"] = SDLK_RIGHTBRACKET; | |
263 | keyMap["Backslash"] = SDLK_BACKSLASH; | |
264 | keyMap["Slash"] = SDLK_SLASH; | |
265 | keyMap["Semicolon"] = SDLK_SEMICOLON; | |
266 | keyMap["Semi"] = SDLK_SEMICOLON; | |
267 | keyMap["Quote"] = SDLK_QUOTE; | |
268 | keyMap["Comma"] = SDLK_COMMA; | |
269 | keyMap["Period"] = SDLK_PERIOD; | |
270 | keyMap["Space"] = SDLK_SPACE; | |
271 | keyMap["BSpace"] = SDLK_BACKSPACE; | |
272 | keyMap["Backspace"] = SDLK_BACKSPACE; | |
273 | keyMap["BackSpace"] = SDLK_BACKSPACE; | |
274 | ||
275 | keyMap["Tab"] = SDLK_TAB; | |
276 | keyMap["BackQuote"] = SDLK_BACKQUOTE; | |
277 | keyMap["BQuote"] = SDLK_BACKQUOTE; | |
278 | keyMap["CapsLock"] = SDLK_CAPSLOCK; | |
279 | keyMap["Caps"] = SDLK_CAPSLOCK; | |
280 | ||
281 | keyMap["Up"] = SDLK_UP; | |
282 | keyMap["Down"] = SDLK_DOWN; | |
283 | keyMap["Left"] = SDLK_LEFT; | |
284 | keyMap["Right"] = SDLK_RIGHT; | |
285 | } |