// Attempts to save the Section list and keys to the file. Note that if Load // was never called (the IniFile object was created manually), then you // must set the mFileName variable before calling save. bool IniFile::Save(ios_base::openmode openMode) { if ( mIniFileName.size() == 0 ) { Log(lError)<<"No filename has been set. Can't save.."; return false; } string aFName = mIniFileName.Get(); fstream aFstream(aFName.c_str(), openMode); if(aFstream.is_open()) { SectionItor s_pos; KeyItor k_pos; IniSection* Section; IniKey* Key; for (s_pos = mSections.begin(); s_pos != mSections.end(); s_pos++) { Section = (*s_pos); bool bWroteComment = false; if ( Section->mComment.size() > 0 ) { bWroteComment = true; WriteLine(aFstream, "\n%s", CommentStr(Section->mComment).c_str()); } if ( Section->mName.size() > 0 ) { WriteLine(aFstream, "%s[%s]", bWroteComment ? "" : "\n", Section->mName.c_str()); } for (k_pos = Section->mKeys.begin(); k_pos != Section->mKeys.end(); k_pos++) { Key = (*k_pos); if ( Key->mKey.size() > 0 && Key->mValue.size() > 0 ) { WriteLine(aFstream, "%s%s%s%s%c%s", Key->mComment.size() > 0 ? "\n" : "", CommentStr(Key->mComment).c_str(), Key->mComment.size() > 0 ? "\n" : "", Key->mKey.c_str(), mEqualIndicator[0], Key->mValue.c_str()); } } } } else { Log(lError)<<"[IniFile::Save] Unable to save file."; return false; } mIsDirty = false; aFstream.flush(); aFstream.close(); Log(lDebug3)<<"IniFile was saved"; return true; }
// Save // Attempts to save the Section list and keys to the file. Note that if Load // was never called (the CDataFile object was created manually), then you // must set the m_szFileName variable before calling save. bool CDataFile::Save() { if ( KeyCount() == 0 && SectionCount() == 0 ) { // no point in saving Report(E_INFO, "[CDataFile::Save] Nothing to save."); return false; } if ( m_szFileName.size() == 0 ) { Report(E_ERROR, "[CDataFile::Save] No filename has been set."); return false; } std::fstream File(m_szFileName.c_str(), std::ios_base::out|std::ios_base::trunc); if ( File.is_open() ) { SectionItor s_pos; KeyItor k_pos; t_Section Section; t_Key Key; for (s_pos = m_Sections.begin(); s_pos != m_Sections.end(); s_pos++) { Section = (*s_pos); bool bWroteComment = false; if ( Section.szComment.size() > 0 ) { bWroteComment = true; WriteLn(File, "\n%s", CommentStr(Section.szComment).c_str()); } if ( Section.szName.size() > 0 ) { WriteLn(File, "%s[%s]", bWroteComment ? "" : "\n", Section.szName.c_str()); } for (k_pos = Section.Keys.begin(); k_pos != Section.Keys.end(); k_pos++) { Key = (*k_pos); if ( Key.szKey.size() > 0 && Key.szValue.size() > 0 ) { WriteLn(File, "%s%s%s%s%c%s", Key.szComment.size() > 0 ? "\n" : "", CommentStr(Key.szComment).c_str(), Key.szComment.size() > 0 ? "\n" : "", Key.szKey.c_str(), EqualIndicators[0], Key.szValue.c_str()); } } } } else { Report(E_ERROR, "[CDataFile::Save] Unable to save file."); return false; } m_bDirty = false; File.flush(); File.close(); return true; }