Merge pull request #2 from exante/master
Add Sections() method
diff --git a/INIReader.h b/INIReader.h
index 4a42781..772dcc0 100644
--- a/INIReader.h
+++ b/INIReader.h
@@ -304,6 +304,7 @@
#define __INIREADER_H__
#include <map>
+#include <set>
#include <string>
// Read an INI file into easy-to-access name/value pairs. (Note that I've gone
@@ -319,6 +320,9 @@
// first error on parse error, or -1 on file open error.
int ParseError();
+ // Return the list of sections found in ini file
+ std::set<std::string> Sections();
+
// Get a string value from INI file, returning default_value if not found.
std::string Get(std::string section, std::string name,
std::string default_value);
@@ -340,6 +344,7 @@
private:
int _error;
std::map<std::string, std::string> _values;
+ std::set<std::string> _sections;
static std::string MakeKey(std::string section, std::string name);
static int ValueHandler(void* user, const char* section, const char* name,
const char* value);
@@ -367,6 +372,11 @@
return _error;
}
+inline std::set<string> INIReader::Sections()
+{
+ return _sections;
+}
+
inline string INIReader::Get(string section, string name, string default_value)
{
string key = MakeKey(section, name);
@@ -421,7 +431,8 @@
if (reader->_values[key].size() > 0)
reader->_values[key] += "\n";
reader->_values[key] += value;
+ reader->_sections.insert(section);
return 1;
}
-#endif // __INIREADER__
\ No newline at end of file
+#endif // __INIREADER__
diff --git a/INIReaderTest.cpp b/INIReaderTest.cpp
index 50b04a1..8472122 100644
--- a/INIReaderTest.cpp
+++ b/INIReaderTest.cpp
@@ -1,8 +1,18 @@
// Example that shows simple usage of the INIReader class
#include <iostream>
+#include <sstream>
#include "INIReader.h"
+std::string sections(INIReader &reader)
+{
+ std::stringstream ss;
+ std::set<std::string> sections = reader.Sections();
+ for (std::set<std::string>::iterator it = sections.begin(); it != sections.end(); ++it)
+ ss << *it << ",";
+ return ss.str();
+}
+
int main()
{
INIReader reader("test.ini");
@@ -11,7 +21,8 @@
std::cout << "Can't load 'test.ini'\n";
return 1;
}
- std::cout << "Config loaded from 'test.ini': version="
+ std::cout << "Config loaded from 'test.ini': found sections=" << sections(reader)
+ << " version="
<< reader.GetInteger("protocol", "version", -1) << ", name="
<< reader.Get("user", "name", "UNKNOWN") << ", email="
<< reader.Get("user", "email", "UNKNOWN") << ", multi="