]> git.saurik.com Git - wxWidgets.git/blame - src/common/textfile.cpp
fixed key event code for ASCII keys in wxListCtrl
[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
a7352379
GD
49// the native type under Mac OS X is:
50// - Unix when compiling with the Apple Developer Tools (__UNIX__)
51// - Mac when compiling with CodeWarrior (__WXMAC__)
52
c2389495 53const wxTextFileType wxTextFile::typeDefault =
34138703 54#if defined(__WINDOWS__)
c2389495 55 wxTextFileType_Dos;
c801d85f 56#elif defined(__UNIX__)
c2389495 57 wxTextFileType_Unix;
34138703 58#elif defined(__WXMAC__)
c2389495 59 wxTextFileType_Mac;
717b9bf2
DW
60#elif defined(__WXPM__)
61 wxTextFileType_Os2;
c801d85f 62#else
c2389495 63 wxTextFileType_None;
c801d85f
KB
64 #error "wxTextFile: unsupported platform."
65#endif
66
a1b82138
VZ
67const wxChar *wxTextFile::GetEOL(wxTextFileType type)
68{
69 switch ( type ) {
70 default:
223d09f6 71 wxFAIL_MSG(wxT("bad file type in wxTextFile::GetEOL."));
a1b82138
VZ
72 // fall through nevertheless - we must return something...
73
f6bcfd97
BP
74 case wxTextFileType_None: return wxT("");
75 case wxTextFileType_Unix: return wxT("\n");
76 case wxTextFileType_Dos: return wxT("\r\n");
77 case wxTextFileType_Mac: return wxT("\r");
a1b82138
VZ
78 }
79}
80
a1b82138
VZ
81wxString wxTextFile::Translate(const wxString& text, wxTextFileType type)
82{
83 // don't do anything if there is nothing to do
84 if ( type == wxTextFileType_None )
85 return text;
86
967a322f
GRG
87 // GRG: don't do anything either if it is empty
88 if ( text.IsEmpty() )
89 return text;
90
a1b82138
VZ
91 wxString eol = GetEOL(type), result;
92
93 // optimization: we know that the length of the new string will be about
94 // the same as the length of the old one, so prealloc memory to aviod
95 // unnecessary relocations
96 result.Alloc(text.Len());
97
98 wxChar chLast = 0;
99 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
100 {
101 wxChar ch = *pc;
102 switch ( ch ) {
c0f587c5 103 case _T('\n'):
a1b82138
VZ
104 // Dos/Unix line termination
105 result += eol;
c0f587c5 106 chLast = 0;
a1b82138
VZ
107 break;
108
c0f587c5
VZ
109 case _T('\r'):
110 if ( chLast == _T('\r') ) {
a1b82138
VZ
111 // Mac empty line
112 result += eol;
113 }
c0f587c5
VZ
114 else {
115 // just remember it: we don't know whether it is just "\r"
116 // or "\r\n" yet
117 chLast = _T('\r');
118 }
a1b82138
VZ
119 break;
120
121 default:
c0f587c5 122 if ( chLast == _T('\r') ) {
a1b82138
VZ
123 // Mac line termination
124 result += eol;
b01ca3e3
VZ
125
126 // reset chLast to avoid inserting another eol before the
127 // next character
128 chLast = 0;
a1b82138 129 }
c0f587c5
VZ
130
131 // add to the current line
132 result += ch;
a1b82138
VZ
133 }
134 }
135
c0f587c5
VZ
136 if ( chLast ) {
137 // trailing '\r'
138 result += eol;
139 }
140
a1b82138
VZ
141 return result;
142}
143
144#if wxUSE_TEXTFILE
c801d85f
KB
145
146// ----------------------------------------------------------------------------
147// ctors & dtor
148// ----------------------------------------------------------------------------
149
150wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
151{
7cc98b3e
VZ
152 m_nCurLine = 0;
153 m_isOpened = FALSE;
c801d85f
KB
154}
155
156wxTextFile::~wxTextFile()
157{
158 // m_file dtor called automatically
159}
160
161// ----------------------------------------------------------------------------
162// file operations
163// ----------------------------------------------------------------------------
164
ef8d96c2
VZ
165bool wxTextFile::Exists() const
166{
167 return wxFile::Exists(m_strFile);
168}
169
1b6dea5d
MB
170bool wxTextFile::Create(const wxString& strFile)
171{
172 m_strFile = strFile;
173
174 return Create();
175}
176
177bool wxTextFile::Create()
178{
179 // file name must be either given in ctor or in Create(const wxString&)
180 wxASSERT( !m_strFile.IsEmpty() );
181
182 // if the file already exists do nothing
183 if ( Exists() ) return FALSE;
184
185 if ( m_file.Open(m_strFile, wxFile::write) )
186 {
187 m_file.Close();
188 return TRUE;
189 }
190 else
191 {
192 return FALSE;
193 }
194}
195
adfed1ca 196bool wxTextFile::Open(const wxString& strFile, wxMBConv& conv)
c801d85f
KB
197{
198 m_strFile = strFile;
f42d2aba 199
adfed1ca 200 return Open(conv);
c801d85f
KB
201}
202
adfed1ca 203bool wxTextFile::Open(wxMBConv& conv)
c801d85f
KB
204{
205 // file name must be either given in ctor or in Open(const wxString&)
206 wxASSERT( !m_strFile.IsEmpty() );
207
208 // open file in read-only mode
209 if ( !m_file.Open(m_strFile) )
210 return FALSE;
211
212 // read file into memory
adfed1ca 213 m_isOpened = Read(conv);
c801d85f
KB
214
215 m_file.Close();
216
7cc98b3e 217 return m_isOpened;
c801d85f
KB
218}
219
220// analyse some lines of the file trying to guess it's type.
221// if it fails, it assumes the native type for our platform.
c2389495 222wxTextFileType wxTextFile::GuessType() const
c801d85f 223{
4d5cd6e0 224 wxASSERT( IsOpened() );
c801d85f
KB
225
226 // scan the file lines
c86f1403 227 size_t nUnix = 0, // number of '\n's alone
adfed1ca
VS
228 nDos = 0, // number of '\r\n'
229 nMac = 0; // number of '\r's
c801d85f
KB
230
231 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
232 #define MAX_LINES_SCAN (10)
c86f1403 233 size_t nCount = m_aLines.Count() / 3,
c801d85f
KB
234 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
235
236 #define AnalyseLine(n) \
237 switch ( m_aTypes[n] ) { \
c2389495
VZ
238 case wxTextFileType_Unix: nUnix++; break; \
239 case wxTextFileType_Dos: nDos++; break; \
240 case wxTextFileType_Mac: nMac++; break; \
1a5a8367 241 default: wxFAIL_MSG(_("unknown line terminator")); \
c801d85f
KB
242 }
243
c86f1403 244 size_t n;
c801d85f
KB
245 for ( n = 0; n < nScan; n++ ) // the beginning
246 AnalyseLine(n);
247 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
248 AnalyseLine(n);
249 for ( n = nCount - nScan; n < nCount; n++ )
250 AnalyseLine(n);
251
252 #undef AnalyseLine
253
a1b82138 254 // interpret the results (FIXME far from being even 50% fool proof)
adfed1ca 255 if ( nScan > 0 && nDos + nUnix + nMac == 0 ) {
c801d85f 256 // no newlines at all
1a5a8367 257 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
c801d85f
KB
258 }
259 else {
260 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
c2389495 261 : n##t1 > n##t2 \
f42d2aba 262 ? wxTextFileType_##t1 \
c2389495 263 : wxTextFileType_##t2
c801d85f 264
f42d2aba
VZ
265 // Watcom C++ doesn't seem to be able to handle the macro
266#if !defined(__WATCOMC__)
c801d85f
KB
267 if ( nDos > nUnix )
268 return GREATER_OF(Dos, Mac);
269 else if ( nDos < nUnix )
270 return GREATER_OF(Unix, Mac);
271 else {
272 // nDos == nUnix
c2389495 273 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
c801d85f 274 }
f42d2aba 275#endif // __WATCOMC__
c801d85f
KB
276
277 #undef GREATER_OF
278 }
279
280 return typeDefault;
281}
282
adfed1ca 283bool wxTextFile::Read(wxMBConv& conv)
c801d85f
KB
284{
285 // file should be opened and we must be in it's beginning
286 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
287
adfed1ca
VS
288#if wxUSE_UNICODE
289 char conv_mbBuf[2];
290 wchar_t conv_wcBuf[2];
291 conv_mbBuf[1] = 0;
292#else
293 (void)conv;
294#endif
295
c801d85f
KB
296 wxString str;
297 char ch, chLast = '\0';
ba7f9a90
VZ
298 char buf[1024];
299 int n, nRead;
f6bcfd97 300 do {
ba7f9a90 301 nRead = m_file.Read(buf, WXSIZEOF(buf));
1678ad78 302 if ( nRead == wxInvalidOffset ) {
ba7f9a90 303 // read error (error message already given in wxFile::Read)
c801d85f
KB
304 return FALSE;
305 }
306
ba7f9a90
VZ
307 for ( n = 0; n < nRead; n++ ) {
308 ch = buf[n];
309 switch ( ch ) {
310 case '\n':
311 // Dos/Unix line termination
c801d85f 312 m_aLines.Add(str);
c2389495
VZ
313 m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos
314 : wxTextFileType_Unix);
ba7f9a90
VZ
315 str.Empty();
316 chLast = '\n';
317 break;
318
319 case '\r':
320 if ( chLast == '\r' ) {
321 // Mac empty line
c0f587c5 322 m_aLines.Add(wxEmptyString);
c2389495 323 m_aTypes.Add(wxTextFileType_Mac);
ba7f9a90
VZ
324 }
325 else
326 chLast = '\r';
327 break;
328
329 default:
330 if ( chLast == '\r' ) {
331 // Mac line termination
332 m_aLines.Add(str);
c2389495 333 m_aTypes.Add(wxTextFileType_Mac);
7749df1f 334 chLast = ch;
adfed1ca
VS
335#if wxUSE_UNICODE
336 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
337 conv_wcBuf[0] = ch;
338 str = conv_wcBuf[0];
339#else
ba7f9a90 340 str = ch;
adfed1ca 341#endif
ba7f9a90
VZ
342 }
343 else {
344 // add to the current line
adfed1ca
VS
345#if wxUSE_UNICODE
346 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
347 conv_wcBuf[0] = ch;
348 str += conv_wcBuf[0];
349#else
ba7f9a90 350 str += ch;
adfed1ca 351#endif
ba7f9a90
VZ
352 }
353 }
c801d85f 354 }
f6bcfd97 355 } while ( nRead == WXSIZEOF(buf) );
c801d85f
KB
356
357 // anything in the last line?
358 if ( !str.IsEmpty() ) {
c2389495 359 m_aTypes.Add(wxTextFileType_None); // no line terminator
c801d85f
KB
360 m_aLines.Add(str);
361 }
362
363 return TRUE;
364}
365
f42d2aba
VZ
366bool wxTextFile::Close()
367{
368 m_aTypes.Clear();
369 m_aLines.Clear();
370 m_nCurLine = 0;
7cc98b3e 371 m_isOpened = FALSE;
f42d2aba
VZ
372
373 return TRUE;
374}
375
adfed1ca 376bool wxTextFile::Write(wxTextFileType typeNew, wxMBConv& conv)
c801d85f
KB
377{
378 wxTempFile fileTmp(m_strFile);
379
380 if ( !fileTmp.IsOpened() ) {
1a5a8367 381 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
c801d85f
KB
382 return FALSE;
383 }
384
c86f1403
VZ
385 size_t nCount = m_aLines.Count();
386 for ( size_t n = 0; n < nCount; n++ ) {
ba7f9a90 387 fileTmp.Write(m_aLines[n] +
c2389495 388 GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n]
adfed1ca 389 : typeNew), conv);
c801d85f
KB
390 }
391
392 // replace the old file with this one
393 return fileTmp.Commit();
ba7f9a90 394}
6164d85e 395
a1b82138 396#endif // wxUSE_TEXTFILE
6164d85e 397