]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
Fixed compile errors.
[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 #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 // ----------------------------------------------------------------------------
39
40 // default type is the native one
41 const wxTextFileType wxTextFile::typeDefault =
42 #if defined(__WINDOWS__)
43 wxTextFileType_Dos;
44 #elif defined(__UNIX__)
45 wxTextFileType_Unix;
46 #elif defined(__WXMAC__)
47 wxTextFileType_Mac;
48 #else
49 wxTextFileType_None;
50 #error "wxTextFile: unsupported platform."
51 #endif
52
53
54 // ----------------------------------------------------------------------------
55 // ctors & dtor
56 // ----------------------------------------------------------------------------
57
58 wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
59 {
60 }
61
62 wxTextFile::~wxTextFile()
63 {
64 // m_file dtor called automatically
65 }
66
67 // ----------------------------------------------------------------------------
68 // file operations
69 // ----------------------------------------------------------------------------
70
71 bool wxTextFile::Open(const wxString& strFile)
72 {
73 m_strFile = strFile;
74
75 return Open();
76 }
77
78 bool 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.
97 wxTextFileType wxTextFile::GuessType() const
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
103 size_t nUnix = 0, // number of '\n's alone
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)
109 size_t nCount = m_aLines.Count() / 3,
110 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
111
112 #define AnalyseLine(n) \
113 switch ( m_aTypes[n] ) { \
114 case wxTextFileType_Unix: nUnix++; break; \
115 case wxTextFileType_Dos: nDos++; break; \
116 case wxTextFileType_Mac: nMac++; break; \
117 default: wxFAIL_MSG(_("unknown line terminator")); \
118 }
119
120 size_t n;
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
133 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
134 }
135 else {
136 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
137 : n##t1 > n##t2 \
138 ? wxTextFileType_##t1 \
139 : wxTextFileType_##t2
140
141 // Watcom C++ doesn't seem to be able to handle the macro
142 #if !defined(__WATCOMC__)
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
149 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
150 }
151 #endif // __WATCOMC__
152
153 #undef GREATER_OF
154 }
155
156 return typeDefault;
157 }
158
159 bool 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';
166 char buf[1024];
167 int n, nRead;
168 while ( !m_file.Eof() ) {
169 nRead = m_file.Read(buf, WXSIZEOF(buf));
170 if ( nRead == wxInvalidOffset ) {
171 // read error (error message already given in wxFile::Read)
172 m_file.Close();
173 return FALSE;
174 }
175
176 for ( n = 0; n < nRead; n++ ) {
177 ch = buf[n];
178 switch ( ch ) {
179 case '\n':
180 // Dos/Unix line termination
181 m_aLines.Add(str);
182 m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos
183 : wxTextFileType_Unix);
184 str.Empty();
185 chLast = '\n';
186 break;
187
188 case '\r':
189 if ( chLast == '\r' ) {
190 // Mac empty line
191 m_aLines.Add("");
192 m_aTypes.Add(wxTextFileType_Mac);
193 }
194 else
195 chLast = '\r';
196 break;
197
198 default:
199 if ( chLast == '\r' ) {
200 // Mac line termination
201 m_aLines.Add(str);
202 m_aTypes.Add(wxTextFileType_Mac);
203 chLast = ch;
204 str = ch;
205 }
206 else {
207 // add to the current line
208 str += ch;
209 }
210 }
211 }
212 }
213
214 // anything in the last line?
215 if ( !str.IsEmpty() ) {
216 m_aTypes.Add(wxTextFileType_None); // no line terminator
217 m_aLines.Add(str);
218 }
219
220 return TRUE;
221 }
222
223 bool wxTextFile::Close()
224 {
225 m_aTypes.Clear();
226 m_aLines.Clear();
227 m_nCurLine = 0;
228
229 return TRUE;
230 }
231
232 bool wxTextFile::Write(wxTextFileType typeNew)
233 {
234 wxTempFile fileTmp(m_strFile);
235
236 if ( !fileTmp.IsOpened() ) {
237 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
238 return FALSE;
239 }
240
241 size_t nCount = m_aLines.Count();
242 for ( size_t n = 0; n < nCount; n++ ) {
243 fileTmp.Write(m_aLines[n] +
244 GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n]
245 : typeNew));
246 }
247
248 // replace the old file with this one
249 return fileTmp.Commit();
250 }
251
252 const wxChar *wxTextFile::GetEOL(wxTextFileType type)
253 {
254 switch ( type ) {
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");
259
260 default:
261 wxFAIL_MSG(_T("bad file type in wxTextFile::GetEOL."));
262 return (const wxChar *) NULL;
263 }
264 }
265