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
= wxFile::read
;
70 FileOpenMode
= wxFile::read
;
73 FileOpenMode
= wxFile::write
;
76 wxASSERT(nAssertVal
); // Should not happen.
80 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
84 bool wxTextFile::OnClose()
86 return m_file
.Close();
90 bool wxTextFile::OnRead(wxMBConv
& conv
)
92 // file should be opened and we must be in it's beginning
93 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
97 wchar_t conv_wcBuf
[2];
104 char ch
, chLast
= '\0';
108 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
109 if ( nRead
== wxInvalidOffset
) {
110 // read error (error message already given in wxFile::Read)
114 for ( n
= 0; n
< nRead
; n
++ ) {
118 // Dos/Unix line termination
119 AddLine(str
, chLast
== '\r' ? wxTextFileType_Dos
120 : wxTextFileType_Unix
);
126 if ( chLast
== '\r' ) {
128 AddLine(wxEmptyString
, wxTextFileType_Mac
);
135 if ( chLast
== '\r' ) {
136 // Mac line termination
137 AddLine(str
, wxTextFileType_Mac
);
141 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
149 // add to the current line
152 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
154 str
+= conv_wcBuf
[0];
161 } while ( nRead
== WXSIZEOF(buf
) );
163 // anything in the last line?
164 if ( !str
.IsEmpty() ) {
165 AddLine(str
, wxTextFileType_None
); // no line terminator
172 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
174 wxTempFile
fileTmp(m_strBufferName
);
176 if ( !fileTmp
.IsOpened() ) {
177 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
181 size_t nCount
= GetLineCount();
182 for ( size_t n
= 0; n
< nCount
; n
++ ) {
183 fileTmp
.Write(GetLine(n
) +
184 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
189 // replace the old file with this one
190 return fileTmp
.Commit();
193 #endif // wxUSE_TEXTFILE