8 std::string
quoted(
const std::string& value) {
9 return "\"" + value +
"\"";
12 std::string
unquoted(
const std::string& value) {
13 const std::size_t n1 = value.find_first_of(
'"');
14 const std::size_t n2 = value.find_last_of(
'"');
18 return value.substr(n1 + 1, n2 - n1 - 1);
22 return children.
insert(name, makeShared<ConfigNode>());
29 return children[name];
33 return entries.
size();
37 for (
auto& child : children) {
38 func(child.key, *child.value);
40 child.value->enumerateChildren(func);
44 void ConfigNode::write(
const std::string& padding, std::stringstream& source) {
45 for (
auto& element : entries) {
46 source << padding <<
quoted(element.key) <<
" = " << element.value <<
"\n";
49 const std::string childPadding = padding + std::string(2,
' ');
50 for (
auto& child : children) {
51 source << padding <<
quoted(child.key) <<
" [\n";
52 child.value->write(childPadding, source);
53 source << padding <<
"]\n";
57 void ConfigNode::read(std::stringstream& source) {
59 while (std::getline(source, line)) {
62 }
else if (line.back() ==
']') {
67 if (childAndBracket.
size() == 2) {
69 children.insert(
unquoted(childAndBracket[0]), child);
73 if (keyAndValue.
empty()) {
76 entries.insert(
unquoted(keyAndValue[0]), keyAndValue[1]);
82 return nodes.
insert(name, makeShared<ConfigNode>());
103 while (source && !source.eof()) {
106 std::getline(source, name,
'[');
107 if (name.empty() || name ==
"\n") {
116 std::stringstream source;
117 const std::string padding(2,
' ');
118 for (
auto& element : nodes) {
119 source <<
quoted(element.key) <<
" [\n";
120 element.value->write(padding, source);
127 std::ofstream ofs(path.
native());
128 ofs << this->
write();
132 std::ifstream ifs(path.
native());
133 std::stringstream buffer;
134 buffer << ifs.rdbuf();
139 for (
auto& element : nodes) {
140 func(element.key, *element.value);
142 element.value->enumerateChildren(func);
std::string unquoted(const std::string &value)
Removes leading and trailing quote from a string.
NAMESPACE_SPH_BEGIN std::string quoted(const std::string &value)
Helper function wrapping a string by quotes.
Interface for the configuration files storing job data.
Exception ConfigException
uint32_t Size
Integral type used to index arrays (by default).
#define NAMESPACE_SPH_END
Array< std::string > split(const std::string &s, const char delimiter)
Splits a string into an array of string using given delimiter.
Pair< std::string > splitByFirst(const std::string &s, const char delimiter)
Splits a string into two parts, using first occurence of given delimiter.
INLINE TCounter size() const noexcept
Represents a single node in the hierarchy written into config file.
void enumerateChildren(Function< void(std::string name, ConfigNode &node)> func)
Calls the provided functor for each child node.
Size size() const
Returns the number of entries stored in the node.
SharedPtr< ConfigNode > getChild(const std::string &name)
Returns a child node.
SharedPtr< ConfigNode > addChild(const std::string &name)
Adds a new child node to this node.
void read(std::stringstream &source)
Deserializes the input string stream into nodes.
SharedPtr< ConfigNode > addNode(const std::string &name)
Adds a new node to the config.
SharedPtr< ConfigNode > getNode(const std::string &name)
Returns a node with given name.
void save(const Path &path)
Serializes all nodes in the config into a file.
SharedPtr< ConfigNode > tryGetNode(const std::string &name)
Returns a node with given name or nullptr if no such node exists.
std::string write()
Serializes all nodes in the config into a string.
void load(const Path &path)
Reads content of given file and deserializes the config from the loaded string.
void enumerate(Function< void(std::string, ConfigNode &)> func)
Calls the provided functor for all nodes in the config.
Object representing a path on a filesystem.
std::string native() const
Returns the native version of the path.
Array with fixed number of allocated elements.
INLINE bool empty() const
Return true if the array is empty.
INLINE Size size() const
Returns the number of elements in the map.
INLINE bool contains(const TKey &key) const
Returns true if the map contains element of given key.
INLINE void clear()
Removes all elements from the map.
INLINE TValue & insert(const TKey &key, const TValue &value)
Adds a new element into the map or sets new value of element with the same key.