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"
38 #include "wx/buffer.h"
40 // ============================================================================
41 // wxTextFile class implementation
42 // ============================================================================
44 wxTextFile::wxTextFile(const wxString
& strFileName
)
45 : wxTextBuffer(strFileName
)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 bool wxTextFile::OnExists() const
56 return wxFile::Exists(m_strBufferName
);
60 bool wxTextFile::OnOpen(const wxString
&strBufferName
, wxTextBufferOpenMode OpenMode
)
62 wxFile::OpenMode FileOpenMode
;
67 wxFAIL_MSG( wxT("unknown open mode in wxTextFile::Open") );
71 FileOpenMode
= wxFile::read
;
75 FileOpenMode
= wxFile::write
;
79 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
83 bool wxTextFile::OnClose()
85 return m_file
.Close();
89 bool wxTextFile::OnRead(const wxMBConv
& conv
)
91 // file should be opened
92 wxASSERT_MSG( m_file
.IsOpened(), wxT("can't read closed file") );
94 // read the entire file in memory: this is not the most efficient thing to
95 // do it but there is no good way to avoid it in Unicode build because if
96 // we read the file block by block we can't convert each block to Unicode
97 // separately (the last multibyte char in the block might be only partially
98 // read and so the conversion would fail) and, as the file contents is kept
99 // in memory by wxTextFile anyhow, it shouldn't be a big problem to read
103 // number of bytes to (try to) read from disk at once
104 static const size_t BLOCK_SIZE
= 4096;
108 // first determine if the file is seekable or not and so whether we can
109 // determine its length in advance
110 wxFileOffset fileLength
;
113 fileLength
= m_file
.Length();
116 // some non-seekable files under /proc under Linux pretend that they're
117 // seekable but always return 0; others do return an error
118 const bool seekable
= fileLength
!= wxInvalidOffset
&& fileLength
!= 0;
121 // we know the required length, so set the buffer size in advance
122 bufSize
= fileLength
;
123 if ( !buf
.extend(bufSize
) )
126 // if the file is seekable, also check that we're at its beginning
127 wxASSERT_MSG( m_file
.Tell() == 0, wxT("should be at start of file") );
129 char *dst
= buf
.data();
130 for ( size_t nRemaining
= bufSize
; nRemaining
> 0; )
132 size_t nToRead
= BLOCK_SIZE
;
134 // the file size could have changed, avoid overflowing the buffer
136 if ( nToRead
> nRemaining
)
137 nToRead
= nRemaining
;
139 ssize_t nRead
= m_file
.Read(dst
, nToRead
);
141 if ( nRead
== wxInvalidOffset
)
143 // read error (error message already given in wxFile::Read)
149 // this file can't be empty because we checked for this above
150 // so this must be the end of file
158 wxASSERT_MSG( dst
- buf
.data() == (wxFileOffset
)bufSize
,
159 wxT("logic error") );
161 else // file is not seekable
163 char block
[BLOCK_SIZE
];
166 ssize_t nRead
= m_file
.Read(block
, WXSIZEOF(block
));
168 if ( nRead
== wxInvalidOffset
)
170 // read error (error message already given in wxFile::Read)
176 // if no bytes have been read, presumably this is a
177 // valid-but-empty file
181 // otherwise we've finished reading the file
185 // extend the buffer for new data
186 if ( !buf
.extend(bufSize
+ nRead
) )
189 // and append it to the buffer
190 memcpy(buf
.data() + bufSize
, block
, nRead
);
195 const wxString
str(buf
, conv
, bufSize
);
197 // there's no risk of this happening in ANSI build
199 if ( bufSize
> 4 && str
.empty() )
201 wxLogError(_("Failed to convert file \"%s\" to Unicode."), GetName());
204 #endif // wxUSE_UNICODE
206 // we don't need this memory any more
210 // now break the buffer in lines
212 // last processed character, we need to know if it was a CR or not
213 wxChar chLast
= '\0';
215 // the beginning of the current line, changes inside the loop
216 wxString::const_iterator lineStart
= str
.begin();
217 const wxString::const_iterator end
= str
.end();
218 for ( wxString::const_iterator p
= lineStart
; p
!= end
; p
++ )
220 const wxChar ch
= *p
;
224 // could be a DOS or Unix EOL
225 if ( chLast
== '\r' )
227 if ( p
- 1 >= lineStart
)
229 AddLine(wxString(lineStart
, p
- 1), wxTextFileType_Dos
);
233 // there were two line endings, so add an empty line:
234 AddLine(wxEmptyString
, wxTextFileType_Dos
);
237 else // bare '\n', Unix style
239 AddLine(wxString(lineStart
, p
), wxTextFileType_Unix
);
246 if ( chLast
== '\r' )
249 AddLine(wxEmptyString
, wxTextFileType_Mac
);
252 //else: we don't know what this is yet -- could be a Mac EOL or
253 // start of DOS EOL so wait for next char
257 if ( chLast
== '\r' )
259 // Mac line termination
260 if ( p
- 1 >= lineStart
)
262 AddLine(wxString(lineStart
, p
- 1), wxTextFileType_Mac
);
266 // there were two line endings, so add an empty line:
267 AddLine(wxEmptyString
, wxTextFileType_Mac
);
276 // anything in the last line?
277 if ( lineStart
!= end
)
279 // add unterminated last line
280 AddLine(wxString(lineStart
, end
), wxTextFileType_None
);
287 bool wxTextFile::OnWrite(wxTextFileType typeNew
, const wxMBConv
& conv
)
289 wxFileName fn
= m_strBufferName
;
291 // We do NOT want wxPATH_NORM_CASE here, or the case will not
293 if ( !fn
.IsAbsolute() )
294 fn
.Normalize(wxPATH_NORM_ENV_VARS
| wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
|
295 wxPATH_NORM_ABSOLUTE
| wxPATH_NORM_LONG
);
297 wxTempFile
fileTmp(fn
.GetFullPath());
299 if ( !fileTmp
.IsOpened() ) {
300 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
304 size_t nCount
= GetLineCount();
305 for ( size_t n
= 0; n
< nCount
; n
++ ) {
306 fileTmp
.Write(GetLine(n
) +
307 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
312 // replace the old file with this one
313 return fileTmp
.Commit();
316 #endif // wxUSE_TEXTFILE