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