Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cpp/INIReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ bool INIReader::GetBoolean(const string& section, const string& name, bool defau
return default_value;
}

void INIReader::SetValue(const std::string& section, const std::string& name, const std::string& value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, thanks for the contribution! I'm not going to add setting/writing functionality to inih or INIReader, so I'm afraid I'm not going to be adding this method. However, I'm willing to consider Sections and Keys -- I'll discuss those below.

_values[MakeKey(section, name)] = value;
}

std::vector<std::string> INIReader::Sections() const {
std::set<std::string> sectionSet;
for (std::map<std::string, std::string>::const_iterator it = _values.begin(); it != _values.end(); ++it) {
size_t pos = it->first.find('=');
if (pos != std::string::npos) {
sectionSet.insert(it->first.substr(0, pos));
}
}
return std::vector<std::string>(sectionSet.begin(), sectionSet.end());
}

std::vector<std::string> INIReader::Keys(const std::string& section) const {
std::vector<std::string> keys;
std::string keyPrefix = MakeKey(section, "");
for (std::map<std::string, std::string>::const_iterator it = _values.begin(); it != _values.end(); ++it) {
if (it->first.compare(0, keyPrefix.length(), keyPrefix) == 0) {
keys.push_back(it->first.substr(keyPrefix.length()));
}
}
return keys;
}

bool INIReader::HasSection(const string& section) const
{
const string key = MakeKey(section, "");
Expand Down
9 changes: 9 additions & 0 deletions cpp/INIReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class INIReader
// and valid false values are "false", "no", "off", "0" (not case sensitive).
INI_API bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const;

// Sets value of the specified key
INI_API void SetValue(const std::string& section, const std::string& name, const std::string& value);

// Return a newly-allocated vector of all section names, in alphabetical order.
INI_API std::vector<std::string> Sections() const;

// Return a newly-allocated vector of keys in the given section, in alphabetical order.
INI_API std::vector<std::string> Keys(const std::string& section) const;

// Return true if the given section exists (section must contain at least
// one name=value pair).
INI_API bool HasSection(const std::string& section) const;
Expand Down
15 changes: 15 additions & 0 deletions examples/INIReaderExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,20 @@ int main()
<< ", user.nose=" << reader.HasValue("user", "nose") << "\n";
std::cout << "Has sections: user=" << reader.HasSection("user")
<< ", fizz=" << reader.HasSection("fizz") << "\n";

std::cout << "Sections:\n";
std::vector<std::string> sections = reader.Sections();
for (std::vector<std::string>::const_iterator it = sections.begin(); it != sections.end(); ++it) {
std::cout << "- " << *it << "\n";
}

for (std::vector<std::string>::const_iterator it = sections.begin(); it != sections.end(); ++it) {
std::cout << "Keys in section [" << *it << "]:\n";
std::vector<std::string> keys = reader.Keys(*it);
for (std::vector<std::string>::const_iterator kit = keys.begin(); kit != keys.end(); ++kit) {
std::cout << " - " << *kit << "\n";
}
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/cpptest.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Config loaded from 'test.ini': version=6, unsigned version=6, trillion=1000000000000, unsigned trillion=1000000000000, name=Bob Smith, [email protected], pi=3.14159, active=1
Has values: user.name=1, user.nose=0
Has sections: user=1, fizz=0
Sections:
- protocol
- user
Keys in section [protocol]:
- version
Keys in section [user]:
- active
- email
- name
- pi
- trillion
Loading