]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
* Changed char to wxChar in operators.
[wxWidgets.git] / src / common / textfile.cpp
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
47 const 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
64 wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
65 {
66 m_nCurLine = 0;
67 m_isOpened = FALSE;
68 }
69
70 wxTextFile::~wxTextFile()
71 {
72 // m_file dtor called automatically
73 }
74
75 // ----------------------------------------------------------------------------
76 // file operations
77 // ----------------------------------------------------------------------------
78
79 bool wxTextFile::Open(const wxString& strFile)
80 {
81 m_strFile = strFile;
82
83 return Open();
84 }
85
86 bool 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
96 m_isOpened = Read();
97
98 m_file.Close();
99
100 return m_isOpened;
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.
105 wxTextFileType wxTextFile::GuessType() const
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
111 size_t nUnix = 0, // number of '\n's alone
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)
117 size_t nCount = m_aLines.Count() / 3,
118 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
119
120 #define AnalyseLine(n) \
121 switch ( m_aTypes[n] ) { \
122 case wxTextFileType_Unix: nUnix++; break; \
123 case wxTextFileType_Dos: nDos++; break; \
124 case wxTextFileType_Mac: nMac++; break; \
125 default: wxFAIL_MSG(_("unknown line terminator")); \
126 }
127
128 size_t n;
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
141 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
142 }
143 else {
144 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
145 : n##t1 > n##t2 \
146 ? wxTextFileType_##t1 \
147 : wxTextFileType_##t2
148
149 // Watcom C++ doesn't seem to be able to handle the macro
150 #if !defined(__WATCOMC__)
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
157 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
158 }
159 #endif // __WATCOMC__
160
161 #undef GREATER_OF
162 }
163
164 return typeDefault;
165 }
166
167 bool 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';
174 char buf[1024];
175 int n, nRead;
176 while ( !m_file.Eof() ) {
177 nRead = m_file.Read(buf, WXSIZEOF(buf));
178 if ( nRead == wxInvalidOffset ) {
179 // read error (error message already given in wxFile::Read)
180 return FALSE;
181 }
182
183 for ( n = 0; n < nRead; n++ ) {
184 ch = buf[n];
185 switch ( ch ) {
186 case '\n':
187 // Dos/Unix line termination
188 m_aLines.Add(str);
189 m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos
190 : wxTextFileType_Unix);
191 str.Empty();
192 chLast = '\n';
193 break;
194
195 case '\r':
196 if ( chLast == '\r' ) {
197 // Mac empty line
198 m_aLines.Add("");
199 m_aTypes.Add(wxTextFileType_Mac);
200 }
201 else
202 chLast = '\r';
203 break;
204
205 default:
206 if ( chLast == '\r' ) {
207 // Mac line termination
208 m_aLines.Add(str);
209 m_aTypes.Add(wxTextFileType_Mac);
210 chLast = ch;
211 str = ch;
212 }
213 else {
214 // add to the current line
215 str += ch;
216 }
217 }
218 }
219 }
220
221 // anything in the last line?
222 if ( !str.IsEmpty() ) {
223 m_aTypes.Add(wxTextFileType_None); // no line terminator
224 m_aLines.Add(str);
225 }
226
227 return TRUE;
228 }
229
230 bool wxTextFile::Close()
231 {
232 m_aTypes.Clear();
233 m_aLines.Clear();
234 m_nCurLine = 0;
235 m_isOpened = FALSE;
236
237 return TRUE;
238 }
239
240 bool wxTextFile::Write(wxTextFileType typeNew)
241 {
242 wxTempFile fileTmp(m_strFile);
243
244 if ( !fileTmp.IsOpened() ) {
245 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
246 return FALSE;
247 }
248
249 size_t nCount = m_aLines.Count();
250 for ( size_t n = 0; n < nCount; n++ ) {
251 fileTmp.Write(m_aLines[n] +
252 GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n]
253 : typeNew));
254 }
255
256 // replace the old file with this one
257 return fileTmp.Commit();
258 }
259
260 const wxChar *wxTextFile::GetEOL(wxTextFileType type)
261 {
262 switch ( type ) {
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");
267
268 default:
269 wxFAIL_MSG(_T("bad file type in wxTextFile::GetEOL."));
270 return (const wxChar *) NULL;
271 }
272 }
273
274 #endif