diff --git a/generator/src/INI.cpp b/generator/src/INI.cpp index 43e4bb6..bc973e7 100644 --- a/generator/src/INI.cpp +++ b/generator/src/INI.cpp @@ -110,6 +110,22 @@ static void removeChar(std::vector &pStrs, const char pChar) { } } +/* Private helper functions ---------------------------- */ +bool INI::sectionExists(const std::string &pSection) const { + /* Does this section exist ? */ + return (mSections.end() != mSections.find(pSection)); +} + +bool INI::keyExists(const std::string &pKey, const std::string &pSection) const { + /* Does the section and key exist ? + * We also check the section here because otherwise + * the "at" method will throw a out_of_range + * exception if it soesn't exist. */ + return ((sectionExists(pSection)) + && (mSections.at(pSection).end() != mSections.at(pSection).find(pKey))); +} + + /* INI class ------------------------------------------- */ INI::INI(const std::string &pFile) { mFileStream.open(pFile, std::ios::in | std::ios::out); @@ -175,8 +191,7 @@ INI::INI(const std::string &pFile) { lSection = lLine.substr(1, lPos - 1); /* Does this section exist already ? */ - std::vector::iterator lSectionIt = find(mSectionOrder.begin(), mSectionOrder.end(), lSection); - if(mSectionOrder.end() != lSectionIt) { + if(sectionExists(lSection)) { /* This section already exists ! */ std::cerr << "[ERROR] Duplicate Section in INI file at line " << lLineCount << std::endl; mFileStream.close(); @@ -238,8 +253,7 @@ INI::INI(const std::string &pFile) { * First, we must check if the section exists */ if(!lNewSection) { - std::vector::iterator lKeyIt = find(mSectionElementOrder.at(lSection).begin(), mSectionElementOrder.at(lSection).end(), lKey); - if(mSectionElementOrder.at(lSection).end() != lKeyIt) { + if(keyExists(lKey)) { /* This key already exists ! */ std::cerr << "[ERROR] Duplicate Key in INI file at line " << lLineCount << std::endl; mFileStream.close(); @@ -275,7 +289,9 @@ int INI::getValue(const std::string &pKey, std::string &pOut, const std::string &pSection) const { + /* Check if the section exists */ if(mSections.end() != mSections.find(pSection)) { + /* Check that the key exists */ if(mSections.at(pSection).end() != mSections.at(pSection).find(pKey)) { pOut = mSections.at(pSection).at(pKey); return 0; diff --git a/generator/src/INI.hpp b/generator/src/INI.hpp index fdeb1c3..198702c 100644 --- a/generator/src/INI.hpp +++ b/generator/src/INI.hpp @@ -69,6 +69,8 @@ class INI { std::vector mSectionOrder; private: + bool sectionExists(const std::string &pSection) const; + bool keyExists(const std::string &pKey, const std::string &pSection = "default") const; }; #endif /* INI_HPP */