X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/50257dba061011e25aa2a89b477c3b4c9da112a1..541ea80f0ec8a1250981bfaa3452f370e5c42c3b:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 229e7a8b8d..09a66cf6b1 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -245,6 +245,7 @@ public: // get the last line belonging to an entry/subgroup of this group wxFileConfigLineList *GetGroupLine(); // line which contains [group] + // may be NULL for "/" only wxFileConfigLineList *GetLastEntryLine(); // after which our subgroups start wxFileConfigLineList *GetLastGroupLine(); // after which the next group starts @@ -372,7 +373,16 @@ void wxFileConfig::Init() } else { - wxLogWarning(_("can't open user configuration file '%s'."), m_fnLocalFile.GetFullPath().c_str() ); + const wxString path = m_fnLocalFile.GetFullPath(); + wxLogWarning(_("can't open user configuration file '%s'."), + path.c_str()); + + if ( m_fnLocalFile.FileExists() ) + { + wxLogWarning(_("Changes won't be saved to avoid overwriting the existing file \"%s\""), + path.c_str()); + m_fnLocalFile.Clear(); + } } } @@ -437,70 +447,60 @@ wxFileConfig::wxFileConfig(wxInputStream &inStream, const wxMBConv& conv) m_linesTail = NULL; // read the entire stream contents in memory - wxString str; - { - static const size_t chunkLen = 1024; + wxWxCharBuffer cbuf; + static const size_t chunkLen = 1024; - wxMemoryBuffer buf(chunkLen); - do - { - inStream.Read(buf.GetAppendBuf(chunkLen), chunkLen); - buf.UngetAppendBuf(inStream.LastRead()); + wxMemoryBuffer buf(chunkLen); + do + { + inStream.Read(buf.GetAppendBuf(chunkLen), chunkLen); + buf.UngetAppendBuf(inStream.LastRead()); - const wxStreamError err = inStream.GetLastError(); + const wxStreamError err = inStream.GetLastError(); - if ( err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF ) - { - wxLogError(_("Error reading config options.")); - break; - } + if ( err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF ) + { + wxLogError(_("Error reading config options.")); + break; } - while ( !inStream.Eof() ); + } + while ( !inStream.Eof() ); #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.")); - } + size_t len; + cbuf = conv.cMB2WC((char *)buf.GetData(), buf.GetDataLen() + 1, &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()); + // no need for conversion + cbuf = wxCharBuffer::CreateNonOwned((char *)buf.GetData()); #endif // wxUSE_UNICODE/!wxUSE_UNICODE - } - // translate everything to the current (platform-dependent) line - // termination character - str = wxTextBuffer::Translate(str); - + // now break it into lines wxMemoryText memText; - - // Now we can add the text to the memory text. To do this we extract line - // by line from the translated string, until we've reached the end. - // - // VZ: all this is horribly inefficient, we should do the translation on - // the fly in one pass saving both memory and time (TODO) - - const wxChar *pEOL = wxTextBuffer::GetEOL(wxTextBuffer::typeDefault); - const size_t EOLLen = wxStrlen(pEOL); - - int posLineStart = str.Find(pEOL); - while ( posLineStart != -1 ) + for ( const wxChar *s = cbuf; ; ++s ) { - wxString line(str.Left(posLineStart)); + const wxChar *e = s; + while ( *e != '\0' && *e != '\n' && *e != '\r' ) + ++e; - memText.AddLine(line); + // notice that we throw away the original EOL kind here, maybe we + // should preserve it? + if ( e != s ) + memText.AddLine(wxString(s, e)); - str = str.Mid(posLineStart + EOLLen); + if ( *e == '\0' ) + break; - posLineStart = str.Find(pEOL); - } + // skip the second EOL byte if it's a DOS one + if ( *e == '\r' && e[1] == '\n' ) + ++e; - // also add whatever we have left in the translated string. - if ( !str.empty() ) - memText.AddLine(str); + s = e; + } // Finally we can parse it all. Parse(memText, true /* local */); @@ -551,16 +551,8 @@ void wxFileConfig::Parse(const wxTextBuffer& buffer, bool bLocal) // add the line to linked list if ( bLocal ) - { LineListAppend(strLine); - // let the root group have its start line as well - if ( !n ) - { - m_pCurrentGroup->SetLine(m_linesTail); - } - } - // skip leading spaces for ( pStart = buf; wxIsspace(*pStart); pStart++ ) @@ -918,6 +910,8 @@ bool wxFileConfig::DoReadLong(const wxString& key, long *pl) const return str.ToLong(pl); } +#if wxUSE_BASE64 + bool wxFileConfig::DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const { wxCHECK_MSG( buf, false, _T("NULL buffer") ); @@ -926,10 +920,12 @@ bool wxFileConfig::DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const if ( !Read(key, &str) ) return false; - *buf = wxBase64Decode(str.ToAscii()); + *buf = wxBase64Decode(str); return true; } +#endif // wxUSE_BASE64 + bool wxFileConfig::DoWriteString(const wxString& key, const wxString& szValue) { wxConfigPathChanger path(this, key); @@ -955,8 +951,8 @@ bool wxFileConfig::DoWriteString(const wxString& key, const wxString& szValue) SetDirty(); - // this will add a line for this group if it didn't have it before - + // this will add a line for this group if it didn't have it before (or + // do nothing for the root but it's ok as it always exists anyhow) (void)m_pCurrentGroup->GetGroupLine(); } else @@ -995,11 +991,15 @@ bool wxFileConfig::DoWriteLong(const wxString& key, long lValue) return Write(key, wxString::Format(_T("%ld"), lValue)); } +#if wxUSE_BASE64 + bool wxFileConfig::DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf) { return Write(key, wxBase64Encode(buf)); } +#endif // wxUSE_BASE64 + bool wxFileConfig::Flush(bool /* bCurrentOnly */) { if ( !IsDirty() || !m_fnLocalFile.GetFullPath() ) @@ -1312,9 +1312,6 @@ void wxFileConfig::LineListRemove(wxFileConfigLineList *pLine) else pNext->SetPrev(pPrev); - if ( m_pRootGroup->GetGroupLine() == pLine ) - m_pRootGroup->SetLine(m_linesHead); - wxLogTrace( FILECONF_TRACE_MASK, _T(" head: %s"), ((m_linesHead) ? (const wxChar*)m_linesHead->Text().c_str() @@ -1803,29 +1800,21 @@ bool wxFileConfigGroup::DeleteEntry(const wxString& name) // our last entry is being deleted - find the last one which stays wxASSERT( m_pLine != NULL ); // if we have an entry with !NULL pLine... - // go back until we find another entry or reach the group's line + // find the previous entry (if any) wxFileConfigEntry *pNewLast = NULL; - size_t n, nEntries = m_aEntries.GetCount(); - wxFileConfigLineList *pl; - for ( pl = pLine->Prev(); pl != m_pLine; pl = pl->Prev() ) { - // is it our subgroup? - for ( n = 0; (pNewLast == NULL) && (n < nEntries); n++ ) { - if ( m_aEntries[n]->GetLine() == m_pLine ) - pNewLast = m_aEntries[n]; - } - - if ( pNewLast != NULL ) // found? + const wxFileConfigLineList * const + pNewLastLine = m_pLastEntry->GetLine()->Prev(); + const size_t nEntries = m_aEntries.GetCount(); + for ( size_t n = 0; n < nEntries; n++ ) { + if ( m_aEntries[n]->GetLine() == pNewLastLine ) { + pNewLast = m_aEntries[n]; break; + } } - if ( pl == m_pLine ) { - wxASSERT( !pNewLast ); // how comes it has the same line as we? - - // we've reached the group line without finding any subgroups - m_pLastEntry = NULL; - } - else - m_pLastEntry = pNewLast; + // pNewLast can be NULL here -- it's ok and can happen if we have no + // entries left + m_pLastEntry = pNewLast; } m_pConfig->LineListRemove(pLine); @@ -1963,47 +1952,66 @@ int CompareGroups(wxFileConfigGroup *p1, wxFileConfigGroup *p2) // undo FilterOutValue static wxString FilterInValue(const wxString& str) { - wxString strResult; - strResult.Alloc(str.Len()); + wxString strResult; + if ( str.empty() ) + return strResult; - bool bQuoted = !str.empty() && str[0] == '"'; + strResult.reserve(str.length()); - for ( size_t n = bQuoted ? 1 : 0; n < str.Len(); n++ ) { - if ( str[n] == wxT('\\') ) { - switch ( str[++n].GetValue() ) { - case wxT('n'): - strResult += wxT('\n'); - break; + wxString::const_iterator i = str.begin(); + const bool bQuoted = *i == '"'; + if ( bQuoted ) + ++i; - case wxT('r'): - strResult += wxT('\r'); - break; + for ( const wxString::const_iterator end = str.end(); i != end; ++i ) + { + if ( *i == wxT('\\') ) + { + if ( ++i == end ) + { + wxLogWarning(_("trailing backslash ignored in '%s'"), str.c_str()); + break; + } - case wxT('t'): - strResult += wxT('\t'); - break; + switch ( (*i).GetValue() ) + { + case wxT('n'): + strResult += wxT('\n'); + break; - case wxT('\\'): - strResult += wxT('\\'); - break; + case wxT('r'): + strResult += wxT('\r'); + break; - case wxT('"'): - strResult += wxT('"'); - break; - } - } - else { - if ( str[n] != wxT('"') || !bQuoted ) - strResult += str[n]; - else if ( n != str.Len() - 1 ) { - wxLogWarning(_("unexpected \" at position %d in '%s'."), - n, str.c_str()); - } - //else: it's the last quote of a quoted string, ok + case wxT('t'): + strResult += wxT('\t'); + break; + + case wxT('\\'): + strResult += wxT('\\'); + break; + + case wxT('"'): + strResult += wxT('"'); + break; + } + } + else // not a backslash + { + if ( *i != wxT('"') || !bQuoted ) + { + strResult += *i; + } + else if ( i != end - 1 ) + { + wxLogWarning(_("unexpected \" at position %d in '%s'."), + i - str.begin(), str.c_str()); + } + //else: it's the last quote of a quoted string, ok + } } - } - return strResult; + return strResult; } // quote the string before writing it to file