]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
wxString(file.fn_str()) doesn't make sense. If just file.fn_str() gives
[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 #if !wxUSE_FILE
27 #undef wxUSE_TEXTFILE
28 #define wxUSE_TEXTFILE 0
29 #endif // wxUSE_FILE
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/intl.h"
34 #include "wx/file.h"
35 #include "wx/log.h"
36 #endif
37
38 #include "wx/textfile.h"
39
40 // ============================================================================
41 // wxTextFile class implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // static methods (always compiled in)
46 // ----------------------------------------------------------------------------
47
48 // default type is the native one
49 const wxTextFileType wxTextFile::typeDefault =
50 #if defined(__WINDOWS__)
51 wxTextFileType_Dos;
52 #elif defined(__UNIX__)
53 wxTextFileType_Unix;
54 #elif defined(__WXMAC__)
55 wxTextFileType_Mac;
56 #else
57 wxTextFileType_None;
58 #error "wxTextFile: unsupported platform."
59 #endif
60
61 const wxChar *wxTextFile::GetEOL(wxTextFileType type)
62 {
63 switch ( type ) {
64 default:
65 wxFAIL_MSG(_T("bad file type in wxTextFile::GetEOL."));
66 // fall through nevertheless - we must return something...
67
68 case wxTextFileType_None: return _T("");
69 case wxTextFileType_Unix: return _T("\n");
70 case wxTextFileType_Dos: return _T("\r\n");
71 case wxTextFileType_Mac: return _T("\r");
72 }
73 }
74
75
76 wxString wxTextFile::Translate(const wxString& text, wxTextFileType type)
77 {
78 // don't do anything if there is nothing to do
79 if ( type == wxTextFileType_None )
80 return text;
81
82 wxString eol = GetEOL(type), result;
83
84 // optimization: we know that the length of the new string will be about
85 // the same as the length of the old one, so prealloc memory to aviod
86 // unnecessary relocations
87 result.Alloc(text.Len());
88
89 wxChar chLast = 0;
90 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
91 {
92 wxChar ch = *pc;
93 switch ( ch ) {
94 case '\n':
95 // Dos/Unix line termination
96 result += eol;
97 chLast = '\n';
98 break;
99
100 case '\r':
101 if ( chLast == '\r' ) {
102 // Mac empty line
103 result += eol;
104 }
105 else
106 chLast = '\r';
107 break;
108
109 default:
110 if ( chLast == '\r' ) {
111 // Mac line termination
112 result += eol;
113 chLast = ch;
114 }
115 else {
116 // add to the current line
117 result += ch;
118 }
119 }
120 }
121
122 return result;
123 }
124
125 #if wxUSE_TEXTFILE
126
127 // ----------------------------------------------------------------------------
128 // ctors & dtor
129 // ----------------------------------------------------------------------------
130
131 wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
132 {
133 m_nCurLine = 0;
134 m_isOpened = FALSE;
135 }
136
137 wxTextFile::~wxTextFile()
138 {
139 // m_file dtor called automatically
140 }
141
142 // ----------------------------------------------------------------------------
143 // file operations
144 // ----------------------------------------------------------------------------
145
146 bool wxTextFile::Open(const wxString& strFile)
147 {
148 m_strFile = strFile;
149
150 return Open();
151 }
152
153 bool wxTextFile::Open()
154 {
155 // file name must be either given in ctor or in Open(const wxString&)
156 wxASSERT( !m_strFile.IsEmpty() );
157
158 // open file in read-only mode
159 if ( !m_file.Open(m_strFile) )
160 return FALSE;
161
162 // read file into memory
163 m_isOpened = Read();
164
165 m_file.Close();
166
167 return m_isOpened;
168 }
169
170 // analyse some lines of the file trying to guess it's type.
171 // if it fails, it assumes the native type for our platform.
172 wxTextFileType wxTextFile::GuessType() const
173 {
174 // file should be opened and we must be in it's beginning
175 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
176
177 // scan the file lines
178 size_t nUnix = 0, // number of '\n's alone
179 nDos = 0, // number of '\r\n'
180 nMac = 0; // number of '\r's
181
182 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
183 #define MAX_LINES_SCAN (10)
184 size_t nCount = m_aLines.Count() / 3,
185 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
186
187 #define AnalyseLine(n) \
188 switch ( m_aTypes[n] ) { \
189 case wxTextFileType_Unix: nUnix++; break; \
190 case wxTextFileType_Dos: nDos++; break; \
191 case wxTextFileType_Mac: nMac++; break; \
192 default: wxFAIL_MSG(_("unknown line terminator")); \
193 }
194
195 size_t n;
196 for ( n = 0; n < nScan; n++ ) // the beginning
197 AnalyseLine(n);
198 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
199 AnalyseLine(n);
200 for ( n = nCount - nScan; n < nCount; n++ )
201 AnalyseLine(n);
202
203 #undef AnalyseLine
204
205 // interpret the results (FIXME far from being even 50% fool proof)
206 if ( nDos + nUnix + nMac == 0 ) {
207 // no newlines at all
208 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
209 }
210 else {
211 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
212 : n##t1 > n##t2 \
213 ? wxTextFileType_##t1 \
214 : wxTextFileType_##t2
215
216 // Watcom C++ doesn't seem to be able to handle the macro
217 #if !defined(__WATCOMC__)
218 if ( nDos > nUnix )
219 return GREATER_OF(Dos, Mac);
220 else if ( nDos < nUnix )
221 return GREATER_OF(Unix, Mac);
222 else {
223 // nDos == nUnix
224 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
225 }
226 #endif // __WATCOMC__
227
228 #undef GREATER_OF
229 }
230
231 return typeDefault;
232 }
233
234 bool wxTextFile::Read()
235 {
236 // file should be opened and we must be in it's beginning
237 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
238
239 wxString str;
240 char ch, chLast = '\0';
241 char buf[1024];
242 int n, nRead;
243 while ( !m_file.Eof() ) {
244 nRead = m_file.Read(buf, WXSIZEOF(buf));
245 if ( nRead == wxInvalidOffset ) {
246 // read error (error message already given in wxFile::Read)
247 return FALSE;
248 }
249
250 for ( n = 0; n < nRead; n++ ) {
251 ch = buf[n];
252 switch ( ch ) {
253 case '\n':
254 // Dos/Unix line termination
255 m_aLines.Add(str);
256 m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos
257 : wxTextFileType_Unix);
258 str.Empty();
259 chLast = '\n';
260 break;
261
262 case '\r':
263 if ( chLast == '\r' ) {
264 // Mac empty line
265 m_aLines.Add("");
266 m_aTypes.Add(wxTextFileType_Mac);
267 }
268 else
269 chLast = '\r';
270 break;
271
272 default:
273 if ( chLast == '\r' ) {
274 // Mac line termination
275 m_aLines.Add(str);
276 m_aTypes.Add(wxTextFileType_Mac);
277 chLast = ch;
278 str = ch;
279 }
280 else {
281 // add to the current line
282 str += ch;
283 }
284 }
285 }
286 }
287
288 // anything in the last line?
289 if ( !str.IsEmpty() ) {
290 m_aTypes.Add(wxTextFileType_None); // no line terminator
291 m_aLines.Add(str);
292 }
293
294 return TRUE;
295 }
296
297 bool wxTextFile::Close()
298 {
299 m_aTypes.Clear();
300 m_aLines.Clear();
301 m_nCurLine = 0;
302 m_isOpened = FALSE;
303
304 return TRUE;
305 }
306
307 bool wxTextFile::Write(wxTextFileType typeNew)
308 {
309 wxTempFile fileTmp(m_strFile);
310
311 if ( !fileTmp.IsOpened() ) {
312 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
313 return FALSE;
314 }
315
316 size_t nCount = m_aLines.Count();
317 for ( size_t n = 0; n < nCount; n++ ) {
318 fileTmp.Write(m_aLines[n] +
319 GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n]
320 : typeNew));
321 }
322
323 // replace the old file with this one
324 return fileTmp.Commit();
325 }
326
327 #endif // wxUSE_TEXTFILE
328