1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textfile.cpp
3 // Purpose: implementation of wxTextFile class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #include "wx/wxprec.h"
22 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
24 #define wxUSE_TEXTFILE 0
30 #include "wx/string.h"
36 #include "wx/textfile.h"
37 #include "wx/filename.h"
39 // ============================================================================
40 // wxTextFile class implementation
41 // ============================================================================
43 wxTextFile::wxTextFile(const wxString
& strFileName
)
44 : wxTextBuffer(strFileName
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 bool wxTextFile::OnExists() const
55 return wxFile::Exists(m_strBufferName
);
59 bool wxTextFile::OnOpen(const wxString
&strBufferName
, wxTextBufferOpenMode OpenMode
)
61 wxFile::OpenMode FileOpenMode
;
66 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
70 FileOpenMode
= wxFile::read
;
74 FileOpenMode
= wxFile::write
;
78 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
82 bool wxTextFile::OnClose()
84 return m_file
.Close();
88 bool wxTextFile::OnRead(wxMBConv
& conv
)
90 // file should be opened and we must be in it's beginning
91 wxASSERT( m_file
.IsOpened() &&
92 (m_file
.GetKind() != wxFILE_KIND_DISK
|| m_file
.Tell() == 0) );
100 // leave space for trailing NUL
101 ssize_t nRead
= m_file
.Read(buf
, WXSIZEOF(buf
) - 1);
103 if ( nRead
== wxInvalidOffset
)
105 // read error (error message already given in wxFile::Read)
114 // append to the remains of the last block, don't overwrite
115 str
+= wxString(buf
, conv
);
117 // the beginning of the current line, changes inside the loop
118 const wxChar
*lineStart
= str
.begin();
119 const wxChar
* const end
= str
.end();
120 for ( const wxChar
*p
= lineStart
; p
!= end
; p
++ )
122 const wxChar ch
= *p
;
126 // could be a DOS or Unix EOL
127 if ( chLast
== '\r' )
129 AddLine(wxString(lineStart
, p
- 1), wxTextFileType_Dos
);
131 else // bare '\n', Unix style
133 AddLine(wxString(lineStart
, p
), wxTextFileType_Unix
);
140 if ( chLast
== '\r' )
143 AddLine(wxEmptyString
, wxTextFileType_Mac
);
146 //else: we don't what this is yet -- could be a Mac EOL or
147 // start of DOS EOL so wait for next char
151 if ( chLast
== '\r' )
153 // Mac line termination
154 AddLine(wxString(lineStart
, p
- 1), wxTextFileType_Mac
);
162 // remove the part we already processed
163 str
.erase(0, lineStart
- str
.begin());
166 // anything in the last line?
169 AddLine(str
, wxTextFileType_None
); // no line terminator
176 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
178 wxFileName fn
= m_strBufferName
;
180 // We do NOT want wxPATH_NORM_CASE here, or the case will not
182 if ( !fn
.IsAbsolute() )
183 fn
.Normalize(wxPATH_NORM_ENV_VARS
| wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
|
184 wxPATH_NORM_ABSOLUTE
| wxPATH_NORM_LONG
);
186 wxTempFile
fileTmp(fn
.GetFullPath());
188 if ( !fileTmp
.IsOpened() ) {
189 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
193 size_t nCount
= GetLineCount();
194 for ( size_t n
= 0; n
< nCount
; n
++ ) {
195 fileTmp
.Write(GetLine(n
) +
196 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
201 // replace the old file with this one
202 return fileTmp
.Commit();
205 #endif // wxUSE_TEXTFILE