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