37 std::ifstream inifile;
40 if (!inifile.is_open())
45 std::string currentSection;
47 while (std::getline(inifile, line))
57 if (line[0] ==
';' || line[0] ==
'#')
62 if (line.front() ==
'[' && line.back() ==
']')
64 currentSection = line.substr(1, line.length() - 2);
68 size_t pos = line.find(
'=');
70 if (pos != std::string::npos && pos > 1)
73 const std::string key = line.substr(0, pos);
74 const std::string value = line.substr(pos + 1);
75 if (!currentSection.empty())
92 if (iniSection->second.find(key) != iniSection->second.end())
100 std::string
IniParser::GetKey(
const std::string& section,
const std::string& key,
const std::string& defaultValue)
const
102 const auto& iniSection =
m_iniValues.find(section);
105 if (iniSection->second.find(key) != iniSection->second.end())
107 return iniSection->second.at(key);
113 int IniParser::GetKey(
const std::string& section,
const std::string& key,
const int defaultValue)
const
115 const auto& iniSection =
m_iniValues.find(section);
118 if (iniSection->second.find(key) != iniSection->second.end())
131 bool IniParser::GetKey(
const std::string& section,
const std::string& key,
const bool defaultValue)
const
133 const auto& iniSection =
m_iniValues.find(section);
136 if (iniSection->second.find(key) != iniSection->second.end())
139 if (text ==
"TRUE" || text ==
"1")
145 if (text ==
"FALSE" || text ==
"0")
159 std::vector<std::string> sections;
162 sections.push_back(section.first);
170 std::vector<std::string> keys;
174 for (
const auto& key : iniSection->second)
176 keys.push_back(key.first);
186 for (
const auto& key : section.second)
188 std::cout << section.first <<
"/" << key.first <<
"=" << key.second << std::endl;
std::string StrTrim(const std::string &str)
remove trailing and leading white spaces and tabs
bool HasKey(const std::string §ion, const std::string &key) const
Checks if given section/key exists.
std::vector< std::string > GetKeys(const std::string §ion) const
returns a vector of all know keys in given section
std::string toupper(const std::string &s)
reads and parse an ini file
void PrintValues() const
for debugging purpose, print all values
std::vector< std::string > GetSections() const
returns a vector of all know sections
bool stringToInt(const std::string &s, int &val)
convert string to integer value, returns true, if sucessful
std::string GetKey(const std::string §ion, const std::string &key, const std::string &defaultValue) const
returns the value of the given section/key or default value if it does not exists ...
int Read(const std::string &file)
Reads the given ini file.
std::map< std::string, IniValues > m_iniValues
map to store the information from different sections