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