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