Async 1.7.0
AsyncConfig.h
Go to the documentation of this file.
1
32
36
37
38#ifndef ASYNC_CONFIG_INCLUDED
39#define ASYNC_CONFIG_INCLUDED
40
41
42/****************************************************************************
43 *
44 * System Includes
45 *
46 ****************************************************************************/
47
48#include <stdio.h>
49#include <sigc++/sigc++.h>
50
51#include <string>
52#include <map>
53#include <list>
54#include <memory>
55#include <sstream>
56#include <locale>
57#include <vector>
58
59
60/****************************************************************************
61 *
62 * Project Includes
63 *
64 ****************************************************************************/
65
66
67
68/****************************************************************************
69 *
70 * Local Includes
71 *
72 ****************************************************************************/
73
74
75
76/****************************************************************************
77 *
78 * Forward declarations
79 *
80 ****************************************************************************/
81
82
83
84/****************************************************************************
85 *
86 * Namespace
87 *
88 ****************************************************************************/
89
90namespace Async
91{
92
93
94/****************************************************************************
95 *
96 * Forward declarations of classes inside of the declared namespace
97 *
98 ****************************************************************************/
99
100
101
102/****************************************************************************
103 *
104 * Defines & typedefs
105 *
106 ****************************************************************************/
107
108
109
110/****************************************************************************
111 *
112 * Exported Global Variables
113 *
114 ****************************************************************************/
115
116
117
118/****************************************************************************
119 *
120 * Class definitions
121 *
122 ****************************************************************************/
123
138{
139 public:
143 Config(void) {}
144
148 ~Config(void);
149
159 bool open(const std::string& name);
160
173 const std::string &getValue(const std::string& section,
174 const std::string& tag) const;
175
188 bool getValue(const std::string& section, const std::string& tag,
189 std::string& value) const;
190
211 template <typename Rsp>
212 bool getValue(const std::string& section, const std::string& tag,
213 Rsp &rsp, bool missing_ok = false) const
214 {
215 std::string str_val;
216 if (!getValue(section, tag, str_val))
217 {
218 return missing_ok;
219 }
220 std::stringstream ssval(str_val);
221 Rsp tmp;
222 ssval >> tmp;
223 if(!ssval.eof())
224 {
225 ssval >> std::ws;
226 }
227 if (ssval.fail() || !ssval.eof())
228 {
229 return false;
230 }
231 rsp = tmp;
232 return true;
233 } /* Config::getValue */
234
255 template <template <typename, typename> class Container,
256 typename Value>
257 bool getValue(const std::string& section, const std::string& tag,
258 Container<Value, std::allocator<Value> > &c,
259 bool missing_ok = false) const
260 {
261 std::string str_val;
262 if (!getValue(section, tag, str_val))
263 {
264 return missing_ok;
265 }
266 if (str_val.empty())
267 {
268 c.clear();
269 return true;
270 }
271 std::stringstream ssval(str_val);
272 ssval.imbue(std::locale(ssval.getloc(), new csv_whitespace));
273 while (!ssval.eof())
274 {
275 Value tmp;
276 ssval >> tmp;
277 if(!ssval.eof())
278 {
279 ssval >> std::ws;
280 }
281 if (ssval.fail())
282 {
283 return false;
284 }
285 c.push_back(tmp);
286 }
287 return true;
288 } /* Config::getValue */
289
311 template <template <typename, typename, typename> class Container,
312 typename Key>
313 bool getValue(const std::string& section, const std::string& tag,
314 Container<Key, std::less<Key>, std::allocator<Key> > &c,
315 bool missing_ok = false) const
316 {
317 std::string str_val;
318 if (!getValue(section, tag, str_val))
319 {
320 return missing_ok;
321 }
322 if (str_val.empty())
323 {
324 c.clear();
325 return true;
326 }
327 std::stringstream ssval(str_val);
328 ssval.imbue(std::locale(ssval.getloc(), new csv_whitespace));
329 while (!ssval.eof())
330 {
331 Key tmp;
332 ssval >> tmp;
333 if(!ssval.eof())
334 {
335 ssval >> std::ws;
336 }
337 if (ssval.fail())
338 {
339 return false;
340 }
341 c.insert(tmp);
342 }
343 return true;
344 } /* Config::getValue */
345
367 template <template <typename, typename, typename, typename> class Container,
368 class Key, class T, class Compare=std::less<Key>,
369 class Allocator=std::allocator<std::pair<const Key, T>>>
370 bool getValue(const std::string& section, const std::string& tag,
371 Container<Key, T, Compare, Allocator>& c,
372 char sep = ':', bool missing_ok = false) const
373 {
374 std::string str_val;
375 if (!getValue(section, tag, str_val))
376 {
377 return missing_ok;
378 }
379 if (str_val.empty())
380 {
381 c.clear();
382 return true;
383 }
384 std::stringstream ssval(str_val);
385 ssval.imbue(std::locale(ssval.getloc(), new csv_whitespace));
386 while (!ssval.eof())
387 {
388 std::string entry;
389 ssval >> entry;
390 std::string::size_type seppos = entry.find(sep);
391 if (seppos == std::string::npos)
392 {
393 return false;
394 }
395 std::string keystr(entry.substr(0, seppos));
396 std::string valuestr(entry.substr(seppos+1));
397 Key key;
398 T value;
399 if (!setValueFromString(key, keystr) ||
400 !setValueFromString(value, valuestr))
401 {
402 return false;
403 }
404 if(!ssval.eof())
405 {
406 ssval >> std::ws;
407 }
408 if (ssval.fail())
409 {
410 return false;
411 }
412 c.insert(std::pair<Key, T>(key, value));
413 }
414 return true;
415 } /* Config::getValue */
416
438 template <typename Rsp>
439 bool getValue(const std::string& section, const std::string& tag,
440 const Rsp& min, const Rsp& max, Rsp &rsp,
441 bool missing_ok = false) const
442 {
443 std::string str_val;
444 if (!getValue(section, tag, str_val))
445 {
446 return missing_ok;
447 }
448 std::stringstream ssval(str_val);
449 Rsp tmp;
450 ssval >> tmp;
451 if(!ssval.eof())
452 {
453 ssval >> std::ws;
454 }
455 if (ssval.fail() || !ssval.eof() || (tmp < min) || (tmp > max))
456 {
457 return false;
458 }
459 rsp = tmp;
460 return true;
461 } /* Config::getValue */
462
467 std::list<std::string> listSections(void);
468
475 std::list<std::string> listSection(const std::string& section);
476
493 void setValue(const std::string& section, const std::string& tag,
494 const std::string& value);
495
513 template <typename Rsp>
514 void setValue(const std::string& section, const std::string& tag,
515 const Rsp& value)
516 {
517 std::ostringstream ss;
518 ss << value;
519 setValue(section, tag, ss.str());
520 }
521
531 sigc::signal<void, const std::string&, const std::string&> valueUpdated;
532
533 private:
534 typedef std::map<std::string, std::string> Values;
535 typedef std::map<std::string, Values> Sections;
536 struct csv_whitespace : std::ctype<char>
537 {
538 static const mask* make_table(void)
539 {
540 // Make a copy of the "C" locale table
541 static std::vector<mask> v(classic_table(),
542 classic_table() + table_size);
543 v[','] |= space; // comma will be classified as whitespace
544 return &v[0];
545 }
546 csv_whitespace(std::size_t refs=0)
547 : std::ctype<char>(make_table(), false, refs) {}
548 };
549
550 Sections sections;
551
552 //Config(const Config&);
553 //Config& operator=(const Config&);
554 bool parseCfgFile(FILE *file);
555 char *trimSpaces(char *line);
556 char *parseSection(char *line);
557 char *parseDelimitedString(char *str, char begin_tok, char end_tok);
558 bool parseValueLine(char *line, std::string& tag, std::string& value);
559 char *parseValue(char *value);
560 char *translateEscapedChars(char *val);
561
562 template <class T>
563 bool setValueFromString(T& val, const std::string &str) const
564 {
565 std::istringstream ss(str);
566 ss >> std::noskipws >> val;
567 if(!ss.eof())
568 {
569 ss >> std::ws;
570 }
571 return !ss.fail() && ss.eof();
572 }
573
574}; /* class Config */
575
576
577} /* namespace */
578
579#endif /* ASYNC_CONFIG_INCLUDED */
580
581
582
583/*
584 * This file has not been truncated
585 */
586
bool getValue(const std::string &section, const std::string &tag, const Rsp &min, const Rsp &max, Rsp &rsp, bool missing_ok=false) const
Get a range checked variable value.
Config(void)
Default constuctor.
std::list< std::string > listSection(const std::string &section)
Return the name of all the tags in the given section.
bool getValue(const std::string &section, const std::string &tag, Container< Key, std::less< Key >, std::allocator< Key > > &c, bool missing_ok=false) const
Get the value of the given config variable into keyed container.
bool getValue(const std::string &section, const std::string &tag, Rsp &rsp, bool missing_ok=false) const
Get the value of the given configuration variable.
bool getValue(const std::string &section, const std::string &tag, Container< Key, T, Compare, Allocator > &c, char sep=':', bool missing_ok=false) const
Get value of given config variable into associative container.
bool getValue(const std::string &section, const std::string &tag, std::string &value) const
Get the string value of the given configuration variable.
bool open(const std::string &name)
Open the given config file.
bool getValue(const std::string &section, const std::string &tag, Container< Value, std::allocator< Value > > &c, bool missing_ok=false) const
Get the value of the given config variable into container.
~Config(void)
Destructor.
const std::string & getValue(const std::string &section, const std::string &tag) const
Return the string value of the given configuration variable.
void setValue(const std::string &section, const std::string &tag, const Rsp &value)
Set the value of a configuration variable (generic type)
std::list< std::string > listSections(void)
Return the name of all configuration sections.
void setValue(const std::string &section, const std::string &tag, const std::string &value)
Set the value of a configuration variable.
sigc::signal< void, const std::string &, const std::string & > valueUpdated
A signal that is emitted when a config value is updated.
Namespace for the asynchronous programming classes.