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