X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/22f3361e1cf25c52a2da8fdfc5cb081809e18fb9..a631300cb83b23107f5b61bd96a97a6ef05cc122:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 46a5dc8e1d..3e39151ade 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -7,7 +7,7 @@ // RCS-ID: $Id$ // Copyright: (c) 1997 Karsten Ballüder & Vadim Zeitlin // Ballueder@usa.net -// Licence: wxWindows license +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ @@ -250,10 +250,11 @@ 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; } - DECLARE_NO_COPY_CLASS(wxFileConfigGroup) + DECLARE_NO_COPY_CLASS(wxFileConfigGroup) }; // ============================================================================ @@ -331,7 +332,7 @@ wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) if ( wxStrchr(szFile, wxT('.')) == NULL ) #if defined( __WXMAC__ ) - str << " Preferences"; + str << wxT(" Preferences") ; #elif defined( __UNIX__ ) str << wxT(".conf"); #else // Windows @@ -343,7 +344,7 @@ wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) { -#ifdef __VMS__ +#ifdef __VMS__ // On VMS I saw the problem that the home directory was appended // twice for the configuration file. Does that also happen for // other platforms? @@ -364,7 +365,7 @@ wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) #endif #ifdef __WXMAC__ - str << " Preferences"; + str << wxT(" Preferences") ; #endif return str; @@ -389,11 +390,7 @@ void wxFileConfig::Init() { wxTextFile fileGlobal(m_strGlobalFile); -#if defined(__WXGTK20__) && wxUSE_UNICODE - if ( fileGlobal.Open( wxConvUTF8 ) ) -#else - if ( fileGlobal.Open() ) -#endif + if ( fileGlobal.Open(m_conv/*ignored in ANSI build*/) ) { Parse(fileGlobal, FALSE /* global */); SetRootPath(); @@ -408,11 +405,7 @@ void wxFileConfig::Init() if ( !m_strLocalFile.IsEmpty() && wxFile::Exists(m_strLocalFile) ) { wxTextFile fileLocal(m_strLocalFile); -#if defined(__WXGTK20__) && wxUSE_UNICODE - if ( fileLocal.Open( wxConvUTF8 ) ) -#else - if ( fileLocal.Open() ) -#endif + if ( fileLocal.Open(m_conv/*ignored in ANSI build*/) ) { Parse(fileLocal, TRUE /* local */); SetRootPath(); @@ -427,11 +420,12 @@ void wxFileConfig::Init() // constructor supports creation of wxFileConfig objects of any type wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, const wxString& strLocal, const wxString& strGlobal, - long style) + long style, wxMBConv& conv) : wxConfigBase(::GetAppName(appName), vendorName, strLocal, strGlobal, style), - m_strLocalFile(strLocal), m_strGlobalFile(strGlobal) + m_strLocalFile(strLocal), m_strGlobalFile(strGlobal), + m_conv(conv) { // Make up names for files if empty if ( m_strLocalFile.IsEmpty() && (style & wxCONFIG_USE_LOCAL_FILE) ) @@ -468,13 +462,14 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, } SetUmask(-1); - + Init(); } #if wxUSE_STREAMS -wxFileConfig::wxFileConfig(wxInputStream &inStream) +wxFileConfig::wxFileConfig(wxInputStream &inStream, wxMBConv& conv) + : m_conv(conv) { // always local_file when this constructor is called (?) SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE); @@ -564,15 +559,24 @@ void wxFileConfig::Parse(wxTextBuffer& buffer, bool bLocal) wxString strLine; size_t nLineCount = buffer.GetLineCount(); - + for ( size_t n = 0; n < nLineCount; n++ ) { strLine = buffer[n]; // 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++ ) ; @@ -668,6 +672,16 @@ void wxFileConfig::Parse(wxTextBuffer& buffer, bool bLocal) // new entry pEntry = m_pCurrentGroup->AddEntry(strKey, n); + // + // Take the opportunity to set some pointers now + // that we know there are items in this group. + // Otherwise, items added to a newly read file + // can be put in the wrong place. + m_pCurrentGroup->SetLastEntry(pEntry); + if (m_pCurrentGroup->Parent()) + m_pCurrentGroup->Parent()->SetLastGroup(m_pCurrentGroup); + // + if ( bLocal ) pEntry->SetLine(m_linesTail); } @@ -861,19 +875,21 @@ bool wxFileConfig::DoReadString(const wxString& key, wxString* pStr) const bool wxFileConfig::DoReadLong(const wxString& key, long *pl) const { - wxString str; - if ( !Read(key, & str) ) - { - return FALSE; - } - return str.ToLong(pl) ; + wxString str; + if ( !Read(key, &str) ) + return FALSE; + + // extra spaces shouldn't prevent us from reading numeric values + str.Trim(); + + return str.ToLong(pl); } bool wxFileConfig::DoWriteString(const wxString& key, const wxString& szValue) { wxConfigPathChanger path(this, key); wxString strName = path.Name(); - + wxLogTrace( _T("wxFileConfig"), _T(" Writing String '%s' = '%s' to Group '%s'"), strName.c_str(), @@ -961,12 +977,7 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) { wxString line = p->Text(); line += wxTextFile::GetEOL(); -#if wxUSE_UNICODE - wxCharBuffer buf = wxConvLocal.cWX2MB( line ); - if ( !file.Write( (const char*)buf, strlen( (const char*) buf ) ) ) -#else - if ( !file.Write( line.c_str(), line.Len() ) ) -#endif + if ( !file.Write(line, m_conv) ) { wxLogError(_("can't write user configuration file.")); return FALSE; @@ -1133,8 +1144,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) { @@ -1313,16 +1323,15 @@ 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") ); wxFileConfigGroup *pParent = Parent(); - // this group wasn't present in local config file, add it now - - if ( pParent != 0 ) + // this group wasn't present in local config file, add it now + if ( pParent ) { wxLogTrace( _T("wxFileConfig"), _T(" checking parent '%s'"), @@ -1330,18 +1339,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; @@ -1352,19 +1359,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) - + // no subgroups, so the last line is the line of thelast entry (if any) return GetLastEntryLine(); } @@ -1377,30 +1383,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(); @@ -1518,13 +1546,17 @@ wxFileConfigGroup *wxFileConfigGroup::AddSubgroup(const wxString& strName) bool wxFileConfigGroup::DeleteSubgroupByName(const wxChar *szName) { - return DeleteSubgroup(FindSubgroup(szName)); + wxFileConfigGroup * const pGroup = FindSubgroup(szName); + + return pGroup ? DeleteSubgroup(pGroup) : FALSE; } // Delete the subgroup and remove all references to it from // other data structures. bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup *pGroup) { + wxCHECK_MSG( pGroup, FALSE, _T("deleting non existing group?") ); + wxLogTrace( _T("wxFileConfig"), _T("Deleting group '%s' from '%s'"), pGroup->Name().c_str(), @@ -1539,11 +1571,8 @@ bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup *pGroup) _T(" text: '%s'"), ((m_pLine) ? m_pLine->Text().c_str() : wxEmptyString) ); - wxCHECK_MSG( pGroup != 0, FALSE, _T("deleting non existing group?") ); - - // delete all entries - - size_t nCount = pGroup->m_aEntries.Count(); + // delete all entries + size_t nCount = pGroup->m_aEntries.Count(); wxLogTrace(_T("wxFileConfig"), _T("Removing %lu Entries"), @@ -1627,7 +1656,7 @@ bool wxFileConfigGroup::DeleteSubgroup(wxFileConfigGroup *pGroup) { wxLogTrace( _T("wxFileConfig"), _T(" ------- No previous group found -------") ); - + wxASSERT_MSG( !pNewLast || m_pLine == 0, _T("how comes it has the same line as we?") ); @@ -1800,17 +1829,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); }