]> git.saurik.com Git - wxWidgets.git/blame - src/common/textfile.cpp
1. added range checks in wxGridStringTable
[wxWidgets.git] / src / common / textfile.cpp
CommitLineData
c801d85f
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: textfile.cpp
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>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers
14// ============================================================================
15
16#ifdef __GNUG__
a1b82138 17 #pragma implementation "textfile.h"
c801d85f
KB
18#endif
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
a1b82138 23 #pragma hdrstop
c801d85f
KB
24#endif //__BORLANDC__
25
a1b82138
VZ
26#if !wxUSE_FILE
27 #undef wxUSE_TEXTFILE
28 #define wxUSE_TEXTFILE 0
29#endif // wxUSE_FILE
30
ce4169a4 31#ifndef WX_PRECOMP
a1b82138
VZ
32 #include "wx/string.h"
33 #include "wx/intl.h"
34 #include "wx/file.h"
35 #include "wx/log.h"
ce4169a4
RR
36#endif
37
a1b82138 38#include "wx/textfile.h"
c801d85f
KB
39
40// ============================================================================
41// wxTextFile class implementation
42// ============================================================================
43
44// ----------------------------------------------------------------------------
a1b82138 45// static methods (always compiled in)
c801d85f 46// ----------------------------------------------------------------------------
ba7f9a90 47
c801d85f 48// default type is the native one
c2389495 49const wxTextFileType wxTextFile::typeDefault =
34138703 50#if defined(__WINDOWS__)
c2389495 51 wxTextFileType_Dos;
c801d85f 52#elif defined(__UNIX__)
c2389495 53 wxTextFileType_Unix;
34138703 54#elif defined(__WXMAC__)
c2389495 55 wxTextFileType_Mac;
717b9bf2
DW
56#elif defined(__WXPM__)
57 wxTextFileType_Os2;
c801d85f 58#else
c2389495 59 wxTextFileType_None;
c801d85f
KB
60 #error "wxTextFile: unsupported platform."
61#endif
62
a1b82138
VZ
63const wxChar *wxTextFile::GetEOL(wxTextFileType type)
64{
65 switch ( type ) {
66 default:
223d09f6 67 wxFAIL_MSG(wxT("bad file type in wxTextFile::GetEOL."));
a1b82138
VZ
68 // fall through nevertheless - we must return something...
69
c0f587c5
VZ
70 case wxTextFileType_None: return wxT(_T(""));
71 case wxTextFileType_Unix: return wxT(_T("\n"));
72 case wxTextFileType_Dos: return wxT(_T("\r\n"));
73 case wxTextFileType_Mac: return wxT(_T("\r"));
a1b82138
VZ
74 }
75}
76
a1b82138
VZ
77wxString wxTextFile::Translate(const wxString& text, wxTextFileType type)
78{
79 // don't do anything if there is nothing to do
80 if ( type == wxTextFileType_None )
81 return text;
82
967a322f
GRG
83 // GRG: don't do anything either if it is empty
84 if ( text.IsEmpty() )
85 return text;
86
a1b82138
VZ
87 wxString eol = GetEOL(type), result;
88
89 // optimization: we know that the length of the new string will be about
90 // the same as the length of the old one, so prealloc memory to aviod
91 // unnecessary relocations
92 result.Alloc(text.Len());
93
94 wxChar chLast = 0;
95 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
96 {
97 wxChar ch = *pc;
98 switch ( ch ) {
c0f587c5 99 case _T('\n'):
a1b82138
VZ
100 // Dos/Unix line termination
101 result += eol;
c0f587c5 102 chLast = 0;
a1b82138
VZ
103 break;
104
c0f587c5
VZ
105 case _T('\r'):
106 if ( chLast == _T('\r') ) {
a1b82138
VZ
107 // Mac empty line
108 result += eol;
109 }
c0f587c5
VZ
110 else {
111 // just remember it: we don't know whether it is just "\r"
112 // or "\r\n" yet
113 chLast = _T('\r');
114 }
a1b82138
VZ
115 break;
116
117 default:
c0f587c5 118 if ( chLast == _T('\r') ) {
a1b82138
VZ
119 // Mac line termination
120 result += eol;
a1b82138 121 }
c0f587c5
VZ
122
123 // add to the current line
124 result += ch;
a1b82138
VZ
125 }
126 }
127
c0f587c5
VZ
128 if ( chLast ) {
129 // trailing '\r'
130 result += eol;
131 }
132
a1b82138
VZ
133 return result;
134}
135
136#if wxUSE_TEXTFILE
c801d85f
KB
137
138// ----------------------------------------------------------------------------
139// ctors & dtor
140// ----------------------------------------------------------------------------
141
142wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
143{
7cc98b3e
VZ
144 m_nCurLine = 0;
145 m_isOpened = FALSE;
c801d85f
KB
146}
147
148wxTextFile::~wxTextFile()
149{
150 // m_file dtor called automatically
151}
152
153// ----------------------------------------------------------------------------
154// file operations
155// ----------------------------------------------------------------------------
156
ef8d96c2
VZ
157bool wxTextFile::Exists() const
158{
159 return wxFile::Exists(m_strFile);
160}
161
c801d85f
KB
162bool wxTextFile::Open(const wxString& strFile)
163{
164 m_strFile = strFile;
f42d2aba 165
c801d85f
KB
166 return Open();
167}
168
169bool wxTextFile::Open()
170{
171 // file name must be either given in ctor or in Open(const wxString&)
172 wxASSERT( !m_strFile.IsEmpty() );
173
174 // open file in read-only mode
175 if ( !m_file.Open(m_strFile) )
176 return FALSE;
177
178 // read file into memory
7cc98b3e 179 m_isOpened = Read();
c801d85f
KB
180
181 m_file.Close();
182
7cc98b3e 183 return m_isOpened;
c801d85f
KB
184}
185
186// analyse some lines of the file trying to guess it's type.
187// if it fails, it assumes the native type for our platform.
c2389495 188wxTextFileType wxTextFile::GuessType() const
c801d85f
KB
189{
190 // file should be opened and we must be in it's beginning
191 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
192
193 // scan the file lines
c86f1403 194 size_t nUnix = 0, // number of '\n's alone
c801d85f
KB
195 nDos = 0, // number of '\r\n'
196 nMac = 0; // number of '\r's
197
198 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
199 #define MAX_LINES_SCAN (10)
c86f1403 200 size_t nCount = m_aLines.Count() / 3,
c801d85f
KB
201 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
202
203 #define AnalyseLine(n) \
204 switch ( m_aTypes[n] ) { \
c2389495
VZ
205 case wxTextFileType_Unix: nUnix++; break; \
206 case wxTextFileType_Dos: nDos++; break; \
207 case wxTextFileType_Mac: nMac++; break; \
1a5a8367 208 default: wxFAIL_MSG(_("unknown line terminator")); \
c801d85f
KB
209 }
210
c86f1403 211 size_t n;
c801d85f
KB
212 for ( n = 0; n < nScan; n++ ) // the beginning
213 AnalyseLine(n);
214 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
215 AnalyseLine(n);
216 for ( n = nCount - nScan; n < nCount; n++ )
217 AnalyseLine(n);
218
219 #undef AnalyseLine
220
a1b82138 221 // interpret the results (FIXME far from being even 50% fool proof)
c801d85f
KB
222 if ( nDos + nUnix + nMac == 0 ) {
223 // no newlines at all
1a5a8367 224 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
c801d85f
KB
225 }
226 else {
227 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
c2389495 228 : n##t1 > n##t2 \
f42d2aba 229 ? wxTextFileType_##t1 \
c2389495 230 : wxTextFileType_##t2
c801d85f 231
f42d2aba
VZ
232 // Watcom C++ doesn't seem to be able to handle the macro
233#if !defined(__WATCOMC__)
c801d85f
KB
234 if ( nDos > nUnix )
235 return GREATER_OF(Dos, Mac);
236 else if ( nDos < nUnix )
237 return GREATER_OF(Unix, Mac);
238 else {
239 // nDos == nUnix
c2389495 240 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
c801d85f 241 }
f42d2aba 242#endif // __WATCOMC__
c801d85f
KB
243
244 #undef GREATER_OF
245 }
246
247 return typeDefault;
248}
249
250bool wxTextFile::Read()
251{
252 // file should be opened and we must be in it's beginning
253 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
254
255 wxString str;
256 char ch, chLast = '\0';
ba7f9a90
VZ
257 char buf[1024];
258 int n, nRead;
c801d85f 259 while ( !m_file.Eof() ) {
ba7f9a90 260 nRead = m_file.Read(buf, WXSIZEOF(buf));
1678ad78 261 if ( nRead == wxInvalidOffset ) {
ba7f9a90 262 // read error (error message already given in wxFile::Read)
c801d85f
KB
263 return FALSE;
264 }
265
ba7f9a90
VZ
266 for ( n = 0; n < nRead; n++ ) {
267 ch = buf[n];
268 switch ( ch ) {
269 case '\n':
270 // Dos/Unix line termination
c801d85f 271 m_aLines.Add(str);
c2389495
VZ
272 m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos
273 : wxTextFileType_Unix);
ba7f9a90
VZ
274 str.Empty();
275 chLast = '\n';
276 break;
277
278 case '\r':
279 if ( chLast == '\r' ) {
280 // Mac empty line
c0f587c5 281 m_aLines.Add(wxEmptyString);
c2389495 282 m_aTypes.Add(wxTextFileType_Mac);
ba7f9a90
VZ
283 }
284 else
285 chLast = '\r';
286 break;
287
288 default:
289 if ( chLast == '\r' ) {
290 // Mac line termination
291 m_aLines.Add(str);
c2389495 292 m_aTypes.Add(wxTextFileType_Mac);
7749df1f 293 chLast = ch;
ba7f9a90
VZ
294 str = ch;
295 }
296 else {
297 // add to the current line
298 str += ch;
299 }
300 }
c801d85f
KB
301 }
302 }
303
304 // anything in the last line?
305 if ( !str.IsEmpty() ) {
c2389495 306 m_aTypes.Add(wxTextFileType_None); // no line terminator
c801d85f
KB
307 m_aLines.Add(str);
308 }
309
310 return TRUE;
311}
312
f42d2aba
VZ
313bool wxTextFile::Close()
314{
315 m_aTypes.Clear();
316 m_aLines.Clear();
317 m_nCurLine = 0;
7cc98b3e 318 m_isOpened = FALSE;
f42d2aba
VZ
319
320 return TRUE;
321}
322
c2389495 323bool wxTextFile::Write(wxTextFileType typeNew)
c801d85f
KB
324{
325 wxTempFile fileTmp(m_strFile);
326
327 if ( !fileTmp.IsOpened() ) {
1a5a8367 328 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
c801d85f
KB
329 return FALSE;
330 }
331
c86f1403
VZ
332 size_t nCount = m_aLines.Count();
333 for ( size_t n = 0; n < nCount; n++ ) {
ba7f9a90 334 fileTmp.Write(m_aLines[n] +
c2389495
VZ
335 GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n]
336 : typeNew));
c801d85f
KB
337 }
338
339 // replace the old file with this one
340 return fileTmp.Commit();
ba7f9a90 341}
6164d85e 342
a1b82138 343#endif // wxUSE_TEXTFILE
6164d85e 344