48 std::ifstream
f(file.c_str());
67 const std::string &value,
70 EntryTable::iterator ei =
table.find(entryName);
72 if (ei ==
table.end()) {
74 table.emplace(entryName, value);
78 ei->second.appendValue(value);
82 ei->second.setValue(value);
90 std::string::size_type
offset = assignment.find(
'=');
91 if (
offset == std::string::npos) {
93 std::cerr <<
"Can't parse .ini line " << assignment << std::endl;
98 bool append = (assignment[
offset-1] ==
'+');
100 std::string entryName = assignment.substr(0, append ?
offset-1 :
offset);
101 std::string value = assignment.substr(
offset + 1);
106 addEntry(entryName, value, append);
115 std::as_const(*this).findEntry(entryName));
123 auto ei =
table.find(entryName);
125 return (ei ==
table.end()) ? nullptr : &ei->second;
132 return &
table[sectionName];
139 std::as_const(*this).findSection(sectionName));
145 auto i =
table.find(sectionName);
147 return (
i ==
table.end()) ? nullptr : &
i->second;
157 std::string::size_type
offset = str.find(
':');
158 if (
offset == std::string::npos)
161 std::string sectionName = str.substr(0,
offset);
162 std::string rest = str.substr(
offset + 1);
183 if (line.size() == 0)
187 int last = line.size() - 1;
189 if (line[0] ==
'[' && line[last] ==
']') {
190 std::string sectionName = line.substr(1, last - 1);
199 if (!section->
add(line))
208 std::string &value)
const
214 auto* entry = section->findEntry(entryName);
218 value = entry->getValue();
225 const std::string &entryName)
const
232 return section->findEntry(entryName);
246 bool search_unref_entries =
false;
249 auto* entry = findEntry(
"unref_entries_ok");
251 tokenize(unref_ok_entries, entry->getValue(),
' ');
252 if (unref_ok_entries.size()) {
253 search_unref_entries =
true;
257 for (
auto& ei:
table) {
258 const std::string &entryName = ei.first;
261 if (entryName ==
"unref_section_ok" ||
262 entryName ==
"unref_entries_ok")
267 if (!entry->isReferenced()) {
268 if (search_unref_entries &&
269 (std::find(unref_ok_entries.begin(), unref_ok_entries.end(),
270 entryName) != unref_ok_entries.end()))
275 std::cerr <<
"Parameter " << sectionName <<
":" << entryName
276 <<
" not referenced." << std::endl;
288 for (
auto& entry:
table) {
289 auto& sectionName = entry.first;
290 list.push_back(sectionName);
299 for (
auto& entry:
table) {
300 auto& [sectionName, section] = entry;
302 if (!section.isReferenced()) {
303 if (section.findEntry(
"unref_section_ok") == NULL) {
304 std::cerr <<
"Section " << sectionName <<
" not referenced."
310 if (section.printUnreferenced(sectionName)) {
323 for (
auto& ei:
table) {
324 std::cout << sectionName <<
": " << ei.first <<
" => "
325 << ei.second.getValue() <<
"\n";
332 for (SectionTable::iterator
i =
table.begin();
334 i->second.dump(
i->first);
338IniFile::Section::EntryTable::const_iterator
341 return table.begin();
344IniFile::Section::EntryTable::const_iterator
354 const auto& section =
table.at(sectionName);
355 for (
const auto& pair : section) {
356 cb(pair.first, pair.second.getValue());
std::string value
The entry value.
const std::string & getValue() const
Fetch the value.
bool referenced
Has this entry been used?
bool printUnreferenced(const std::string §ionName) const
Print the unreferenced entries in this section to cerr.
void addEntry(const std::string &entryName, const std::string &value, bool append)
Add an entry to the table.
EntryTable::const_iterator end() const
bool add(const std::string &assignment)
Add an entry to the table given a string assigment.
EntryTable::const_iterator begin() const
void dump(const std::string §ionName) const
Print the contents of this section to cout (for debugging).
Entry * findEntry(const std::string &entryName)
Find the entry with the given name.
bool add(const std::string &s)
Take string of the form "<section>:<parameter>=<value>" or "<section>:<parameter>+=<value>" and add t...
bool find(const std::string §ion, const std::string &entry, std::string &value) const
Find value corresponding to given section and entry names.
void visitSection(const std::string §ionName, VisitSectionCallback cb)
Iterate over key/value pairs of the given section.
std::function< void( const std::string &, const std::string &)> VisitSectionCallback
Visitor callback that receives key/value pairs.
SectionTable table
Hash of section names to Section object pointers.
bool entryExists(const std::string §ion, const std::string &entry) const
Determine whether the entry exists within named section exists in the .ini file.
void getSectionNames(std::vector< std::string > &list) const
Push all section names into the given vector.
bool load(std::istream &f)
Load parameter settings from given istream.
void dump()
Dump contents to cout. For debugging.
bool printUnreferenced() const
Print unreferenced entries in object.
Section * addSection(const std::string §ionName)
Look up section with the given name, creating a new section if not found.
bool sectionExists(const std::string §ion) const
Determine whether the named section exists in the .ini file.
Section * findSection(const std::string §ionName)
Look up section with the given name.
Declaration of IniFile object.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
void eat_end_white(std::string &s)
void eat_white(std::string &s)