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 licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #include "wx/wxprec.h"
22 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
24 #define wxUSE_TEXTFILE 0
30 #include "wx/string.h"
36 #include "wx/textfile.h"
37 #include "wx/filename.h"
39 // ============================================================================
40 // wxTextFile class implementation
41 // ============================================================================
43 wxTextFile::wxTextFile(const wxString
& strFileName
)
44 : wxTextBuffer(strFileName
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 bool wxTextFile::OnExists() const
55 return wxFile::Exists(m_strBufferName
);
59 bool wxTextFile::OnOpen(const wxString
&strBufferName
, wxTextBufferOpenMode OpenMode
)
61 wxFile::OpenMode FileOpenMode
;
66 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
70 FileOpenMode
= wxFile::read
;
74 FileOpenMode
= wxFile::write
;
78 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
82 bool wxTextFile::OnClose()
84 return m_file
.Close();
88 bool wxTextFile::OnRead(wxMBConv
& conv
)
90 // file should be opened and we must be in it's beginning
91 wxASSERT( m_file
.IsOpened() &&
92 (m_file
.GetKind() != wxFILE_KIND_DISK
|| m_file
.Tell() == 0) );
94 static const size_t BUF_SIZE
= 1024;
96 static const size_t NUL_SIZE
= 4;
98 static const size_t NUL_SIZE
= 1;
101 char buf
[BUF_SIZE
+ NUL_SIZE
];
102 wxChar chLast
= '\0';
107 // leave space for trailing NUL
108 ssize_t nRead
= m_file
.Read(buf
, BUF_SIZE
);
110 if ( nRead
== wxInvalidOffset
)
112 // read error (error message already given in wxFile::Read)
120 // we have to properly NUL-terminate the string for any encoding it may
121 // use -- 4 NULs should be enough for everyone (this is why we add 4
122 // extra bytes to the buffer)
126 buf
[nRead
+ 3] = '\0';
128 // append to the remains of the last block, don't overwrite
129 wxString
strbuf(buf
, conv
);
130 if ( strbuf
.empty() )
141 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
144 // the beginning of the current line, changes inside the loop
145 wxString::const_iterator lineStart
= str
.begin();
146 const wxString::const_iterator end
= str
.end();
147 for ( wxString::const_iterator p
= lineStart
; p
!= end
; p
++ )
149 const wxChar ch
= *p
;
153 // could be a DOS or Unix EOL
154 if ( chLast
== '\r' )
156 AddLine(wxString(lineStart
, p
- 1), wxTextFileType_Dos
);
158 else // bare '\n', Unix style
160 AddLine(wxString(lineStart
, p
), wxTextFileType_Unix
);
167 if ( chLast
== '\r' )
170 AddLine(wxEmptyString
, wxTextFileType_Mac
);
173 //else: we don't what this is yet -- could be a Mac EOL or
174 // start of DOS EOL so wait for next char
178 if ( chLast
== '\r' )
180 // Mac line termination
181 AddLine(wxString(lineStart
, p
- 1), wxTextFileType_Mac
);
189 // remove the part we already processed
190 str
.erase(0, lineStart
- str
.begin());
193 // anything in the last line?
196 AddLine(str
, wxTextFileType_None
); // no line terminator
203 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
205 wxFileName fn
= m_strBufferName
;
207 // We do NOT want wxPATH_NORM_CASE here, or the case will not
209 if ( !fn
.IsAbsolute() )
210 fn
.Normalize(wxPATH_NORM_ENV_VARS
| wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
|
211 wxPATH_NORM_ABSOLUTE
| wxPATH_NORM_LONG
);
213 wxTempFile
fileTmp(fn
.GetFullPath());
215 if ( !fileTmp
.IsOpened() ) {
216 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
220 size_t nCount
= GetLineCount();
221 for ( size_t n
= 0; n
< nCount
; n
++ ) {
222 fileTmp
.Write(GetLine(n
) +
223 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
228 // replace the old file with this one
229 return fileTmp
.Commit();
232 #endif // wxUSE_TEXTFILE