void TUserIniFile::RecreateValues() { TStrings* AStrings; for(int i=0; i < SectionCount(); i++) { AStrings = (TStrings*)Sections->Objects[i]; for(int j=0; j < AStrings->Count; j++) WriteString(SectionNames[i], AStrings->Names[j], AStrings->Values[AStrings->Names[j]]); }; }
void TUserIniFile::RecreateSections() { const String CR = "\n"; String* S; TFileStream* FileStream = new TFileStream(FileName, fmCreate); try { for(int i=0; i < SectionCount(); i++) { FileStream->Seek(0, soFromEnd); S = new String(); *S = Format((String)"%s[%s]%s", ARRAYOFCONST((CR, SectionNames[i], CR))); FileStream->WriteBuffer((const void *)S, S->Length()); delete S; } } __finally { delete FileStream; } }
void TUserIniFile::Clear() { for(int i=0; i < SectionCount(); i++) ReleaseSection(i); Sections->Clear(); }
// 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; }