]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/textfile.cpp
Changed a few true -> TRUE
[wxWidgets.git] / src / common / textfile.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: textfile.cpp
3// Purpose: implementation of wxTextFile class
4// Author: Vadim Zeitlin
5// Modified by:
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__
17#pragma implementation "textfile.h"
18#endif
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif //__BORLANDC__
25
26#ifndef WX_PRECOMP
27#include "wx/defs.h"
28#endif
29
30#if wxUSE_TEXTFILE && wxUSE_FILE
31
32#include <wx/string.h>
33#include <wx/intl.h>
34#include <wx/file.h>
35#include <wx/log.h>
36#include <wx/textfile.h>
37
38// ============================================================================
39// wxTextFile class implementation
40// ============================================================================
41
42// ----------------------------------------------------------------------------
43// static variables
44// ----------------------------------------------------------------------------
45
46// default type is the native one
47const wxTextFileType wxTextFile::typeDefault =
48#if defined(__WINDOWS__)
49 wxTextFileType_Dos;
50#elif defined(__UNIX__)
51 wxTextFileType_Unix;
52#elif defined(__WXMAC__)
53 wxTextFileType_Mac;
54#else
55 wxTextFileType_None;
56 #error "wxTextFile: unsupported platform."
57#endif
58
59
60// ----------------------------------------------------------------------------
61// ctors & dtor
62// ----------------------------------------------------------------------------
63
64wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
65{
66}
67
68wxTextFile::~wxTextFile()
69{
70 // m_file dtor called automatically
71}
72
73// ----------------------------------------------------------------------------
74// file operations
75// ----------------------------------------------------------------------------
76
77bool wxTextFile::Open(const wxString& strFile)
78{
79 m_strFile = strFile;
80
81 return Open();
82}
83
84bool wxTextFile::Open()
85{
86 // file name must be either given in ctor or in Open(const wxString&)
87 wxASSERT( !m_strFile.IsEmpty() );
88
89 // open file in read-only mode
90 if ( !m_file.Open(m_strFile) )
91 return FALSE;
92
93 // read file into memory
94 bool bRet = Read();
95
96 m_file.Close();
97
98 return bRet;
99}
100
101// analyse some lines of the file trying to guess it's type.
102// if it fails, it assumes the native type for our platform.
103wxTextFileType wxTextFile::GuessType() const
104{
105 // file should be opened and we must be in it's beginning
106 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
107
108 // scan the file lines
109 size_t nUnix = 0, // number of '\n's alone
110 nDos = 0, // number of '\r\n'
111 nMac = 0; // number of '\r's
112
113 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
114 #define MAX_LINES_SCAN (10)
115 size_t nCount = m_aLines.Count() / 3,
116 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
117
118 #define AnalyseLine(n) \
119 switch ( m_aTypes[n] ) { \
120 case wxTextFileType_Unix: nUnix++; break; \
121 case wxTextFileType_Dos: nDos++; break; \
122 case wxTextFileType_Mac: nMac++; break; \
123 default: wxFAIL_MSG(_("unknown line terminator")); \
124 }
125
126 size_t n;
127 for ( n = 0; n < nScan; n++ ) // the beginning
128 AnalyseLine(n);
129 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
130 AnalyseLine(n);
131 for ( n = nCount - nScan; n < nCount; n++ )
132 AnalyseLine(n);
133
134 #undef AnalyseLine
135
136 // interpret the results (@@ far from being even 50% fool proof)
137 if ( nDos + nUnix + nMac == 0 ) {
138 // no newlines at all
139 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
140 }
141 else {
142 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
143 : n##t1 > n##t2 \
144 ? wxTextFileType_##t1 \
145 : wxTextFileType_##t2
146
147 // Watcom C++ doesn't seem to be able to handle the macro
148#if !defined(__WATCOMC__)
149 if ( nDos > nUnix )
150 return GREATER_OF(Dos, Mac);
151 else if ( nDos < nUnix )
152 return GREATER_OF(Unix, Mac);
153 else {
154 // nDos == nUnix
155 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
156 }
157#endif // __WATCOMC__
158
159 #undef GREATER_OF
160 }
161
162 return typeDefault;
163}
164
165bool wxTextFile::Read()
166{
167 // file should be opened and we must be in it's beginning
168 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
169
170 wxString str;
171 char ch, chLast = '\0';
172 char buf[1024];
173 int n, nRead;
174 while ( !m_file.Eof() ) {
175 nRead = m_file.Read(buf, WXSIZEOF(buf));
176 if ( nRead == wxInvalidOffset ) {
177 // read error (error message already given in wxFile::Read)
178 m_file.Close();
179 return FALSE;
180 }
181
182 for ( n = 0; n < nRead; n++ ) {
183 ch = buf[n];
184 switch ( ch ) {
185 case '\n':
186 // Dos/Unix line termination
187 m_aLines.Add(str);
188 m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos
189 : wxTextFileType_Unix);
190 str.Empty();
191 chLast = '\n';
192 break;
193
194 case '\r':
195 if ( chLast == '\r' ) {
196 // Mac empty line
197 m_aLines.Add("");
198 m_aTypes.Add(wxTextFileType_Mac);
199 }
200 else
201 chLast = '\r';
202 break;
203
204 default:
205 if ( chLast == '\r' ) {
206 // Mac line termination
207 m_aLines.Add(str);
208 m_aTypes.Add(wxTextFileType_Mac);
209 chLast = ch;
210 str = ch;
211 }
212 else {
213 // add to the current line
214 str += ch;
215 }
216 }
217 }
218 }
219
220 // anything in the last line?
221 if ( !str.IsEmpty() ) {
222 m_aTypes.Add(wxTextFileType_None); // no line terminator
223 m_aLines.Add(str);
224 }
225
226 return TRUE;
227}
228
229bool wxTextFile::Close()
230{
231 m_aTypes.Clear();
232 m_aLines.Clear();
233 m_nCurLine = 0;
234
235 return TRUE;
236}
237
238bool wxTextFile::Write(wxTextFileType typeNew)
239{
240 wxTempFile fileTmp(m_strFile);
241
242 if ( !fileTmp.IsOpened() ) {
243 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
244 return FALSE;
245 }
246
247 size_t nCount = m_aLines.Count();
248 for ( size_t n = 0; n < nCount; n++ ) {
249 fileTmp.Write(m_aLines[n] +
250 GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n]
251 : typeNew));
252 }
253
254 // replace the old file with this one
255 return fileTmp.Commit();
256}
257
258const wxChar *wxTextFile::GetEOL(wxTextFileType type)
259{
260 switch ( type ) {
261 case wxTextFileType_None: return _T("");
262 case wxTextFileType_Unix: return _T("\n");
263 case wxTextFileType_Dos: return _T("\r\n");
264 case wxTextFileType_Mac: return _T("\r");
265
266 default:
267 wxFAIL_MSG(_T("bad file type in wxTextFile::GetEOL."));
268 return (const wxChar *) NULL;
269 }
270}
271
272#endif