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;
205 bool getValue(
const std::string& section,
const std::string& tag,
206 char& value,
bool missing_ok =
false)
const;
227 template <
typename Rsp>
228 bool getValue(
const std::string& section,
const std::string& tag,
229 Rsp &rsp,
bool missing_ok =
false)
const
232 if (!
getValue(section, tag, str_val))
236 std::stringstream ssval(str_val);
243 if (ssval.fail() || !ssval.eof())
271 template <
template <
typename,
typename>
class Container,
273 bool getValue(
const std::string& section,
const std::string& tag,
274 Container<Value, std::allocator<Value> > &c,
275 bool missing_ok =
false)
const
278 if (!
getValue(section, tag, str_val))
287 std::stringstream ssval(str_val);
288 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
327 template <
template <
typename,
typename,
typename>
class Container,
329 bool getValue(
const std::string& section,
const std::string& tag,
330 Container<Key, std::less<Key>, std::allocator<Key> > &c,
331 bool missing_ok =
false)
const
334 if (!
getValue(section, tag, str_val))
343 std::stringstream ssval(str_val);
344 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
383 template <
template <
typename,
typename,
typename,
typename>
class Container,
384 class Key,
class T,
class Compare=std::less<Key>,
385 class Allocator=std::allocator<std::pair<const Key, T>>>
386 bool getValue(
const std::string& section,
const std::string& tag,
387 Container<Key, T, Compare, Allocator>& c,
388 char sep =
':',
bool missing_ok =
false)
const
391 if (!
getValue(section, tag, str_val))
400 std::stringstream ssval(str_val);
401 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
406 std::string::size_type seppos = entry.find(sep);
407 if (seppos == std::string::npos)
411 std::string keystr(entry.substr(0, seppos));
412 std::string valuestr(entry.substr(seppos+1));
415 if (!setValueFromString(key, keystr) ||
416 !setValueFromString(value, valuestr))
428 c.insert(std::pair<Key, T>(key, value));
454 template <
typename Rsp>
455 bool getValue(
const std::string& section,
const std::string& tag,
456 const Rsp& min,
const Rsp& max, Rsp &rsp,
457 bool missing_ok =
false)
const
460 if (!
getValue(section, tag, str_val))
464 std::stringstream ssval(str_val);
471 if (ssval.fail() || !ssval.eof() || (tmp < min) || (tmp > max))
495 template <
typename F=std::function<
void(const
char*)>>
497 const char* def, F func)
500 [=](
const std::string& str_val) ->
void
502 func(str_val.c_str());
522 template <
typename Rsp,
typename F=std::function<
void(const Rsp&)>>
524 const Rsp& def, F func)
526 Value& v = getValueP(section, tag, def);
528 [=](
const std::string& str_val) ->
void
530 std::stringstream ssval(str_val);
531 ssval.imbue(std::locale(ssval.getloc(),
new empty_ctype));
536 v.subs.back()(v.val);
555 template <
template <
typename,
typename>
class Container,
556 typename Rsp,
typename F=std::function<void(
const Rsp&)>>
558 const Container<Rsp, std::allocator<Rsp>>& def, F func)
560 Value& v = getValueP(section, tag, def);
562 [=](
const std::string& str_val) ->
void
564 std::stringstream ssval(str_val);
565 ssval.imbue(std::locale(ssval.getloc(),
new csv_whitespace));
566 Container<Rsp, std::allocator<Rsp>> c;
583 v.subs.back()(v.val);
616 void setValue(
const std::string& section,
const std::string& tag,
617 const std::string& value);
636 template <
typename Rsp>
637 void setValue(
const std::string& section,
const std::string& tag,
640 std::ostringstream ss;
664 template <
template <
typename,
typename>
class Container,
666 void setValue(
const std::string& section,
const std::string& tag,
667 const Container<Rsp, std::allocator<Rsp>>& c)
669 std::ostringstream ss;
670 bool first_val =
true;
671 for (
const auto& val : c)
692 sigc::signal<void(
const std::string&,
const std::string&)>
valueUpdated;
695 using Subscriber = std::function<void(
const std::string&)>;
699 std::vector<Subscriber> subs;
701 typedef std::map<std::string, Value> Values;
702 typedef std::map<std::string, Values> Sections;
705 static const std::ctype<char>::mask* empty_table()
707 static const auto table_size = std::ctype<char>::table_size;
708 static std::ctype<char>::mask v[table_size];
709 std::fill(&v[0], &v[table_size], 0);
713 struct empty_ctype : std::ctype<char>
715 static const mask* make_table(
void) {
return empty_table(); }
716 empty_ctype(std::size_t refs=0) : ctype(make_table(), false, refs) {}
719 struct csv_whitespace : std::ctype<char>
721 static const mask* make_table()
723 auto tbl = empty_table();
724 static std::vector<mask> v(tbl, tbl + table_size);
729 csv_whitespace(std::size_t refs=0) : ctype(make_table(), false, refs) {}
734 bool parseCfgFile(FILE *file);
735 char *trimSpaces(
char *line);
736 char *parseSection(
char *line);
737 char *parseDelimitedString(
char *str,
char begin_tok,
char end_tok);
738 bool parseValueLine(
char *line, std::string& tag, std::string& value);
739 char *parseValue(
char *value);
740 char *translateEscapedChars(
char *val);
743 bool setValueFromString(T& val,
const std::string &str)
const
745 std::istringstream ss(str);
746 ss >> std::noskipws >> val;
751 return !ss.fail() && ss.eof();
754 template <
typename T>
755 Value& getValueP(
const std::string& section,
const std::string& tag,
758 Values::iterator val_it = sections[section].find(tag);
759 if (val_it == sections[section].end())
764 return sections[section][tag];
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.
sigc::signal< void(const std::string &, const std::string &)> valueUpdated
A signal that is emitted when a config value is updated.
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.
bool getValue(const std::string §ion, const std::string &tag, char &value, bool missing_ok=false) const
Get the char 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*).
Namespace for the asynchronous programming classes.