38#ifndef ASYNC_CONFIG_INCLUDED
39#define ASYNC_CONFIG_INCLUDED
49#include <sigc++/sigc++.h>
160 bool open(
const std::string& name);
174 const std::string &
getValue(
const std::string& section,
175 const std::string& tag)
const;
189 bool getValue(
const std::string& section,
const std::string& tag,
190 std::string& value,
bool missing_ok =
false)
const;
212 template <
typename Rsp>
213 bool getValue(
const std::string& section,
const std::string& tag,
214 Rsp &rsp,
bool missing_ok =
false)
const
217 if (!
getValue(section, tag, str_val))
221 std::stringstream ssval(str_val);
228 if (ssval.fail() || !ssval.eof())
256 template <
template <
typename,
typename>
class Container,
258 bool getValue(
const std::string& section,
const std::string& tag,
259 Container<Value, std::allocator<Value> > &c,
260 bool missing_ok =
false)
const
263 if (!
getValue(section, tag, str_val))
272 std::stringstream ssval(str_val);
273 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
312 template <
template <
typename,
typename,
typename>
class Container,
314 bool getValue(
const std::string& section,
const std::string& tag,
315 Container<Key, std::less<Key>, std::allocator<Key> > &c,
316 bool missing_ok =
false)
const
319 if (!
getValue(section, tag, str_val))
328 std::stringstream ssval(str_val);
329 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
368 template <
template <
typename,
typename,
typename,
typename>
class Container,
369 class Key,
class T,
class Compare=std::less<Key>,
370 class Allocator=std::allocator<std::pair<const Key, T>>>
371 bool getValue(
const std::string& section,
const std::string& tag,
372 Container<Key, T, Compare, Allocator>& c,
373 char sep =
':',
bool missing_ok =
false)
const
376 if (!
getValue(section, tag, str_val))
385 std::stringstream ssval(str_val);
386 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
391 std::string::size_type seppos = entry.find(sep);
392 if (seppos == std::string::npos)
396 std::string keystr(entry.substr(0, seppos));
397 std::string valuestr(entry.substr(seppos+1));
400 if (!setValueFromString(key, keystr) ||
401 !setValueFromString(value, valuestr))
413 c.insert(std::pair<Key, T>(key, value));
439 template <
typename Rsp>
440 bool getValue(
const std::string& section,
const std::string& tag,
441 const Rsp& min,
const Rsp& max, Rsp &rsp,
442 bool missing_ok =
false)
const
445 if (!
getValue(section, tag, str_val))
449 std::stringstream ssval(str_val);
456 if (ssval.fail() || !ssval.eof() || (tmp < min) || (tmp > max))
480 template <
typename F=std::function<
void(const
char*)>>
482 const char* def, F func)
485 [=](
const std::string& str_val) ->
void
487 func(str_val.c_str());
507 template <
typename Rsp,
typename F=std::function<
void(const Rsp&)>>
509 const Rsp& def, F func)
511 Value& v = getValueP(section, tag, def);
513 [=](
const std::string& str_val) ->
void
515 std::stringstream ssval(str_val);
516 ssval.imbue(std::locale(ssval.getloc(),
new empty_ctype));
521 v.subs.back()(v.val);
540 template <
template <
typename,
typename>
class Container,
541 typename Rsp,
typename F=std::function<void(
const Rsp&)>>
543 const Container<Rsp, std::allocator<Rsp>>& def, F func)
545 Value& v = getValueP(section, tag, def);
547 [=](
const std::string& str_val) ->
void
549 std::stringstream ssval(str_val);
550 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
551 Container<Rsp, std::allocator<Rsp>> c;
568 v.subs.back()(v.val);
601 void setValue(
const std::string& section,
const std::string& tag,
602 const std::string& value);
621 template <
typename Rsp>
622 void setValue(
const std::string& section,
const std::string& tag,
625 std::ostringstream ss;
649 template <
template <
typename,
typename>
class Container,
651 void setValue(
const std::string& section,
const std::string& tag,
652 const Container<Rsp, std::allocator<Rsp>>& c)
654 std::ostringstream ss;
655 bool first_val =
true;
656 for (
const auto& val : c)
677 sigc::signal<void, const std::string&, const std::string&>
valueUpdated;
680 using Subscriber = std::function<void(
const std::string&)>;
684 std::vector<Subscriber> subs;
686 typedef std::map<std::string, Value> Values;
687 typedef std::map<std::string, Values> Sections;
690 static const std::ctype<char>::mask* empty_table()
692 static const auto table_size = std::ctype<char>::table_size;
693 static std::ctype<char>::mask v[table_size];
694 std::fill(&v[0], &v[table_size], 0);
698 struct empty_ctype : std::ctype<char>
700 static const mask* make_table(
void) {
return empty_table(); }
701 empty_ctype(std::size_t refs=0) : ctype(make_table(), false, refs) {}
704 struct csv_whitespace : std::ctype<char>
706 static const mask* make_table()
708 auto tbl = empty_table();
709 static std::vector<mask> v(tbl, tbl + table_size);
714 csv_whitespace(std::size_t refs=0) : ctype(make_table(), false, refs) {}
719 bool parseCfgFile(FILE *file);
720 char *trimSpaces(
char *line);
721 char *parseSection(
char *line);
722 char *parseDelimitedString(
char *str,
char begin_tok,
char end_tok);
723 bool parseValueLine(
char *line, std::string& tag, std::string& value);
724 char *parseValue(
char *value);
725 char *translateEscapedChars(
char *val);
728 bool setValueFromString(T& val,
const std::string &str)
const
730 std::istringstream ss(str);
731 ss >> std::noskipws >> val;
736 return !ss.fail() && ss.eof();
739 template <
typename T>
740 Value& getValueP(
const std::string& section,
const std::string& tag,
743 Values::iterator val_it = sections[section].find(tag);
744 if (val_it == sections[section].end())
749 return sections[section][tag];
A class for reading INI-formatted configuration files.
bool getValue(const std::string §ion, 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 §ion)
Return the name of all the tags in the given section.
void setValue(const std::string §ion, const std::string &tag, const Container< Rsp, std::allocator< Rsp > > &c)
Set the value of a configuration variable (sequence container)
void subscribeValue(const std::string §ion, const std::string &tag, const Rsp &def, F func)
Subscribe to the given configuration variable.
bool getValue(const std::string §ion, 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 §ion, const std::string &tag, Rsp &rsp, bool missing_ok=false) const
Get the value of the given configuration variable.
void subscribeValue(const std::string §ion, const std::string &tag, const Container< Rsp, std::allocator< Rsp > > &def, F func)
Subscribe to the given configuration variable (sequence)
bool getValue(const std::string §ion, 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 open(const std::string &name)
Open the given config file.
bool getValue(const std::string §ion, 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.
const std::string & getValue(const std::string §ion, const std::string &tag) const
Return the string value of the given configuration variable.
void setValue(const std::string §ion, const std::string &tag, const Rsp &value)
Set the value of a configuration variable (generic type)
bool getValue(const std::string §ion, const std::string &tag, std::string &value, bool missing_ok=false) const
Get the string value of the given configuration variable.
std::list< std::string > listSections(void)
Return the name of all configuration sections.
void setValue(const std::string §ion, const std::string &tag, const std::string &value)
Set the value of a configuration variable.
void subscribeValue(const std::string §ion, const std::string &tag, const char *def, F func)
Subscribe to the given configuration variable (char*)
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.