]> git.saurik.com Git - wxWidgets.git/blame - src/common/textfile.cpp
adding implementation for osx
[wxWidgets.git] / src / common / textfile.cpp
CommitLineData
c801d85f 1///////////////////////////////////////////////////////////////////////////////
a3a584a7 2// Name: src/common/textfile.cpp
c801d85f
KB
3// Purpose: implementation of wxTextFile class
4// Author: Vadim Zeitlin
ba7f9a90 5// Modified by:
c801d85f
KB
6// Created: 03.04.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
c801d85f
KB
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers
14// ============================================================================
15
c801d85f
KB
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
a1b82138 19 #pragma hdrstop
c801d85f
KB
20#endif //__BORLANDC__
21
a3a584a7 22#if !wxUSE_FILE || !wxUSE_TEXTBUFFER
a1b82138
VZ
23 #undef wxUSE_TEXTFILE
24 #define wxUSE_TEXTFILE 0
25#endif // wxUSE_FILE
26
a3a584a7
VZ
27#if wxUSE_TEXTFILE
28
ce4169a4 29#ifndef WX_PRECOMP
68c97af3
VZ
30 #include "wx/string.h"
31 #include "wx/intl.h"
32 #include "wx/file.h"
33 #include "wx/log.h"
ce4169a4
RR
34#endif
35
a3a584a7 36#include "wx/textfile.h"
68c97af3 37#include "wx/filename.h"
dbcf443c 38#include "wx/buffer.h"
c801d85f
KB
39
40// ============================================================================
41// wxTextFile class implementation
42// ============================================================================
43
a3a584a7
VZ
44wxTextFile::wxTextFile(const wxString& strFileName)
45 : wxTextBuffer(strFileName)
a1b82138 46{
a1b82138
VZ
47}
48
c801d85f
KB
49
50// ----------------------------------------------------------------------------
51// file operations
52// ----------------------------------------------------------------------------
53
a3a584a7 54bool wxTextFile::OnExists() const
ef8d96c2 55{
a3a584a7 56 return wxFile::Exists(m_strBufferName);
ef8d96c2
VZ
57}
58
1b6dea5d 59
a3a584a7 60bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode OpenMode)
1b6dea5d 61{
77f859c3
VZ
62 wxFile::OpenMode FileOpenMode;
63
64 switch ( OpenMode )
65 {
66 default:
9a83f860 67 wxFAIL_MSG( wxT("unknown open mode in wxTextFile::Open") );
77f859c3 68 // fall through
27752aab 69
a3a584a7
VZ
70 case ReadAccess :
71 FileOpenMode = wxFile::read;
72 break;
77f859c3 73
a3a584a7
VZ
74 case WriteAccess :
75 FileOpenMode = wxFile::write;
76 break;
77f859c3 77 }
1b6dea5d 78
a3a584a7 79 return m_file.Open(strBufferName.c_str(), FileOpenMode);
c801d85f
KB
80}
81
c801d85f 82
a3a584a7 83bool wxTextFile::OnClose()
c801d85f 84{
a3a584a7 85 return m_file.Close();
c801d85f
KB
86}
87
a3a584a7 88
830f8f11 89bool wxTextFile::OnRead(const wxMBConv& conv)
c801d85f 90{
6594faa9 91 // file should be opened
9a83f860 92 wxASSERT_MSG( m_file.IsOpened(), wxT("can't read closed file") );
dbcf443c
VZ
93
94 // read the entire file in memory: this is not the most efficient thing to
29a96d3c
VZ
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
dbcf443c
VZ
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
29a96d3c
VZ
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
6594faa9
VZ
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;
29a96d3c 123 if ( !buf.extend(bufSize) )
6594faa9
VZ
124 return false;
125
126 // if the file is seekable, also check that we're at its beginning
9a83f860 127 wxASSERT_MSG( m_file.Tell() == 0, wxT("should be at start of file") );
6594faa9 128
29a96d3c 129 char *dst = buf.data();
2059910d 130 for ( size_t nRemaining = bufSize; nRemaining > 0; )
d9ade1df 131 {
2059910d
VZ
132 size_t nToRead = BLOCK_SIZE;
133
134 // the file size could have changed, avoid overflowing the buffer
135 // even if it did
136 if ( nToRead > nRemaining )
137 nToRead = nRemaining;
138
139 ssize_t nRead = m_file.Read(dst, nToRead);
d9ade1df 140
29a96d3c
VZ
141 if ( nRead == wxInvalidOffset )
142 {
143 // read error (error message already given in wxFile::Read)
144 return false;
145 }
6594faa9 146
29a96d3c
VZ
147 if ( nRead == 0 )
148 {
149 // this file can't be empty because we checked for this above
2059910d 150 // so this must be the end of file
29a96d3c
VZ
151 break;
152 }
86948c99 153
29a96d3c 154 dst += nRead;
2059910d 155 nRemaining -= nRead;
6594faa9 156 }
29a96d3c
VZ
157
158 wxASSERT_MSG( dst - buf.data() == (wxFileOffset)bufSize,
9a83f860 159 wxT("logic error") );
29a96d3c
VZ
160 }
161 else // file is not seekable
162 {
163 char block[BLOCK_SIZE];
164 for ( ;; )
6594faa9 165 {
29a96d3c
VZ
166 ssize_t nRead = m_file.Read(block, WXSIZEOF(block));
167
168 if ( nRead == wxInvalidOffset )
169 {
170 // read error (error message already given in wxFile::Read)
6594faa9 171 return false;
29a96d3c 172 }
dbcf443c 173
29a96d3c
VZ
174 if ( nRead == 0 )
175 {
176 // if no bytes have been read, presumably this is a
177 // valid-but-empty file
178 if ( bufSize == 0 )
179 return true;
c1981a2f 180
29a96d3c
VZ
181 // otherwise we've finished reading the file
182 break;
183 }
184
185 // extend the buffer for new data
186 if ( !buf.extend(bufSize + nRead) )
187 return false;
188
189 // and append it to the buffer
190 memcpy(buf.data() + bufSize, block, nRead);
191 bufSize += nRead;
192 }
6594faa9
VZ
193 }
194
29a96d3c 195 const wxString str(buf, conv, bufSize);
44327ff3 196
6594faa9 197 // there's no risk of this happening in ANSI build
b260e323 198#if wxUSE_UNICODE
44327ff3 199 if ( bufSize > 4 && str.empty() )
dbcf443c 200 {
abc912df 201 wxLogError(_("Failed to convert file \"%s\" to Unicode."), GetName());
dbcf443c
VZ
202 return false;
203 }
204#endif // wxUSE_UNICODE
b260e323 205
29a96d3c
VZ
206 // we don't need this memory any more
207 buf.reset();
b260e323 208
86948c99 209
dbcf443c
VZ
210 // now break the buffer in lines
211
212 // last processed character, we need to know if it was a CR or not
213 wxChar chLast = '\0';
86948c99 214
dbcf443c
VZ
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++ )
219 {
220 const wxChar ch = *p;
221 switch ( ch )
222 {
223 case '\n':
224 // could be a DOS or Unix EOL
225 if ( chLast == '\r' )
226 {
82bf96f5
VS
227 if ( p - 1 >= lineStart )
228 {
229 AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos);
230 }
231 else
232 {
233 // there were two line endings, so add an empty line:
234 AddLine(wxEmptyString, wxTextFileType_Dos);
235 }
dbcf443c
VZ
236 }
237 else // bare '\n', Unix style
238 {
239 AddLine(wxString(lineStart, p), wxTextFileType_Unix);
240 }
241
242 lineStart = p + 1;
243 break;
244
245 case '\r':
246 if ( chLast == '\r' )
247 {
248 // Mac empty line
249 AddLine(wxEmptyString, wxTextFileType_Mac);
86948c99 250 lineStart = p + 1;
dbcf443c
VZ
251 }
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
254 break;
255
256 default:
257 if ( chLast == '\r' )
258 {
259 // Mac line termination
82bf96f5
VS
260 if ( p - 1 >= lineStart )
261 {
262 AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac);
263 }
264 else
265 {
266 // there were two line endings, so add an empty line:
267 AddLine(wxEmptyString, wxTextFileType_Mac);
268 }
dbcf443c
VZ
269 lineStart = p;
270 }
d9ade1df 271 }
86948c99 272
dbcf443c 273 chLast = ch;
86948c99 274 }
d9ade1df
VS
275
276 // anything in the last line?
dbcf443c 277 if ( lineStart != end )
d9ade1df 278 {
dbcf443c
VZ
279 // add unterminated last line
280 AddLine(wxString(lineStart, end), wxTextFileType_None);
c801d85f 281 }
c801d85f 282
cb719f2e 283 return true;
c801d85f
KB
284}
285
f42d2aba 286
830f8f11 287bool wxTextFile::OnWrite(wxTextFileType typeNew, const wxMBConv& conv)
c801d85f 288{
68c97af3 289 wxFileName fn = m_strBufferName;
baed1077
JS
290
291 // We do NOT want wxPATH_NORM_CASE here, or the case will not
292 // be preserved.
68c97af3 293 if ( !fn.IsAbsolute() )
32a0d013
VS
294 fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE |
295 wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG);
68c97af3 296
deab4540 297 wxTempFile fileTmp(fn.GetFullPath());
c801d85f 298
a3a584a7
VZ
299 if ( !fileTmp.IsOpened() ) {
300 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
cb719f2e 301 return false;
a3a584a7 302 }
c801d85f 303
a3a584a7
VZ
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)
308 : typeNew),
309 conv);
310 }
c801d85f 311
a3a584a7
VZ
312 // replace the old file with this one
313 return fileTmp.Commit();
ba7f9a90 314}
6164d85e 315
a1b82138 316#endif // wxUSE_TEXTFILE