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