bool INIFile::ParseFile(ifstream& f) { if ( !f.is_open() ) return false; char* pPos1 = NULL, *pPos2 = NULL; int nLine = 0; char buffer[1024]; string strSection, strKey, strVal; CSection* pSection = NULL; while ( f.getline(buffer, sizeof(buffer)) ) { nLine++; // is this the beginning of a section? if ( buffer[0] == c_INISectionNameStart ) { // yes, so get the end of the section name pPos1 = strchr( buffer, c_INISectionNameEnd ); *pPos1 = '\0'; pSection = new CSection; m_lstSections.push_back(pSection); strSection = &buffer[1]; std::transform(strSection.begin(), strSection.end(), strSection.begin(), (int(*)(int))tolower); pSection->SetName( strSection ); continue; } // check that it is not a comment if ( buffer[0] != c_INIComment ) { if ( (pPos2 = strchr( buffer, '=' )) ) { *pPos2 = '\0'; strKey = buffer; // make the key lower case std::transform(strKey.begin(), strKey.end(), strKey.begin(), (int(*)(int))tolower); // trim the key strKey.erase( strKey.find_last_not_of(" \r\n")+1); strVal = pPos2+1; // trim the value strKey.erase( strKey.find_last_not_of(" \r\n")+1); if ( pSection ) { pSection->AddKey( strKey, strVal); } else { return false; } } } } return true; }
INIFile::CSection* INIFile::AddSection(string name) { if ( !name.empty() ) return NULL; CSection* pSection = new CSection; pSection->SetName(name); m_lstSections.push_back(pSection); return pSection; }