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