6#include <kernel/util/property_map.hpp>
7#include <kernel/util/dist_file_io.hpp>
28 std::pair<EntryMap::iterator, bool> rtn(
_values.insert(std::make_pair(key,
value)));
38 (*rtn.first).second =
value;
47 SectionMap::iterator it(
_sections.find(name));
52 return ((*it).second).get();
56 return _sections.emplace(name, std::make_shared<PropertyMap>(
this)).first->second.get();
61 EntryMap::iterator it(
_values.find(key));
72 SectionMap::iterator it(
_sections.find(name));
99 std::size_t p = path.find(
'/');
103 sec_name = path.substr(0, p).
trim();
104 rem_path = path.substr(p+1).
trim();
111 else if(sec_name ==
"~")
119 else if(lead !=
nullptr)
131 std::size_t p = key_path.rfind(
'/');
134 if(p == key_path.npos)
142 String sec_path = key_path.substr(0, p).
trim();
144 String key_name = key_path.substr(p+1).
trim();
151 return std::make_pair(
String(),
false);
159 std::pair<String, bool> sb = this->
query(key_path);
162 return sb.second ? sb.first : default_value;
167 EntryMap::const_iterator iter(
_values.find(key));
170 return std::make_pair(
"",
false);
172 return std::make_pair(iter->second,
true);
177 SectionMap::const_iterator iter(
_sections.find(name));
182 return (iter->second).get();
187 SectionMap::iterator iter(
_sections.find(name));
192 return (iter->second).get();
197 EntryMap::const_iterator valiter(section.
_values.begin());
198 EntryMap::const_iterator valend(section.
_values.end());
201 for(; valiter != valend; ++valiter)
203 add_entry(valiter->first, valiter->second, replace);
206 SectionMap::const_iterator seciter(section.
_sections.begin());
207 SectionMap::const_iterator secend(section.
_sections.end());
210 for(; seciter != secend; ++seciter)
219 std::stringstream is;
227 std::stack<PropertyMap*> stack;
239 String::size_type found;
254 LastRead last_read = None;
257 while(!ifs.eof() && ifs.good())
293 if((found = line.find(
'#')) != std::string::npos)
306 if((line.front() ==
'[') && (line.back() ==
']'))
326 else if((found = line.find(
'=')) != std::string::npos)
329 if(last_read == BraceClose)
336 key = line.substr(0, found);
353 int old_line = cur_line;
356 while(
value.back() ==
'&')
363 while(line.empty() && !ifs.eof() && ifs.good())
370 if((found = line.find(
'#')) != std::string::npos)
383 throw SyntaxError(
"Only empty lines from line " +
stringify(old_line) +
" on for line continuation");
399 if(last_read == Section)
403 last_read = BraceOpen;
417 if((last_read != None) && (stack.size() > 1))
422 current = stack.top();
423 last_read = BraceClose;
452 std::ofstream ofs(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
455 throw FileError(
"Failed to create '" + filename +
"' for dumping!");
468 String prefix(2*indent,
' ');
471 EntryMap::const_iterator vit(
_values.begin());
472 EntryMap::const_iterator vend(
_values.end());
473 for( ; vit != vend ; ++vit)
475 os << prefix << (*vit).first <<
" = " << (*vit).second <<
"\n";
479 SectionMap::const_iterator sit(
_sections.begin());
480 SectionMap::const_iterator send(
_sections.end());
481 for( ; sit != send ; ++sit)
484 os << prefix <<
"[" << (*sit).first <<
"]\n";
485 os << prefix <<
"{\n";
488 (*sit).second->write(os, indent + 1);
491 os << prefix <<
"} # end of [" << (*sit).first <<
"]\n";
497 for(
const auto& sec : deq)
504 void _treeify(PropertyMap* out,
const PropertyMap* in)
506 for(
auto iter = in->begin_section(); iter != in->end_section(); ++iter)
508 std::deque<String> key_deq =
iter->first.split_by_charset(
"/");
509 auto curr_sec = _add_sections(out, key_deq);
510 for(
auto iter_entry =
iter->second->begin_entry(); iter_entry !=
iter->second->end_entry(); ++iter_entry)
512 curr_sec->add_entry(iter_entry->first, iter_entry->second);
514 _treeify(curr_sec,
iter->second.get());
520 auto base_prop = std::make_unique<PropertyMap>();
int rank() const
Returns the rank of this process in this communicator.
static void read_common(std::stringstream &stream, const String &filename, const Dist::Comm &comm, int root_rank=0)
Reads a common text file for all ranks.
Base class for file related errors.
A class organizing a tree of key-value pairs.
PropertyMap * add_section(String name)
Adds a new sub-section to this map.
bool erase_section(String name)
Erases a sub-section.
PropertyMap * get_parent()
Returns a pointer to the parent section.
PropertyMap * get_root()
Returns a pointer to the root section.
std::unique_ptr< PropertyMap > treeify_structures() const
Builds a new property map by inserting subsection based on section names.
PropertyMap * get_sub_section(String name)
Retrieves a sub-section by its name.
PropertyMap * query_section(String sec_path)
Queries a section by its section path.
std::pair< String, bool > query(String key_path) const
Queries a value by its key path.
EntryMap _values
a map storing the key-value-pairs
SectionMap _sections
a map storing the sub-sections
virtual ~PropertyMap()
Virtual Destructor.
bool erase_entry(String key)
Erases a key-value pair.
PropertyMap(PropertyMap *parent=nullptr)
Default Constructor.
bool add_entry(String key, String value, bool replace=true)
Adds a new key-value pair to this map.
void merge(const PropertyMap §ion, bool replace=true)
Merges another PropertyMap into this.
std::pair< String, bool > get_entry(String key) const
Retrieves a value by its key.
void read(String filename, bool replace=true)
Parses a file in INI-format.
void write(String filename) const
Writes the property map into a file.
String class implementation.
void pop_front()
Removes the first character from the string.
String & trim_me(const String &charset)
Trims this string.
void pop_back()
Removes the last character from the string.
String trim(const String &charset) const
Trims the string.
Syntax Error exception class.
@ iter
Plot every iteration (if applicable)
String stringify(const T_ &item)
Converts an item into a String.
@ value
specifies whether the space should supply basis function values