//==================================================================================== int IniFile::extractSection(const AnsiStr& title, std::vector<AnsiStr>& lines) { lines.resize(0); AnsiStr str; AnsiStr titleUpperCased = title; titleUpperCased.toUpper(); int idxStart = -1; for(int i=0; i < (int)m_content.size(); i++) { str = m_content[i]; if((str.firstChar() == '[')&&(str.lastChar() == ']')) { //[ + ] = 2 str = str.substr(1, str.length() - 2); if(str.toUpper() == titleUpperCased) { idxStart = i; break; } } } //Process Content for(int i=idxStart+1; i < (int)m_content.size(); i++) { str = m_content[i]; if((str.firstChar() == '[')&&(str.lastChar() == ']')) break; else lines.push_back(str); } return (int)lines.size(); }
//==================================================================================== bool IniFile::readLine(const AnsiStr& section, const AnsiStr& variable, AnsiStr& strLine) { int startPos = hasSection(section); if(startPos < 0) return false; //Record file pos for(int i=startPos+1; i < (int)m_content.size(); i++) { strLine = m_content[i]; //If it is a comment then ignore if(strLine.firstChar() == '#') continue; //Is section? if((strLine.firstChar() == L'[')&&(strLine.lastChar() == L']')) { return false; } else { for(int iChar=0; iChar < strLine.length(); iChar++) { if(strLine[iChar] == L'=') { AnsiStr str = strLine.substr(0, iChar); AnsiStr varUpperCased = variable; varUpperCased.toUpper(); if( str.toUpper() == varUpperCased ) { return true; } else break; } } } } return false; }
//==================================================================================== int IniFile::hasSection(const AnsiStr& strSection) { AnsiStr str; for(int i=0; i < (int)m_content.size(); i++) { str = m_content[i]; char ch = str.firstChar(); ch = str.lastChar(); if((str.firstChar() == '[')&&(str.lastChar() == ']')) { //[ + ] = 2 str = str.substr(1, str.length() - 2); AnsiStr strSecUpperCased = strSection; strSecUpperCased.toUpper(); if(str.toUpper() == strSecUpperCased) return i; } } return -1; }
//==================================================================================== bool IniFile::writeValue(const AnsiStr& section, const AnsiStr& variable, const AnsiStr& strValue) { AnsiStr strLine, strToAdd; strToAdd = variable; strToAdd += "="; strToAdd += strValue; int startPos = hasSection(section); if(startPos >= 0) { //Record file pos for(int i=startPos+1; i < (int)m_content.size(); i++) { strLine = m_content[i]; //If it is a comment then ignore if(strLine.firstChar() == '#') continue; //Is it another section? if((strLine.firstChar() == L'[')&&(strLine.lastChar() == L']')) { m_content.insert(m_content.begin() + i, strToAdd); return true; } else { //Check Variable for(int iChar=0; iChar < strLine.length(); iChar++) { if(strLine[iChar] == L'=') { strLine = strLine.substr(0, iChar); AnsiStr varUpperCased = variable; varUpperCased.toUpper(); if( strLine.toUpper() == varUpperCased ) { //Write it here m_content[i] = strToAdd; return true; } else break; } } } } //Not Written m_content.push_back(strToAdd); } else { //Add it if not found anywhere strLine = AnsiStr("[") + section + AnsiStr("]"); m_content.push_back(strLine); m_content.push_back(strToAdd); } return true; }