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