]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
avoid extra reallocations and memcpy for seekable files in OnRead() (slightly modifie...
[wxWidgets.git] / src / common / textfile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textfile.cpp
3 // Purpose: implementation of wxTextFile class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 03.04.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers
14 // ============================================================================
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif //__BORLANDC__
21
22 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
23 #undef wxUSE_TEXTFILE
24 #define wxUSE_TEXTFILE 0
25 #endif // wxUSE_FILE
26
27 #if wxUSE_TEXTFILE
28
29 #ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #include "wx/intl.h"
32 #include "wx/file.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/textfile.h"
37 #include "wx/filename.h"
38 #include "wx/buffer.h"
39
40 // ============================================================================
41 // wxTextFile class implementation
42 // ============================================================================
43
44 wxTextFile::wxTextFile(const wxString& strFileName)
45 : wxTextBuffer(strFileName)
46 {
47 }
48
49
50 // ----------------------------------------------------------------------------
51 // file operations
52 // ----------------------------------------------------------------------------
53
54 bool wxTextFile::OnExists() const
55 {
56 return wxFile::Exists(m_strBufferName);
57 }
58
59
60 bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode OpenMode)
61 {
62 wxFile::OpenMode FileOpenMode;
63
64 switch ( OpenMode )
65 {
66 default:
67 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
68 // fall through
69
70 case ReadAccess :
71 FileOpenMode = wxFile::read;
72 break;
73
74 case WriteAccess :
75 FileOpenMode = wxFile::write;
76 break;
77 }
78
79 return m_file.Open(strBufferName.c_str(), FileOpenMode);
80 }
81
82
83 bool wxTextFile::OnClose()
84 {
85 return m_file.Close();
86 }
87
88
89 bool wxTextFile::OnRead(const wxMBConv& conv)
90 {
91 // file should be opened
92 wxASSERT_MSG( m_file.IsOpened(), _T("can't read closed file") );
93
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
100 // the file entirely
101 size_t bufSize = 0;
102
103 // number of bytes to (try to) read from disk at once
104 static const size_t BLOCK_SIZE = 4096;
105
106 wxCharBuffer buf;
107
108 // first determine if the file is seekable or not and so whether we can
109 // determine its length in advance
110 wxFileOffset fileLength;
111 {
112 wxLogNull logNull;
113 fileLength = m_file.Length();
114 }
115
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;
119 if ( seekable )
120 {
121 // we know the required length, so set the buffer size in advance
122 bufSize = fileLength;
123 if ( !buf.extend(bufSize) )
124 return false;
125
126 // if the file is seekable, also check that we're at its beginning
127 wxASSERT_MSG( m_file.Tell() == 0, _T("should be at start of file") );
128
129 char *dst = buf.data();
130 for ( ;; )
131 {
132 ssize_t nRead = m_file.Read(dst, BLOCK_SIZE);
133
134 if ( nRead == wxInvalidOffset )
135 {
136 // read error (error message already given in wxFile::Read)
137 return false;
138 }
139
140 if ( nRead == 0 )
141 {
142 // this file can't be empty because we checked for this above
143 break;
144 }
145
146 dst += nRead;
147 }
148
149 wxASSERT_MSG( dst - buf.data() == (wxFileOffset)bufSize,
150 _T("logic error") );
151 }
152 else // file is not seekable
153 {
154 char block[BLOCK_SIZE];
155 for ( ;; )
156 {
157 ssize_t nRead = m_file.Read(block, WXSIZEOF(block));
158
159 if ( nRead == wxInvalidOffset )
160 {
161 // read error (error message already given in wxFile::Read)
162 return false;
163 }
164
165 if ( nRead == 0 )
166 {
167 // if no bytes have been read, presumably this is a
168 // valid-but-empty file
169 if ( bufSize == 0 )
170 return true;
171
172 // otherwise we've finished reading the file
173 break;
174 }
175
176 // extend the buffer for new data
177 if ( !buf.extend(bufSize + nRead) )
178 return false;
179
180 // and append it to the buffer
181 memcpy(buf.data() + bufSize, block, nRead);
182 bufSize += nRead;
183 }
184 }
185
186 const wxString str(buf, conv, bufSize);
187
188 // there's no risk of this happening in ANSI build
189 #if wxUSE_UNICODE
190 if ( bufSize > 4 && str.empty() )
191 {
192 wxLogError(_("Failed to convert file \"%s\" to Unicode."), GetName());
193 return false;
194 }
195 #endif // wxUSE_UNICODE
196
197 // we don't need this memory any more
198 buf.reset();
199
200
201 // now break the buffer in lines
202
203 // last processed character, we need to know if it was a CR or not
204 wxChar chLast = '\0';
205
206 // the beginning of the current line, changes inside the loop
207 wxString::const_iterator lineStart = str.begin();
208 const wxString::const_iterator end = str.end();
209 for ( wxString::const_iterator p = lineStart; p != end; p++ )
210 {
211 const wxChar ch = *p;
212 switch ( ch )
213 {
214 case '\n':
215 // could be a DOS or Unix EOL
216 if ( chLast == '\r' )
217 {
218 if ( p - 1 >= lineStart )
219 {
220 AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos);
221 }
222 else
223 {
224 // there were two line endings, so add an empty line:
225 AddLine(wxEmptyString, wxTextFileType_Dos);
226 }
227 }
228 else // bare '\n', Unix style
229 {
230 AddLine(wxString(lineStart, p), wxTextFileType_Unix);
231 }
232
233 lineStart = p + 1;
234 break;
235
236 case '\r':
237 if ( chLast == '\r' )
238 {
239 // Mac empty line
240 AddLine(wxEmptyString, wxTextFileType_Mac);
241 lineStart = p + 1;
242 }
243 //else: we don't know what this is yet -- could be a Mac EOL or
244 // start of DOS EOL so wait for next char
245 break;
246
247 default:
248 if ( chLast == '\r' )
249 {
250 // Mac line termination
251 if ( p - 1 >= lineStart )
252 {
253 AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac);
254 }
255 else
256 {
257 // there were two line endings, so add an empty line:
258 AddLine(wxEmptyString, wxTextFileType_Mac);
259 }
260 lineStart = p;
261 }
262 }
263
264 chLast = ch;
265 }
266
267 // anything in the last line?
268 if ( lineStart != end )
269 {
270 // add unterminated last line
271 AddLine(wxString(lineStart, end), wxTextFileType_None);
272 }
273
274 return true;
275 }
276
277
278 bool wxTextFile::OnWrite(wxTextFileType typeNew, const wxMBConv& conv)
279 {
280 wxFileName fn = m_strBufferName;
281
282 // We do NOT want wxPATH_NORM_CASE here, or the case will not
283 // be preserved.
284 if ( !fn.IsAbsolute() )
285 fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE |
286 wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG);
287
288 wxTempFile fileTmp(fn.GetFullPath());
289
290 if ( !fileTmp.IsOpened() ) {
291 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
292 return false;
293 }
294
295 size_t nCount = GetLineCount();
296 for ( size_t n = 0; n < nCount; n++ ) {
297 fileTmp.Write(GetLine(n) +
298 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
299 : typeNew),
300 conv);
301 }
302
303 // replace the old file with this one
304 return fileTmp.Commit();
305 }
306
307 #endif // wxUSE_TEXTFILE