X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/008202b7e900e475422dd39e6248249da5192feb..c43507b0fed02bf204b524b953ce7c9794ed250f:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index cc4823e058..09731e4194 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -328,12 +328,12 @@ wxString wxFileConfig::GetLocalDir() return strDir; } -wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) +wxString wxFileConfig::GetGlobalFileName(const wxString& file) { wxString str = GetGlobalDir(); - str << szFile; + str << file; - if ( wxStrchr(szFile, wxT('.')) == NULL ) + if ( wxStrchr(file, wxT('.')) == NULL ) #if defined( __WXMAC__ ) str << wxT(" Preferences") ; #elif defined( __UNIX__ ) @@ -345,7 +345,7 @@ wxString wxFileConfig::GetGlobalFileName(const wxChar *szFile) return str; } -wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) +wxString wxFileConfig::GetLocalFileName(const wxString& file) { #ifdef __VMS__ // On VMS I saw the problem that the home directory was appended @@ -360,10 +360,10 @@ wxString wxFileConfig::GetLocalFileName(const wxChar *szFile) str << wxT('.'); #endif - str << szFile; + str << file; #if defined(__WINDOWS__) || defined(__DOS__) - if ( wxStrchr(szFile, wxT('.')) == NULL ) + if ( wxStrchr(file, wxT('.')) == NULL ) str << wxT(".ini"); #endif @@ -435,7 +435,13 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, { // Make up names for files if empty if ( m_strLocalFile.empty() && (style & wxCONFIG_USE_LOCAL_FILE) ) + { m_strLocalFile = GetLocalFileName(GetAppName()); +#if defined(__UNIX__) && !defined(__VMS) + if ( style & wxCONFIG_USE_SUBDIR ) + m_strLocalFile << wxFILE_SEP_PATH << GetAppName() << _T(".conf"); +#endif + } if ( m_strGlobalFile.empty() && (style & wxCONFIG_USE_GLOBAL_FILE) ) m_strGlobalFile = GetGlobalFileName(GetAppName()); @@ -486,16 +492,16 @@ wxFileConfig::wxFileConfig(wxInputStream &inStream, const wxMBConv& conv) m_linesHead = m_linesTail = NULL; - // translate everything to the current (platform-dependent) line - // termination character - wxString strTrans; + // read the entire stream contents in memory + wxString str; { - wxString strTmp; + static const size_t chunkLen = 1024; - char buf[1024]; + wxMemoryBuffer buf(chunkLen); do { - inStream.Read(buf, WXSIZEOF(buf)-1); // leave room for the NULL + inStream.Read(buf.GetAppendBuf(chunkLen), chunkLen); + buf.UngetAppendBuf(inStream.LastRead()); const wxStreamError err = inStream.GetLastError(); @@ -504,19 +510,27 @@ wxFileConfig::wxFileConfig(wxInputStream &inStream, const wxMBConv& conv) wxLogError(_("Error reading config options.")); break; } - - // FIXME: this is broken because if we have part of multibyte - // character in the buffer (and another part hasn't been - // read yet) we're going to lose data because of conversion - // errors - buf[inStream.LastRead()] = '\0'; - strTmp += conv.cMB2WX(buf); } while ( !inStream.Eof() ); - strTrans = wxTextBuffer::Translate(strTmp); +#if wxUSE_UNICODE + size_t len; + str = conv.cMB2WC((char *)buf.GetData(), buf.GetDataLen(), &len); + if ( !len && buf.GetDataLen() ) + { + wxLogError(_("Failed to read config options.")); + } +#else // !wxUSE_UNICODE + // no need for conversion + str.assign((char *)buf.GetData(), buf.GetDataLen()); +#endif // wxUSE_UNICODE/!wxUSE_UNICODE } + + // translate everything to the current (platform-dependent) line + // termination character + str = wxTextBuffer::Translate(str); + wxMemoryText memText; // Now we can add the text to the memory text. To do this we extract line @@ -528,21 +542,21 @@ wxFileConfig::wxFileConfig(wxInputStream &inStream, const wxMBConv& conv) const wxChar *pEOL = wxTextBuffer::GetEOL(wxTextBuffer::typeDefault); const size_t EOLLen = wxStrlen(pEOL); - int posLineStart = strTrans.Find(pEOL); + int posLineStart = str.Find(pEOL); while ( posLineStart != -1 ) { - wxString line(strTrans.Left(posLineStart)); + wxString line(str.Left(posLineStart)); memText.AddLine(line); - strTrans = strTrans.Mid(posLineStart + EOLLen); + str = str.Mid(posLineStart + EOLLen); - posLineStart = strTrans.Find(pEOL); + posLineStart = str.Find(pEOL); } // also add whatever we have left in the translated string. - if ( !strTrans.empty() ) - memText.AddLine(strTrans); + if ( !str.empty() ) + memText.AddLine(str); // Finally we can parse it all. Parse(memText, true /* local */); @@ -1041,15 +1055,17 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) } // write all strings to file + wxString filetext; + filetext.reserve(4096); for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() ) { - wxString line = p->Text(); - line += wxTextFile::GetEOL(); - if ( !file.Write(line, *m_conv) ) - { - wxLogError(_("can't write user configuration file.")); - return false; - } + filetext << p->Text() << wxTextFile::GetEOL(); + } + + if ( !file.Write(filetext, *m_conv) ) + { + wxLogError(_("can't write user configuration file.")); + return false; } if ( !file.Commit() ) @@ -1172,7 +1188,7 @@ bool wxFileConfig::DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso) bool wxFileConfig::DeleteGroup(const wxString& key) { - wxConfigPathChanger path(this, key); + wxConfigPathChanger path(this, RemoveTrailingSeparator(key)); if ( !m_pCurrentGroup->DeleteSubgroupByName(path.Name()) ) return false; @@ -1547,8 +1563,17 @@ void wxFileConfigGroup::Rename(const wxString& newName) { wxCHECK_RET( m_pParent, _T("the root group can't be renamed") ); + if ( newName == m_strName ) + return; + + // we need to remove the group from the parent and it back under the new + // name to keep the parents array of subgroups alphabetically sorted + m_pParent->m_aSubgroups.Remove(this); + m_strName = newName; + m_pParent->m_aSubgroups.Add(this); + // update the group lines recursively UpdateGroupAndSubgroupsLines(); }