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
);
139 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
147 // add to the current line
149 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
151 str
+= conv_wcBuf
[0];
158 } while ( nRead
== WXSIZEOF(buf
) );
160 // anything in the last line?
161 if ( !str
.IsEmpty() ) {
162 AddLine(str
, wxTextFileType_None
); // no line terminator
169 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
171 wxTempFile
fileTmp(m_strBufferName
);
173 if ( !fileTmp
.IsOpened() ) {
174 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
178 size_t nCount
= GetLineCount();
179 for ( size_t n
= 0; n
< nCount
; n
++ ) {
180 fileTmp
.Write(GetLine(n
) +
181 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
186 // replace the old file with this one
187 return fileTmp
.Commit();
190 #endif // wxUSE_TEXTFILE