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
;
69 FileOpenMode
= wxFile::read
;
72 FileOpenMode
= wxFile::write
;
75 wxASSERT(0); // Should not happen.
79 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
83 bool wxTextFile::OnClose()
85 return m_file
.Close();
89 bool wxTextFile::OnRead(wxMBConv
& conv
)
91 // file should be opened and we must be in it's beginning
92 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
96 wchar_t conv_wcBuf
[2];
103 char ch
, chLast
= '\0';
107 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
108 if ( nRead
== wxInvalidOffset
) {
109 // read error (error message already given in wxFile::Read)
113 for ( n
= 0; n
< nRead
; n
++ ) {
117 // Dos/Unix line termination
118 AddLine(str
, chLast
== '\r' ? wxTextFileType_Dos
119 : wxTextFileType_Unix
);
125 if ( chLast
== '\r' ) {
127 AddLine(wxEmptyString
, wxTextFileType_Mac
);
134 if ( chLast
== '\r' ) {
135 // Mac line termination
136 AddLine(str
, wxTextFileType_Mac
);
140 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
148 // add to the current line
151 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
153 str
+= conv_wcBuf
[0];
160 } while ( nRead
== WXSIZEOF(buf
) );
162 // anything in the last line?
163 if ( !str
.IsEmpty() ) {
164 AddLine(str
, wxTextFileType_None
); // no line terminator
171 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
173 wxTempFile
fileTmp(m_strBufferName
);
175 if ( !fileTmp
.IsOpened() ) {
176 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
180 size_t nCount
= GetLineCount();
181 for ( size_t n
= 0; n
< nCount
; n
++ ) {
182 fileTmp
.Write(GetLine(n
) +
183 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
188 // replace the old file with this one
189 return fileTmp
.Commit();
192 #endif // wxUSE_TEXTFILE