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