From 3b4b3d8550cda4dd15af1325704ddd2d2db2ae74 Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Sun, 6 Apr 2003 14:38:19 +0000 Subject: [PATCH] Fix for deleting entries at root level in wxFileConfig backported from head git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@20012 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + src/common/fileconf.cpp | 87 +++++++++++++++++++++++++++-------------- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index d56c51c7c6..3f10d677e5 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -179,6 +179,7 @@ All: - added missing wx/quantize.h to install - improved wxScrolledWindow/sizer interaction - fixed assert (and crash in debug) in wxFileConfigGroup::DeleteSubgroupByName +- fix for deleting entries at root level in wxFileConfig Unix (GUI): diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 171eb89d78..fa8a95c6b8 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -246,8 +246,9 @@ public: wxFileConfigLineList *GetLastGroupLine(); // after which the next group starts // called by entries/subgroups when they're created/deleted - void SetLastEntry(wxFileConfigEntry *pEntry) { m_pLastEntry = pEntry; } - void SetLastGroup(wxFileConfigGroup *pGroup) { m_pLastGroup = pGroup; } + void SetLastEntry(wxFileConfigEntry *pEntry); + void SetLastGroup(wxFileConfigGroup *pGroup) + { m_pLastGroup = pGroup; } }; // ============================================================================ @@ -565,8 +566,17 @@ void wxFileConfig::Parse(wxTextBuffer& buffer, bool bLocal) // add the line to linked list if ( bLocal ) + { LineListAppend(strLine); + // let the root group have it start line as well + if ( !n ) + { + m_pCurrentGroup->SetLine(m_linesTail); + } + } + + // skip leading spaces for ( pStart = strLine; wxIsspace(*pStart); pStart++ ) ; @@ -1127,8 +1137,7 @@ wxFileConfigLineList *wxFileConfig::LineListAppend(const wxString& str) return m_linesTail; } - // insert a new line after the given one or in the very beginning if !pLine - +// insert a new line after the given one or in the very beginning if !pLine wxFileConfigLineList *wxFileConfig::LineListInsert(const wxString& str, wxFileConfigLineList *pLine) { @@ -1307,7 +1316,7 @@ wxFileConfigLineList *wxFileConfigGroup::GetGroupLine() _T(" GetGroupLine() for Group '%s'"), Name().c_str() ); - if ( m_pLine == 0 ) + if ( !m_pLine ) { wxLogTrace( _T("wxFileConfig"), _T(" Getting Line item pointer") ); @@ -1315,8 +1324,7 @@ wxFileConfigLineList *wxFileConfigGroup::GetGroupLine() wxFileConfigGroup *pParent = Parent(); // this group wasn't present in local config file, add it now - - if ( pParent != 0 ) + if ( pParent ) { wxLogTrace( _T("wxFileConfig"), _T(" checking parent '%s'"), @@ -1324,18 +1332,16 @@ wxFileConfigLineList *wxFileConfigGroup::GetGroupLine() wxString strFullName; - strFullName << wxT("[") // +1: no '/' + // add 1 to the name because we don't want to start with '/' + strFullName << wxT("[") << FilterOutEntryName(GetFullName().c_str() + 1) << wxT("]"); m_pLine = m_pConfig->LineListInsert(strFullName, pParent->GetLastGroupLine()); pParent->SetLastGroup(this); // we're surely after all the others } - else - { - // we return NULL, so that LineListInsert() will insert us in the - // very beginning - } + //else: this is the root group and so we return NULL because we don't + // have any group line } return m_pLine; @@ -1346,19 +1352,18 @@ wxFileConfigLineList *wxFileConfigGroup::GetGroupLine() // last line is the group line (m_pLine) itself. wxFileConfigLineList *wxFileConfigGroup::GetLastGroupLine() { - // if we have any subgroups, our last line is - // the last line of the last subgroup - - if ( m_pLastGroup != 0 ) + // if we have any subgroups, our last line is the last line of the last + // subgroup + if ( m_pLastGroup ) { wxFileConfigLineList *pLine = m_pLastGroup->GetLastGroupLine(); - wxASSERT( pLine != 0 ); // last group must have !NULL associated line + wxASSERT_MSG( pLine, _T("last group must have !NULL associated line") ); + return pLine; } // no subgroups, so the last line is the line of thelast entry (if any) - return GetLastEntryLine(); } @@ -1371,30 +1376,52 @@ wxFileConfigLineList *wxFileConfigGroup::GetLastEntryLine() _T(" GetLastEntryLine() for Group '%s'"), Name().c_str() ); - if ( m_pLastEntry != 0 ) + if ( m_pLastEntry ) { wxFileConfigLineList *pLine = m_pLastEntry->GetLine(); - wxASSERT( pLine != 0 ); // last entry must have !NULL associated line + wxASSERT_MSG( pLine, _T("last entry must have !NULL associated line") ); + return pLine; } - // no entries: insert after the group header - + // no entries: insert after the group header, if any return GetGroupLine(); } +void wxFileConfigGroup::SetLastEntry(wxFileConfigEntry *pEntry) +{ + m_pLastEntry = pEntry; + + if ( !m_pLine ) + { + // the only situation in which a group without its own line can have + // an entry is when the first entry is added to the initially empty + // root pseudo-group + wxASSERT_MSG( !m_pParent, _T("unexpected for non root group") ); + + // let the group know that it does have a line in the file now + m_pLine = pEntry->GetLine(); + } +} + // ---------------------------------------------------------------------------- // group name // ---------------------------------------------------------------------------- void wxFileConfigGroup::Rename(const wxString& newName) { + wxCHECK_RET( m_pParent, _T("the root group can't be renamed") ); + m_strName = newName; - wxFileConfigLineList *line = GetGroupLine(); + // +1: no leading '/' wxString strFullName; - strFullName << wxT("[") << (GetFullName().c_str() + 1) << wxT("]"); // +1: no '/' + strFullName << wxT("[") << (GetFullName().c_str() + 1) << wxT("]"); + + wxFileConfigLineList *line = GetGroupLine(); + wxCHECK_RET( line, _T("a non root group must have a corresponding line!") ); + line->SetText(strFullName); SetDirty(); @@ -1795,17 +1822,19 @@ void wxFileConfigEntry::SetValue(const wxString& strValue, bool bUser) wxString strLine; strLine << FilterOutEntryName(m_strName) << wxT('=') << strValFiltered; - if ( m_pLine != 0 ) + if ( m_pLine ) { // entry was read from the local config file, just modify the line m_pLine->SetText(strLine); } - else { + else // this entry didn't exist in the local file + { // add a new line to the file wxASSERT( m_nLine == wxNOT_FOUND ); // consistency check - m_pLine = Group()->Config()->LineListInsert(strLine, - Group()->GetLastEntryLine()); + wxFileConfigLineList *line = Group()->GetLastEntryLine(); + m_pLine = Group()->Config()->LineListInsert(strLine, line); + Group()->SetLastEntry(this); }