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