X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/68c97af3c39f294259ae5e16633f573ca729bfa1..f7570f5771dc963279ad2745fcb2fa1ca8eb0763:/src/common/textfile.cpp diff --git a/src/common/textfile.cpp b/src/common/textfile.cpp index 26053922cd..9bf2947f43 100644 --- a/src/common/textfile.cpp +++ b/src/common/textfile.cpp @@ -6,17 +6,13 @@ // Created: 03.04.98 // RCS-ID: $Id$ // Copyright: (c) 1998 Vadim Zeitlin -// Licence: wxWindows license +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// // ============================================================================ // headers // ============================================================================ -#ifdef __GNUG__ - #pragma implementation "textfile.h" -#endif - #include "wx/wxprec.h" #ifdef __BORLANDC__ @@ -91,97 +87,134 @@ bool wxTextFile::OnClose() bool wxTextFile::OnRead(wxMBConv& conv) { - // file should be opened and we must be in it's beginning - wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); + // file should be opened and we must be in it's beginning + wxASSERT( m_file.IsOpened() && + (m_file.GetKind() != wxFILE_KIND_DISK || m_file.Tell() == 0) ); + static const size_t BUF_SIZE = 1024; #if wxUSE_UNICODE - char conv_mbBuf[2]; - wchar_t conv_wcBuf[2]; - conv_mbBuf[1] = 0; + static const size_t NUL_SIZE = 4; #else - (void)conv; + static const size_t NUL_SIZE = 1; #endif - wxString str; - char ch, chLast = '\0'; - char buf[1024]; - int n, nRead; - do { - nRead = m_file.Read(buf, WXSIZEOF(buf)); - if ( nRead == wxInvalidOffset ) { - // read error (error message already given in wxFile::Read) - return FALSE; - } + char buf[BUF_SIZE + NUL_SIZE]; + wxChar chLast = '\0'; + wxString str; - for ( n = 0; n < nRead; n++ ) { - ch = buf[n]; - switch ( ch ) { - case '\n': - // Dos/Unix line termination - AddLine(str, chLast == '\r' ? wxTextFileType_Dos - : wxTextFileType_Unix); - str.Empty(); - chLast = '\n'; - break; - - case '\r': - if ( chLast == '\r' ) { - // Mac empty line - AddLine(wxEmptyString, wxTextFileType_Mac); - } - else - chLast = '\r'; - break; + for ( ;; ) + { + // leave space for trailing NUL + ssize_t nRead = m_file.Read(buf, BUF_SIZE); + + if ( nRead == wxInvalidOffset ) + { + // read error (error message already given in wxFile::Read) + return false; + } + + if ( nRead == 0 ) + break; - default: - if ( chLast == '\r' ) { - // Mac line termination - AddLine(str, wxTextFileType_Mac); - chLast = ch; -#if wxUSE_UNICODE - conv_mbBuf[0] = ch; - if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) - conv_wcBuf[0] = ch; - str = conv_wcBuf[0]; -#else - str = ch; -#endif - } - else { - // add to the current line #if wxUSE_UNICODE - conv_mbBuf[0] = ch; - if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) - conv_wcBuf[0] = ch; - str += conv_wcBuf[0]; -#else - str += ch; -#endif - } - } + // we have to properly NUL-terminate the string for any encoding it may + // use -- 4 NULs should be enough for everyone (this is why we add 4 + // extra bytes to the buffer) + buf[nRead] = + buf[nRead + 1] = + buf[nRead + 2] = + buf[nRead + 3] = '\0'; + + // append to the remains of the last block, don't overwrite + wxString strbuf(buf, conv); + if ( strbuf.empty() ) + { + // conversion failed + return false; + } + + str += strbuf; +#else // ANSI + wxUnusedVar(conv); + buf[nRead] = '\0'; + str += buf; +#endif // wxUSE_UNICODE/!wxUSE_UNICODE + + + // the beginning of the current line, changes inside the loop + wxString::const_iterator lineStart = str.begin(); + const wxString::const_iterator end = str.end(); + for ( wxString::const_iterator p = lineStart; p != end; p++ ) + { + const wxChar ch = *p; + switch ( ch ) + { + case '\n': + // could be a DOS or Unix EOL + if ( chLast == '\r' ) + { + AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos); + } + else // bare '\n', Unix style + { + AddLine(wxString(lineStart, p), wxTextFileType_Unix); + } + + lineStart = p + 1; + break; + + case '\r': + if ( chLast == '\r' ) + { + // Mac empty line + AddLine(wxEmptyString, wxTextFileType_Mac); + lineStart = p + 1; + } + //else: we don't what this is yet -- could be a Mac EOL or + // start of DOS EOL so wait for next char + break; + + default: + if ( chLast == '\r' ) + { + // Mac line termination + AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac); + lineStart = p; + } + } + + chLast = ch; + } + + // remove the part we already processed + str.erase(0, lineStart - str.begin()); } - } while ( nRead == WXSIZEOF(buf) ); - // anything in the last line? - if ( !str.IsEmpty() ) { - AddLine(str, wxTextFileType_None); // no line terminator - } + // anything in the last line? + if ( !str.empty() ) + { + AddLine(str, wxTextFileType_None); // no line terminator + } - return TRUE; + return true; } bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv) { wxFileName fn = m_strBufferName; + + // We do NOT want wxPATH_NORM_CASE here, or the case will not + // be preserved. if ( !fn.IsAbsolute() ) - fn.Normalize(); + fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | + wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG); - wxTempFile fileTmp(fn.GetFullName()); + wxTempFile fileTmp(fn.GetFullPath()); if ( !fileTmp.IsOpened() ) { wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str()); - return FALSE; + return false; } size_t nCount = GetLineCount();