Skip to content

Commit 93f392b

Browse files
authored
Add INIReader Sections and Keys methods (#186)
1 parent 63a302c commit 93f392b

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

cpp/INIReader.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ long INIReader::GetInteger(const string& section, const string& name, long defau
5656
return end > value ? n : default_value;
5757
}
5858

59-
INI_API int64_t INIReader::GetInteger64(const std::string& section, const std::string& name, int64_t default_value) const
59+
INI_API int64_t INIReader::GetInteger64(const string& section, const string& name, int64_t default_value) const
6060
{
6161
string valstr = Get(section, name, "");
6262
const char* value = valstr.c_str();
@@ -76,7 +76,7 @@ unsigned long INIReader::GetUnsigned(const string& section, const string& name,
7676
return end > value ? n : default_value;
7777
}
7878

79-
INI_API uint64_t INIReader::GetUnsigned64(const std::string& section, const std::string& name, uint64_t default_value) const
79+
INI_API uint64_t INIReader::GetUnsigned64(const string& section, const string& name, uint64_t default_value) const
8080
{
8181
string valstr = Get(section, name, "");
8282
const char* value = valstr.c_str();
@@ -109,6 +109,30 @@ bool INIReader::GetBoolean(const string& section, const string& name, bool defau
109109
return default_value;
110110
}
111111

112+
std::vector<string> INIReader::Sections() const
113+
{
114+
std::set<string> sectionSet;
115+
for (std::map<string, string>::const_iterator it = _values.begin(); it != _values.end(); ++it) {
116+
size_t pos = it->first.find('=');
117+
if (pos != string::npos) {
118+
sectionSet.insert(it->first.substr(0, pos));
119+
}
120+
}
121+
return std::vector<string>(sectionSet.begin(), sectionSet.end());
122+
}
123+
124+
std::vector<string> INIReader::Keys(const string& section) const
125+
{
126+
std::vector<string> keys;
127+
string keyPrefix = MakeKey(section, "");
128+
for (std::map<string, string>::const_iterator it = _values.begin(); it != _values.end(); ++it) {
129+
if (it->first.compare(0, keyPrefix.length(), keyPrefix) == 0) {
130+
keys.push_back(it->first.substr(keyPrefix.length()));
131+
}
132+
}
133+
return keys;
134+
}
135+
112136
bool INIReader::HasSection(const string& section) const
113137
{
114138
const string key = MakeKey(section, "");

cpp/INIReader.h

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <map>
1616
#include <string>
1717
#include <cstdint>
18+
#include <vector>
19+
#include <set>
1820

1921
// Visibility symbols, required for Windows DLLs
2022
#ifndef INI_API
@@ -89,6 +91,12 @@ class INIReader
8991
// and valid false values are "false", "no", "off", "0" (not case sensitive).
9092
INI_API bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const;
9193

94+
// Return a newly-allocated vector of all section names, in alphabetical order.
95+
INI_API std::vector<std::string> Sections() const;
96+
97+
// Return a newly-allocated vector of keys in the given section, in alphabetical order.
98+
INI_API std::vector<std::string> Keys(const std::string& section) const;
99+
92100
// Return true if the given section exists (section must contain at least
93101
// one name=value pair).
94102
INI_API bool HasSection(const std::string& section) const;

examples/INIReaderExample.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,20 @@ int main()
2424
<< ", user.nose=" << reader.HasValue("user", "nose") << "\n";
2525
std::cout << "Has sections: user=" << reader.HasSection("user")
2626
<< ", fizz=" << reader.HasSection("fizz") << "\n";
27+
28+
std::cout << "Sections:\n";
29+
std::vector<std::string> sections = reader.Sections();
30+
for (std::vector<std::string>::const_iterator it = sections.begin(); it != sections.end(); ++it) {
31+
std::cout << "- " << *it << "\n";
32+
}
33+
34+
for (std::vector<std::string>::const_iterator it = sections.begin(); it != sections.end(); ++it) {
35+
std::cout << "Keys in section [" << *it << "]:\n";
36+
std::vector<std::string> keys = reader.Keys(*it);
37+
for (std::vector<std::string>::const_iterator kit = keys.begin(); kit != keys.end(); ++kit) {
38+
std::cout << "- " << *kit << "\n";
39+
}
40+
}
41+
2742
return 0;
2843
}

examples/cpptest.txt

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
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
22
Has values: user.name=1, user.nose=0
33
Has sections: user=1, fizz=0
4+
Sections:
5+
- protocol
6+
- user
7+
Keys in section [protocol]:
8+
- version
9+
Keys in section [user]:
10+
- active
11+
- email
12+
- name
13+
- pi
14+
- trillion

0 commit comments

Comments
 (0)