74# define GUM_CONFIGURATION_USER_FILE "pyagrum.ini"
76# define GUM_CONFIGURATION_USER_FILE "agrum.ini"
88 constexpr std::string_view _undocumented_ =
"(undocumented)";
92 constexpr std::array< std::string_view, 3 > _epsilonKeys_
93 = {
"epsiloncoarse",
"epsilonstandard",
"epsilonfine"};
96 _syntaxError_(
const std::string& msg, std::string_view origin, std::size_t line) {
100 bool _isCommentOrBlank_(std::string_view trimmed) {
101 return trimmed.empty() || trimmed.front() ==
'#' || trimmed.front() ==
';';
105 bool _isMetaLine_(std::string_view trimmed) {
106 return trimmed.size() >= 3 && trimmed.substr(0, 3) ==
"###"
107 && (trimmed.size() == 3 || trimmed[3] !=
'#');
110 bool _isSectionLine_(std::string_view trimmed, std::string& name) {
111 if (trimmed.size() < 3 || trimmed.front() !=
'[' || trimmed.back() !=
']')
return false;
112 const auto inner = trimmed.substr(1, trimmed.size() - 2);
113 if (inner.find(
']') != std::string_view::npos)
return false;
114 name = std::string(inner);
119 bool _splitKeyValue_(std::string_view trimmed, std::string& key, std::string& value) {
120 const auto pos = trimmed.find_first_of(
"=:");
121 if (pos == std::string_view::npos)
return false;
123 value =
trim_copy(trimmed.substr(pos + 1));
127 bool _isIdentifier_(std::string_view s) {
128 if (s.empty() || !(std::isalpha(
static_cast< unsigned char >(s[0])) || s[0] ==
'_')) {
131 return std::all_of(s.begin(), s.end(), [](
char c) {
132 return std::isalnum(static_cast< unsigned char >(c)) || c ==
'_';
136 bool _hasSpace_(std::string_view s) {
137 return std::any_of(s.begin(), s.end(), [](
char c) {
138 return std::isspace(static_cast< unsigned char >(c));
142 std::vector< std::string > _lines_(std::string_view text) {
143 std::vector< std::string > lines;
144 std::size_t prev = 0;
145 while (prev <= text.size()) {
146 auto pos = text.find(
'\n', prev);
147 if (pos == std::string_view::npos) pos = text.size();
148 auto line = text.substr(prev, pos - prev);
149 if (!line.empty() && line.back() ==
'\r') line.remove_suffix(1);
150 lines.emplace_back(line);
153 if (!lines.empty() && lines.back().empty()) lines.pop_back();
157 bool _toInt_(std::string_view s, std::int64_t& res) {
158 if (!s.empty() && s.front() ==
'+') s.remove_prefix(1);
159 if (s.empty())
return false;
160 const auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), res);
161 return ec == std::errc() && ptr == s.data() + s.size();
166 bool _parseDomain_(std::string_view text,
169 std::string& error) {
171 for (
const char c: text)
172 if (!std::isspace(
static_cast< unsigned char >(c))) t += c;
174 const auto isBracket = [](
char c) {
return c ==
'[' || c ==
']'; };
175 const auto comma = t.find(
',');
176 if (t.size() < 3 || !isBracket(t.front()) || !isBracket(t.back())
177 || comma == std::string::npos || t.find(
',', comma + 1) != std::string::npos) {
178 error =
"'" + t +
"' is not a valid domain (e.g. ]0,1[ or [1,])";
182 const auto parseBound = [&](
const std::string& s,
bool& has,
double& value) {
184 if (!has)
return true;
187 if (!_toInt_(s, i))
return false;
188 value =
static_cast< double >(i);
195 domain.lowerIncluded = t.front() ==
'[';
196 domain.upperIncluded = t.back() ==
']';
197 if (!parseBound(t.substr(1, comma - 1), domain.hasLower, domain.lower)
198 || !parseBound(t.substr(comma + 1, t.size() - comma - 2),
201 error =
"'" + t +
"' has a bound that is not " + (isInt ?
"an int" :
"a float");
204 if (domain.hasLower && domain.hasUpper
205 && (domain.lower > domain.upper
206 || (domain.lower == domain.upper
207 && !(domain.lowerIncluded && domain.upperIncluded)))) {
208 error =
"the domain '" + t +
"' is empty";
216 bool _parseMetaSpec_(
const std::string& spec,
217 const std::string& doc,
219 std::string& error) {
224 const auto cut = spec.find_first_of(
"[]");
225 const auto type =
trim_copy(std::string_view(spec).substr(0, cut));
227 if (spec.rfind(
"deprecated", 0) == 0) {
228 const auto rest =
trim_copy(std::string_view(spec).substr(10));
229 if (rest.rfind(
"->", 0) == 0) {
230 const auto target =
trim_copy(std::string_view(rest).substr(2));
231 const auto dot = target.find(
'.');
232 if (
dot != std::string::npos &&
dot > 0 &&
dot + 1 < target.size()
233 && !_hasSpace_(target)) {
235 meta.targetSection = target.substr(0,
dot);
236 meta.targetKey =
toLower(target.substr(
dot + 1));
240 }
else if (type ==
"int" || type ==
"float" || type ==
"bool" || type ==
"str") {
245 if (cut == std::string::npos)
return true;
247 return _parseDomain_(std::string_view(spec).substr(cut),
252 error =
"'" + type +
"' cannot have a domain";
254 }
else if (spec.find(
'|') != std::string::npos) {
255 std::size_t prev = 0;
258 auto pos = spec.find(
'|', prev);
259 if (pos == std::string::npos) pos = spec.size();
260 auto value =
trim_copy(std::string_view(spec).substr(prev, pos - prev));
261 if (value.empty()) ok =
false;
262 meta.values.push_back(std::move(value));
263 if (pos == spec.size())
break;
272 +
"' is not a valid type (int, float, bool, str, int[min,max], float]min,max[, "
274 "deprecated -> section.key)";
278 std::string _readFile_(
const std::string& filename) {
279 std::ifstream f(filename, std::ios::binary);
280 if (!f) {
GUM_ERROR(IOError,
"Cannot read file '" << filename <<
"'.") }
281 std::ostringstream content;
282 content << f.rdbuf();
283 return content.str();
290 for (
const auto& v:
values) {
291 if (!res.empty()) res +=
'|';
311 default :
return "str";
324 for (
const auto& source: sources) {
325 if (!source.text.empty())
_parse_(source.text, source.origin);
335 const auto lines = _lines_(text);
338 std::vector< std::string > openedHere;
340 bool inSection =
false;
342 bool pending =
false;
344 std::size_t numline = 0;
346 auto err = [&](
const std::string& msg) { _syntaxError_(msg, origin, numline); };
348 for (
const auto& rawLine: lines) {
351 std::string key, value, name;
353 const bool hasKeyValue = !_isCommentOrBlank_(trimmed) && _splitKeyValue_(trimmed, key, value);
354 const bool isKeyLine = hasKeyValue && _isIdentifier_(key);
356 if (pending && !isKeyLine) {
357 err(
"metadata line must be immediately followed by a 'key = value' line");
360 if (_isMetaLine_(trimmed)) {
361 const auto payload =
trim_copy(std::string_view(trimmed).substr(3));
362 const auto sep = payload.find(
"::");
363 if (sep == std::string::npos) {
364 err(
"metadata line must follow the syntax '### type :: documentation'");
366 const auto spec =
trim_copy(std::string_view(payload).substr(0, sep));
367 const auto doc =
trim_copy(std::string_view(payload).substr(sep + 2));
368 if (
doc.empty()) err(
"missing documentation in metadata line");
370 if (!_parseMetaSpec_(spec,
doc, pendingMeta, error)) err(error);
375 if (_isCommentOrBlank_(trimmed))
continue;
377 if (_isSectionLine_(trimmed, name)) {
378 if (std::find(openedHere.begin(), openedHere.end(), name) != openedHere.end()) {
379 err(
"duplicate section '" + name +
"'");
381 openedHere.push_back(name);
388 if (!inSection) err(
"key outside of any section");
389 if (!hasKeyValue) err(
"malformed line '" + trimmed +
"'");
393 err(
"duplicate key '" + section +
"." + key +
"' (already defined in "
394 +
_items_[first].origin +
")");
401 item.
origin = std::string(origin);
403 item.
meta = pendingMeta;
406 item.
meta.
doc = std::string(_undocumented_);
409 _items_.push_back(std::move(item));
412 if (pending) err(
"metadata line at end of file");
418 std::vector< Item > grouped;
419 grouped.reserve(
_items_.size());
422 if (item.section == section) grouped.push_back(std::move(item));
427 for (std::size_t pos = 0; pos <
_items_.size(); ++pos) {
435 const auto pos =
_tryFind_(item.meta.targetSection, item.meta.targetKey);
438 << item.section <<
"." << item.key <<
"' redirects to unknown key '"
439 << item.meta.targetSection <<
"." << item.meta.targetKey <<
"'",
446 <<
"' redirects to deprecated key '"
447 << item.meta.targetSection <<
"."
448 << item.meta.targetKey <<
"'",
454 std::string canonical, error;
455 if (!
_validate_(item, item.value, canonical, error)) {
458 item.value = canonical;
459 item.defaultValue = canonical;
469 std::string res(section);
485 const auto pos =
_tryFind_(section, key);
486 if (pos !=
_npos_)
return pos;
488 GUM_SYNTAX_ERROR(
"Section '" << section <<
"' unknown in configuration.",
"", 0, 0)
490 GUM_SYNTAX_ERROR(
"Key '" << section <<
"." << key <<
"' unknown in configuration.",
"", 0, 0)
496 const auto same = [&](
const Notice& n) {
return n.kind == kind && n.message == message; };
503 const auto& item =
_items_[pos];
506 "'" + item.section +
"." + item.key +
"' is deprecated (" + item.meta.doc
507 +
"), use '" + item.meta.targetSection +
"." + item.meta.targetKey
509 if (sink !=
nullptr) sink->push_back(std::move(notice));
511 return _tryFind_(item.meta.targetSection, item.meta.targetKey);
514 std::pair< std::string, Configuration::Kind >
516 std::shared_lock lock(
_mutex_);
518 return {item.value, item.meta.kind};
526 std::string_view value,
527 std::string& canonical,
528 std::string& error) {
529 const auto name = item.
section +
"." + item.
key;
535 std::int64_t res = 0;
536 if (!_toInt_(s, res)) {
537 error =
"'" + s +
"' must be an int for '" + name +
"'.";
541 error =
"'" + s +
"' must be in " + item.
meta.
domain.
text +
" for '" + name +
"'.";
549 error =
"'" + s +
"' must be a float for '" + name +
"'.";
553 error =
"'" + s +
"' must be in " + item.
meta.
domain.
text +
" for '" + name +
"'.";
560 if (low ==
"true" || low ==
"1" || low ==
"on" || low ==
"yes") {
564 if (low ==
"false" || low ==
"0" || low ==
"off" || low ==
"no") {
569 =
"'" + s +
"' must be a boolean (False/True, 0/1, Off/On, No/Yes) for '" + name +
"'.";
580 error =
"'" + s +
"' must be one of " + item.
meta.
joinedValues() +
" for '" + name +
"'.";
583 default : canonical = std::string(value);
return true;
592 std::shared_lock lock(
_mutex_);
597 std::string_view key)
const {
598 const auto [value, kind] =
_typedRaw_(section, key);
601 case Kind::INT :
return static_cast< std::int64_t
>(std::strtoll(value.c_str(),
nullptr, 10));
607 default :
return value;
612 std::shared_lock lock(
_mutex_);
617 std::shared_lock lock(
_mutex_);
622 std::shared_lock lock(
_mutex_);
626 return "deprecated -> " + m.targetSection +
"." + m.targetKey +
" :: " + m.doc;
627 case Kind::ENUM :
return m.joinedValues() +
" :: " + m.doc;
628 default :
return kindName(m.kind) + m.domain.text +
" :: " + m.doc;
633 std::shared_lock lock(
_mutex_);
634 std::vector< SchemaEntry > res;
636 for (
const auto& item:
_items_) {
637 res.push_back(
SchemaEntry{item.section, item.key, item.meta, item.defaultValue});
643 std::shared_lock lock(
_mutex_);
652 std::string canonical, error;
654 _items_[pos].value = std::move(canonical);
665 _epsilons_[i].store(value, std::memory_order_relaxed);
671 std::string_view key,
672 std::string_view value,
675 std::string previous;
677 std::unique_lock lock(
_mutex_);
688 std::unique_lock lock(
_mutex_);
689 _items_[pos].value = std::move(previous);
703 for (
const auto& item:
_items_)
704 res.push_back(item.value);
709 for (std::size_t i = 0; i <
_items_.size(); ++i)
710 _items_[i].value = snapshot[i];
716 std::unique_lock lock(
_mutex_);
725 std::unique_lock lock(
_mutex_);
731 std::unique_lock lock(
_mutex_);
744 const auto lines = _lines_(_readFile_(filename));
748 std::vector< std::pair< std::size_t, std::string > > changes;
749 std::vector< Notice > notices;
750 auto note = [&](std::string message) {
754 std::shared_lock lock(
_mutex_);
756 bool knownSection =
false;
757 std::size_t numline = 0;
759 for (
const auto& rawLine: lines) {
762 std::string name, key, value;
763 if (_isCommentOrBlank_(trimmed))
continue;
765 if (_isSectionLine_(trimmed, name)) {
768 if (!knownSection) { note(
"Section '" + section +
"' does not exist."); }
772 if (section.empty() || !_splitKeyValue_(trimmed, key, value)) {
773 _syntaxError_(
"malformed line '" + trimmed +
"'", filename, numline);
775 if (!knownSection)
continue;
777 const auto pos =
_tryFind_(section, key);
779 note(
"Option '" + section +
"." +
toLower(key) +
"' does not exist.");
782 const auto target =
_resolve_(pos, ¬ices);
784 std::string canonical, error;
786 changes.emplace_back(target, std::move(canonical));
788 note(
"Invalid value for '" + section +
"." +
toLower(key) +
"': " + error);
794 std::unique_lock lock(
_mutex_);
795 for (
auto& [pos, value]: changes)
796 _items_[pos].value = std::move(value);
799 for (
auto& notice: notices)
800 _queue_(notice.kind, std::move(notice.message));
805 const auto content =
diff();
806 std::ofstream f(filename, std::ios::binary | std::ios::trunc);
808 f << content <<
'\n';
813 std::shared_lock lock(
_mutex_);
816 const std::string* current =
nullptr;
817 for (
const auto& item:
_items_) {
819 if (current ==
nullptr || *current != item.section) {
820 if (current !=
nullptr) res +=
'\n';
821 res +=
"[" + item.section +
"]";
822 current = &item.section;
824 res +=
"\n " + item.key +
" = " + item.value;
830 const auto low =
toLower(search);
845 const auto changes =
diff();
846 return changes.empty() ?
"# no customized property\n" +
toString() : changes;
854 std::unique_lock lock(
_mutex_);
855 _hooks_.push_back(std::move(hook));
859 std::vector< Hook > hooks;
861 std::shared_lock lock(
_mutex_);
864 for (
const auto& hook: hooks)
882 if (std::filesystem::is_regular_file(filename))
load(filename);
883 }
catch (
const std::exception& e) {
899 const auto n = c.
typed< std::int64_t >(
"core",
"maxNumberOfThreads");
905 }
catch (
const std::exception& e) {
918 const struct ConfigurationInitializer {
919 ConfigurationInitializer() {
924 } _configurationInitializer_;
Sectioned key/value configuration with typed and documented keys.
std::string describe() const
static Configuration & instance()
The global configuration.
static constexpr std::size_t _npos_
std::unordered_map< std::string, std::size_t > _index_
Configuration(std::string_view defaultsText, std::string_view origin="defaults.ini")
Builds a configuration from a defaults text.
bool has(std::string_view section, std::string_view key) const
std::size_t _find_(std::string_view section, std::string_view key) const
std::shared_mutex _mutex_
void reset()
Back to the defaults. Runs the hooks.
std::vector< Notice > _notices_
std::string _render_(const std::function< bool(const Item &) > &keep) const
static std::string _indexKey_(std::string_view section, std::string_view key)
T typed(std::string_view section, std::string_view key) const
Value of section.key converted according to its declared type.
std::vector< Notice > takeNotices()
std::array< std::size_t, _nbEpsilons_ > _epsilonPos_
std::vector< Snapshot > _stack_
void _queue_(Notice::Kind kind, std::string message) const
static bool _validate_(const Item &item, std::string_view value, std::string &canonical, std::string &error)
std::size_t _resolve_(std::size_t pos, std::vector< Notice > *sink=nullptr) const
void save(const std::string &filename) const
Writes the difference with the defaults (see diff()) in a file.
void addHook(Hook hook)
Adds a function called after each change (see set(), reset(), pop(), load()).
std::vector< std::string > _sections_
std::vector< Hook > _hooks_
std::array< std::atomic< double >, _nbEpsilons_ > _epsilons_
void runHooks() const
Runs the hooks (outside of any lock).
void push()
Pushes the current values on a stack.
void _parse_(std::string_view text, std::string_view origin)
static std::string kindName(Kind kind)
void pop()
Restores the values on top of the stack.
std::function< void(const Configuration &) > Hook
std::string toString() const
std::string grep(std::string_view search) const
std::string doc(std::string_view section, std::string_view key) const
Snapshot _snapshot_() const
std::vector< Item > _items_
std::pair< std::string, Kind > _typedRaw_(std::string_view section, std::string_view key) const
static std::string userFileName()
std::variant< bool, std::int64_t, double, std::string > Value
a value converted according to the declared type of its key (see getValue())
void _setAt_(std::size_t pos, std::string_view value)
std::size_t _tryFind_(std::string_view section, std::string_view key) const
Value getValue(std::string_view section, std::string_view key) const
Value of section.key converted according to its declared type (following a deprecation).
bool _hasSection_(std::string_view section) const
void set(std::string_view section, std::string_view key, std::string_view value, bool noHook=false)
Sets section.key.
static constexpr std::size_t _nbEpsilons_
void load(const std::string &filename)
Loads a user file and changes the values it contains.
std::vector< SchemaEntry > schema() const
Kind
the declared type of a key
void _restore_(const Snapshot &snapshot)
Meta meta(std::string_view section, std::string_view key) const
std::vector< std::string > sections() const
std::string get(std::string_view section, std::string_view key) const
Value of section.key, as a string.
std::vector< std::string > Snapshot
Exception : input/output problem.
Exception: at least one argument passed to a function is not what was expected.
Exception : operation not allowed.
#define GUM_CONFIGURATION_USER_FILE
Typed, documented, sectioned configuration singleton read from an .ini content.
Inline definitions of gum::Configuration.
#define GUM_SYNTAX_ERROR(msg, filename, line, column)
#define GUM_ERROR(type, msg)
void setNumberOfThreads(unsigned int number)
Set the max number of threads to be used when entering the next parallel region.
GUM_SHARED_PUBLIC bool contains(std::string_view s, std::string_view needle)
true if needle in s
GUM_SHARED_PUBLIC std::string toLower(std::string_view str)
Returns the lowercase version of str.
GUM_SHARED_PUBLIC std::string trim_copy(std::string_view s)
trim from both ends (copying)
bool parseDecimal(std::string_view val, double &res)
parse a decimal number, whatever the locale of the program
gum is the global namespace for all aGrUM entities
std::string_view configurationEmbeddedPyAgrumDefaults()
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...
GUM_SHARED_PUBLIC unsigned int getMaxNumberOfThreads()
Returns the maximum number of threads you should use at any time.
std::string_view configurationEmbeddedDefaults()
a text of defaults, with the name designating it in error messages
domain of validity of an int or float key, written after the type in the ### line: float]0,...
std::string text
the domain as written, without blanks (e.g. ]0,1[) ; empty if none
bool contains(double x) const
a message that cannot be an exception (the operation went on), see takeNotices()
one line of the schema (see schema())
Utility functions used for exploiting OpenMP/STL parallelism.
Utilities for manipulating strings.