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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "textfile.h"
20 #include "wx/wxprec.h"
26 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
28 #define wxUSE_TEXTFILE 0
34 #include "wx/string.h"
40 #include "wx/textfile.h"
42 // ============================================================================
43 // wxTextFile class implementation
44 // ============================================================================
46 wxTextFile::wxTextFile(const wxString
& strFileName
)
47 : wxTextBuffer(strFileName
)
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 bool wxTextFile::OnExists() const
58 return wxFile::Exists(m_strBufferName
);
62 bool wxTextFile::OnOpen(const wxString
&strBufferName
, wxTextBufferOpenMode OpenMode
)
64 wxFile::OpenMode FileOpenMode
;
69 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
73 FileOpenMode
= wxFile::read
;
77 FileOpenMode
= wxFile::write
;
81 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
85 bool wxTextFile::OnClose()
87 return m_file
.Close();
91 bool wxTextFile::OnRead(wxMBConv
& conv
)
93 // file should be opened and we must be in it's beginning
94 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
98 wchar_t conv_wcBuf
[2];
105 char ch
, chLast
= '\0';
109 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
110 if ( nRead
== wxInvalidOffset
) {
111 // read error (error message already given in wxFile::Read)
115 for ( n
= 0; n
< nRead
; n
++ ) {
119 // Dos/Unix line termination
120 AddLine(str
, chLast
== '\r' ? wxTextFileType_Dos
121 : wxTextFileType_Unix
);
127 if ( chLast
== '\r' ) {
129 AddLine(wxEmptyString
, wxTextFileType_Mac
);
136 if ( chLast
== '\r' ) {
137 // Mac line termination
138 AddLine(str
, wxTextFileType_Mac
);
142 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
150 // add to the current line
153 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
155 str
+= conv_wcBuf
[0];
162 } while ( nRead
== WXSIZEOF(buf
) );
164 // anything in the last line?
165 if ( !str
.IsEmpty() ) {
166 AddLine(str
, wxTextFileType_None
); // no line terminator
173 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
175 wxTempFile
fileTmp(m_strBufferName
);
177 if ( !fileTmp
.IsOpened() ) {
178 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
182 size_t nCount
= GetLineCount();
183 for ( size_t n
= 0; n
< nCount
; n
++ ) {
184 fileTmp
.Write(GetLine(n
) +
185 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
190 // replace the old file with this one
191 return fileTmp
.Commit();
194 #endif // wxUSE_TEXTFILE