33 #define _INIPARSE_H_ 1 41 #include <unordered_map> 49 #define FROM_STRING(T) void from_string(const string &, T *); 54 FROM_STRING(
long long)
55 FROM_STRING(
unsigned char)
56 FROM_STRING(
unsigned short)
57 FROM_STRING(
unsigned int)
58 FROM_STRING(
unsigned long)
59 FROM_STRING(
unsigned long long)
62 FROM_STRING(
long double)
68 mutable bool error_ {
false};
80 std::ostream &
warn()
const {
81 return std::cerr << file_ <<
':' << lineno_ <<
": ";
84 std::ostream &
fail()
const { error_ =
true;
return warn(); }
86 bool error()
const {
return error_; }
89 std::vector<string>
argv()
const;
92 template<
typename T>
void convert(T *rp)
const { from_string(value_, rp); }
97 using cb_t = std::function<void(const IniLine &li)>;
98 std::unordered_map<string, cb_t> cbs_;
105 if (!cbs_.emplace(key, std::move(cb)).second)
106 throw std::runtime_error(
"IniGroup::add: duplicate key " + key);
109 IniGroup &add(
const string &key,
const cb_t &cb) {
110 if (!cbs_.emplace(key, cb).second)
111 throw std::runtime_error(
"IniGroup::add: duplicate key " + key);
120 template<
typename T,
typename ...Rest>
IniGroup &
121 add(
const string &key, T &&valp,
const string &key2, Rest...rest) {
122 add(key, std::forward<T>(valp));
123 return add(key2, rest...);
std::unordered_map< string, IniGroup > IniActions
Holds the actions to execute on various properties in the ini file.
Most of the xdrpp library is encapsulated in the xdr namespace.
string value_
Value of the property.
IniGroup & add(const string &key, T &&valp, const string &key2, Rest...rest)
A convenience allowing add(field, callback, field, callback...).
void ini_runparse(IniActions &a, IniLine &st, std::istream &s)
Run the ini parser.
bool ini_parse(IniActions &a, string file)
Run the parser on a file.
string rawvalue_
Raw value of the property (with escape sequences unexpanded).
string group_
Name of the group containing property.
void convert(T *rp) const
Convert the line into a value of a type for which a from_string function has been defined...
bool error() const
Returns true if there has been a parsing error in the ini file.
string key_
Key of the property.
Set of callbacks to run when parsing properties within a particular group.
IniGroup & add(const string &key, T *valp)
Construct a callback that places the parced value in valp.
Contents of a specific property.
std::ostream & warn() const
Print a message to cerr with the file ane line number prefixed.
std::ostream & fail() const
Like IniLine::warn, but also sets the error flag to indicate failure.
IniGroup & add(const string &key, cb_t &&cb)
Add an explicit callback for a particular key.
std::vector< string > argv() const
Transform the line into a vector of words.