2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
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.
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.
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/>.
28 char* configDir = "../configs/";
30 /// ***** Private Method Headers *****
32 void processLine(const string& str, keyMap* map);
33 bool extractLine(const string& str, string* name, string* value);
35 /// ***** Private Variables *****
37 /// ***** Public Methods *****
39 void readConfigs(keyMap* map)
43 strcpy(fileName, configDir);
44 strcat(fileName, "test.cfg");
46 ifstream file(fileName);
50 cerr << "Unable to open file " << fileName << "." << endl;
62 processLine(line, map);
68 /// ***** Private Methods *****
70 void processLine(const string& str, keyMap* map)
77 extracted = extractLine(str, &name, &value);
82 cout << value << endl;
87 * Return true iff the string str, had a name and value extracted and saved in
88 * name and value. Otherwise return false and leave name and value alone.
90 bool extractLine(const string& str, string* name, string* value)
93 * state := 0 if eating leading name whitespace
95 * := 2 if eating trailing name whitespace
96 * := 3 if eating leading value whitespace
97 * := 4 if saving value
98 * := 5 if all is done and well
110 const char* c_str = str.c_str();
114 if(c_str[char_pos] == 0 && state != 4)
120 if(c_str[char_pos] != ' '
121 && c_str[char_pos] != '\t')
124 name_start = char_pos;
128 if(c_str[char_pos] == ' '
129 || c_str[char_pos] == '\t'
130 || c_str[char_pos] == '=')
135 if(c_str[char_pos] == '=')
136 char_pos--; // decrement to stay on this char
139 if(c_str[char_pos] == '=')
145 if(c_str[char_pos] != ' '
146 && c_str[char_pos] != '\t')
149 value_start = char_pos;
153 if(c_str[char_pos] == ' '
154 || c_str[char_pos] == '\t'
155 || c_str[char_pos] == 0)
158 value_end = char_pos;
172 name->replace(0, 0, c_str, name_start, name_end-name_start);
173 value->replace(0, 0, c_str, value_start, value_end-value_start);