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"
41 #include "wx/filename.h"
43 // ============================================================================
44 // wxTextFile class implementation
45 // ============================================================================
47 wxTextFile::wxTextFile(const wxString
& strFileName
)
48 : wxTextBuffer(strFileName
)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 bool wxTextFile::OnExists() const
59 return wxFile::Exists(m_strBufferName
);
63 bool wxTextFile::OnOpen(const wxString
&strBufferName
, wxTextBufferOpenMode OpenMode
)
65 wxFile::OpenMode FileOpenMode
;
70 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
74 FileOpenMode
= wxFile::read
;
78 FileOpenMode
= wxFile::write
;
82 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
86 bool wxTextFile::OnClose()
88 return m_file
.Close();
92 bool wxTextFile::OnRead(wxMBConv
& conv
)
94 // file should be opened and we must be in it's beginning
95 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
99 wchar_t conv_wcBuf
[2];
106 char ch
, chLast
= '\0';
110 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
111 if ( nRead
== wxInvalidOffset
) {
112 // read error (error message already given in wxFile::Read)
116 for ( n
= 0; n
< nRead
; n
++ ) {
120 // Dos/Unix line termination
121 AddLine(str
, chLast
== '\r' ? wxTextFileType_Dos
122 : wxTextFileType_Unix
);
128 if ( chLast
== '\r' ) {
130 AddLine(wxEmptyString
, wxTextFileType_Mac
);
137 if ( chLast
== '\r' ) {
138 // Mac line termination
139 AddLine(str
, wxTextFileType_Mac
);
143 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
151 // add to the current line
154 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
156 str
+= conv_wcBuf
[0];
163 } while ( nRead
== WXSIZEOF(buf
) );
165 // anything in the last line?
166 if ( !str
.IsEmpty() ) {
167 AddLine(str
, wxTextFileType_None
); // no line terminator
174 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
176 wxFileName fn
= m_strBufferName
;
177 if ( !fn
.IsAbsolute() )
180 wxTempFile
fileTmp(fn
.GetFullPath());
182 if ( !fileTmp
.IsOpened() ) {
183 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
187 size_t nCount
= GetLineCount();
188 for ( size_t n
= 0; n
< nCount
; n
++ ) {
189 fileTmp
.Write(GetLine(n
) +
190 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
195 // replace the old file with this one
196 return fileTmp
.Commit();
199 #endif // wxUSE_TEXTFILE