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