From: Vadim Zeitlin Date: Fri, 1 Jul 2005 18:05:10 +0000 (+0000) Subject: fixed bug with HasGroup() creating groups as side effect X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/6c99cd3d8cf7038537947a8ae9fe2279190e5fdd fixed bug with HasGroup() creating groups as side effect git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34794 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/fileconf.h b/include/wx/fileconf.h index 5bb6b91fb1..92f0c42c37 100644 --- a/include/wx/fileconf.h +++ b/include/wx/fileconf.h @@ -209,6 +209,10 @@ private: // the same as SetPath("/") void SetRootPath(); + // real SetPath() implementation, returns true if path could be set or false + // if path doesn't exist and createMissingComponents == false + bool DoSetPath(const wxString& strPath, bool createMissingComponents); + // set/test the dirty flag void SetDirty() { m_isDirty = true; } void ResetDirty() { m_isDirty = false; } diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 2944b09995..4e44795799 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -746,13 +746,14 @@ void wxFileConfig::SetRootPath() m_pCurrentGroup = m_pRootGroup; } -void wxFileConfig::SetPath(const wxString& strPath) +bool +wxFileConfig::DoSetPath(const wxString& strPath, bool createMissingComponents) { wxArrayString aParts; if ( strPath.empty() ) { SetRootPath(); - return; + return true; } if ( strPath[0] == wxCONFIG_PATH_SEPARATOR ) { @@ -772,7 +773,13 @@ void wxFileConfig::SetPath(const wxString& strPath) for ( n = 0; n < aParts.Count(); n++ ) { wxFileConfigGroup *pNextGroup = m_pCurrentGroup->FindSubgroup(aParts[n]); if ( pNextGroup == NULL ) + { + if ( !createMissingComponents ) + return false; + pNextGroup = m_pCurrentGroup->AddSubgroup(aParts[n]); + } + m_pCurrentGroup = pNextGroup; } @@ -781,6 +788,13 @@ void wxFileConfig::SetPath(const wxString& strPath) for ( n = 0; n < aParts.Count(); n++ ) { m_strPath << wxCONFIG_PATH_SEPARATOR << aParts[n]; } + + return true; +} + +void wxFileConfig::SetPath(const wxString& strPath) +{ + DoSetPath(strPath, true /* create missing path components */); } // ---------------------------------------------------------------------------- @@ -857,10 +871,20 @@ size_t wxFileConfig::GetNumberOfGroups(bool bRecursive) const bool wxFileConfig::HasGroup(const wxString& strName) const { - wxConfigPathChanger path(this, strName); + // special case: DoSetPath("") does work as it's equivalent to DoSetPath("/") + // but there is no group with empty name so treat this separately + if ( strName.empty() ) + return false; + + const wxString pathOld = GetPath(); + + wxFileConfig *self = wx_const_cast(wxFileConfig *, this); + const bool + rc = self->DoSetPath(strName, false /* don't create missing components */); + + self->SetPath(pathOld); - wxFileConfigGroup *pGroup = m_pCurrentGroup->FindSubgroup(path.Name()); - return pGroup != NULL; + return rc; } bool wxFileConfig::HasEntry(const wxString& strName) const