comparison C++/Parser.cpp @ 187:600754c27c88

Update parser to style and remove useless stuff
author David Demelier <markand@malikania.fr>
date Tue, 26 Nov 2013 20:20:59 +0100
parents 2bcdee0fe8d4
children 1ffe6d4937b7
comparison
equal deleted inserted replaced
186:d4b8416e9ab1 187:600754c27c88
48 const std::string Section::findOption(const std::string &name) const 48 const std::string Section::findOption(const std::string &name) const
49 { 49 {
50 std::string ret; 50 std::string ret;
51 51
52 for (const Option &o : m_options) 52 for (const Option &o : m_options)
53 if (o.m_key == name) 53 if (o.m_key == name) {
54 {
55 ret = o.m_value; 54 ret = o.m_value;
56 break; 55 break;
57 } 56 }
58 57
59 return ret; 58 return ret;
62 template <> 61 template <>
63 bool Section::getValue(const std::string &name) const 62 bool Section::getValue(const std::string &name) const
64 { 63 {
65 bool result = false; 64 bool result = false;
66 65
67 if (hasOption(name)) 66 if (hasOption(name)) {
68 {
69 std::string value = findOption(name); 67 std::string value = findOption(name);
70 68
71 if (value == "yes" || value == "true"|| value == "1") 69 if (value == "yes" || value == "true"|| value == "1")
72 result = true; 70 result = true;
73 else if (value == "no" || value == "false" || value == "0") 71 else if (value == "no" || value == "false" || value == "0")
148 146
149 void Parser::readSection(int lineno, const std::string &line) 147 void Parser::readSection(int lineno, const std::string &line)
150 { 148 {
151 size_t end; 149 size_t end;
152 150
153 if ((end = line.find_first_of(']')) != std::string::npos) 151 if ((end = line.find_first_of(']')) != std::string::npos) {
154 { 152 if (end > 1) {
155 if (end > 1)
156 {
157 std::string name = line.substr(1, end - 1); 153 std::string name = line.substr(1, end - 1);
158 154
159 /* 155 /*
160 * Check if we can add a section, if redefinition is 156 * Check if we can add a section, if redefinition is
161 * disabled, we must disable the previous section so the 157 * disabled, we must disable the previous section so the
162 * further read options should not be enabled until 158 * further read options should not be enabled until
163 * a correct section is found again. 159 * a correct section is found again.
164 */ 160 */
165 if (hasSection(name) && (m_tuning & DisableRedefinition)) 161 if (hasSection(name) && (m_tuning & DisableRedefinition)) {
166 {
167 if (!(m_tuning & DisableVerbosity)) 162 if (!(m_tuning & DisableVerbosity))
168 log(lineno, name, "redefinition not allowed"); 163 log(lineno, name, "redefinition not allowed");
169 m_sections.back().m_allowed = false; 164 m_sections.back().m_allowed = false;
170 } 165 } else
171 else
172 addSection(name); 166 addSection(name);
173 } 167 } else if (!(m_tuning & DisableVerbosity)) {
174 else if (!(m_tuning & DisableVerbosity))
175 {
176 /* 168 /*
177 * Do not add options at this step because it will 169 * Do not add options at this step because it will
178 * corrupt the previous one. 170 * corrupt the previous one.
179 */ 171 */
180 m_sections.back().m_allowed = false; 172 m_sections.back().m_allowed = false;
188 size_t epos; 180 size_t epos;
189 std::string key, value; 181 std::string key, value;
190 Section &current = m_sections.back(); 182 Section &current = m_sections.back();
191 183
192 // Error on last section? 184 // Error on last section?
193 if (!current.m_allowed) 185 if (!current.m_allowed) {
194 {
195 /* 186 /*
196 * If it is the root section, this has been probably set by 187 * If it is the root section, this has been probably set by
197 * DisableRootSection flag, otherwise an error has occured 188 * DisableRootSection flag, otherwise an error has occured
198 * so no need to log. 189 * so no need to log.
199 */ 190 */
201 log(lineno, "", "option not allowed in that scope"); 192 log(lineno, "", "option not allowed in that scope");
202 193
203 return; 194 return;
204 } 195 }
205 196
206 if ((epos = line.find_first_of('=')) == std::string::npos) 197 if ((epos = line.find_first_of('=')) == std::string::npos) {
207 {
208 if (!(m_tuning & DisableVerbosity)) 198 if (!(m_tuning & DisableVerbosity))
209 log(lineno, current.m_name, "missing `=' keyword"); 199 log(lineno, current.m_name, "missing `=' keyword");
210 return; 200 return;
211 } 201 }
212 202
213 if (epos > 0) 203 if (epos > 0) {
214 {
215 size_t i, begin, last; 204 size_t i, begin, last;
216 char c; 205 char c;
217 206
218 key = line.substr(0, epos - 1); 207 key = line.substr(0, epos - 1);
219 value = line.substr(epos + 1); 208 value = line.substr(epos + 1);
228 continue; 217 continue;
229 value = value.substr(begin); 218 value = value.substr(begin);
230 219
231 c = value[0]; 220 c = value[0];
232 begin = 0; 221 begin = 0;
233 if (c == '\'' || c == '"') 222 if (c == '\'' || c == '"') {
234 {
235 for (last = begin = 1; value[last] != c && last < value.length(); ++last) 223 for (last = begin = 1; value[last] != c && last < value.length(); ++last)
236 continue; 224 continue;
237 if (value[last] != c && !(m_tuning & DisableVerbosity)) 225 if (value[last] != c && !(m_tuning & DisableVerbosity))
238 if (!(m_tuning & DisableVerbosity)) 226 if (!(m_tuning & DisableVerbosity))
239 log(lineno, current.m_name, "undisclosed std::string"); 227 log(lineno, current.m_name, "undisclosed std::string");
240 } 228 } else {
241 else
242 {
243 for (last = begin; !isspace(value[last]) && last < value.length(); ++last) 229 for (last = begin; !isspace(value[last]) && last < value.length(); ++last)
244 continue; 230 continue;
245 } 231 }
246 232
247 if (last - begin > 0) 233 if (last - begin > 0)
263 // Skip default spaces 249 // Skip default spaces
264 for (i = 0; isspace(line[i]) && i < line.length(); ++i) 250 for (i = 0; isspace(line[i]) && i < line.length(); ++i)
265 continue; 251 continue;
266 252
267 buffer = line.substr(i); 253 buffer = line.substr(i);
268 if (buffer.length() > 0) 254 if (buffer.length() > 0) {
269 { 255 if (buffer[0] != m_commentChar) {
270 if (buffer[0] != m_commentChar)
271 {
272 if (buffer[0] == '[') 256 if (buffer[0] == '[')
273 readSection(lineno, buffer); 257 readSection(lineno, buffer);
274 else 258 else
275 readOption(lineno, buffer); 259 readOption(lineno, buffer);
276 } 260 }
310 std::ifstream file; 294 std::ifstream file;
311 std::string line; 295 std::string line;
312 int lineno = 1; 296 int lineno = 1;
313 297
314 file.open(m_path.c_str()); 298 file.open(m_path.c_str());
315 if (!file.is_open()) 299 if (!file.is_open()) {
316 {
317 m_error = "could not open file " + m_path; // XXX: add a real error 300 m_error = "could not open file " + m_path; // XXX: add a real error
318 return false; 301 return false;
319 } 302 }
320 303
321 // Avoid use of C getline 304 // Avoid use of C getline
360 return s; 343 return s;
361 344
362 throw NotFoundException(name); 345 throw NotFoundException(name);
363 } 346 }
364 347
365 const Section &Parser::requireSection(const std::string &name) const
366 {
367 if (!hasSection(name))
368 throw NotFoundException(name);
369
370 return getSection(name);
371 }
372
373 void Parser::log(int number, const std::string &, const std::string &message) 348 void Parser::log(int number, const std::string &, const std::string &message)
374 { 349 {
375 std::cout << "line " << number << ": " << message << std::endl; 350 std::cout << "line " << number << ": " << message << std::endl;
376 } 351 }
377 352
378 void Parser::dump() 353 void Parser::dump()
379 { 354 {
380 for (auto s : m_sections) 355 for (auto s : m_sections) {
381 {
382 dumpSection(s); 356 dumpSection(s);
383 357
384 for (auto o : s.m_options) 358 for (auto o : s.m_options)
385 dumpOption(o); 359 dumpOption(o);
386 } 360 }