Commit | Line | Data |
---|---|---|
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__ | |
a1b82138 | 17 | #pragma implementation "textfile.h" |
c801d85f KB |
18 | #endif |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
a1b82138 | 23 | #pragma hdrstop |
c801d85f KB |
24 | #endif //__BORLANDC__ |
25 | ||
a1b82138 VZ |
26 | #if !wxUSE_FILE |
27 | #undef wxUSE_TEXTFILE | |
28 | #define wxUSE_TEXTFILE 0 | |
29 | #endif // wxUSE_FILE | |
30 | ||
ce4169a4 | 31 | #ifndef WX_PRECOMP |
a1b82138 VZ |
32 | #include "wx/string.h" |
33 | #include "wx/intl.h" | |
34 | #include "wx/file.h" | |
35 | #include "wx/log.h" | |
ce4169a4 RR |
36 | #endif |
37 | ||
a1b82138 | 38 | #include "wx/textfile.h" |
c801d85f KB |
39 | |
40 | // ============================================================================ | |
41 | // wxTextFile class implementation | |
42 | // ============================================================================ | |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
a1b82138 | 45 | // static methods (always compiled in) |
c801d85f | 46 | // ---------------------------------------------------------------------------- |
ba7f9a90 | 47 | |
c801d85f | 48 | // default type is the native one |
c2389495 | 49 | const wxTextFileType wxTextFile::typeDefault = |
34138703 | 50 | #if defined(__WINDOWS__) |
c2389495 | 51 | wxTextFileType_Dos; |
c801d85f | 52 | #elif defined(__UNIX__) |
c2389495 | 53 | wxTextFileType_Unix; |
34138703 | 54 | #elif defined(__WXMAC__) |
c2389495 | 55 | wxTextFileType_Mac; |
717b9bf2 DW |
56 | #elif defined(__WXPM__) |
57 | wxTextFileType_Os2; | |
c801d85f | 58 | #else |
c2389495 | 59 | wxTextFileType_None; |
c801d85f KB |
60 | #error "wxTextFile: unsupported platform." |
61 | #endif | |
62 | ||
a1b82138 VZ |
63 | const wxChar *wxTextFile::GetEOL(wxTextFileType type) |
64 | { | |
65 | switch ( type ) { | |
66 | default: | |
223d09f6 | 67 | wxFAIL_MSG(wxT("bad file type in wxTextFile::GetEOL.")); |
a1b82138 VZ |
68 | // fall through nevertheless - we must return something... |
69 | ||
f6bcfd97 BP |
70 | case wxTextFileType_None: return wxT(""); |
71 | case wxTextFileType_Unix: return wxT("\n"); | |
72 | case wxTextFileType_Dos: return wxT("\r\n"); | |
73 | case wxTextFileType_Mac: return wxT("\r"); | |
a1b82138 VZ |
74 | } |
75 | } | |
76 | ||
a1b82138 VZ |
77 | wxString wxTextFile::Translate(const wxString& text, wxTextFileType type) |
78 | { | |
79 | // don't do anything if there is nothing to do | |
80 | if ( type == wxTextFileType_None ) | |
81 | return text; | |
82 | ||
967a322f GRG |
83 | // GRG: don't do anything either if it is empty |
84 | if ( text.IsEmpty() ) | |
85 | return text; | |
86 | ||
a1b82138 VZ |
87 | wxString eol = GetEOL(type), result; |
88 | ||
89 | // optimization: we know that the length of the new string will be about | |
90 | // the same as the length of the old one, so prealloc memory to aviod | |
91 | // unnecessary relocations | |
92 | result.Alloc(text.Len()); | |
93 | ||
94 | wxChar chLast = 0; | |
95 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) | |
96 | { | |
97 | wxChar ch = *pc; | |
98 | switch ( ch ) { | |
c0f587c5 | 99 | case _T('\n'): |
a1b82138 VZ |
100 | // Dos/Unix line termination |
101 | result += eol; | |
c0f587c5 | 102 | chLast = 0; |
a1b82138 VZ |
103 | break; |
104 | ||
c0f587c5 VZ |
105 | case _T('\r'): |
106 | if ( chLast == _T('\r') ) { | |
a1b82138 VZ |
107 | // Mac empty line |
108 | result += eol; | |
109 | } | |
c0f587c5 VZ |
110 | else { |
111 | // just remember it: we don't know whether it is just "\r" | |
112 | // or "\r\n" yet | |
113 | chLast = _T('\r'); | |
114 | } | |
a1b82138 VZ |
115 | break; |
116 | ||
117 | default: | |
c0f587c5 | 118 | if ( chLast == _T('\r') ) { |
a1b82138 VZ |
119 | // Mac line termination |
120 | result += eol; | |
b01ca3e3 VZ |
121 | |
122 | // reset chLast to avoid inserting another eol before the | |
123 | // next character | |
124 | chLast = 0; | |
a1b82138 | 125 | } |
c0f587c5 VZ |
126 | |
127 | // add to the current line | |
128 | result += ch; | |
a1b82138 VZ |
129 | } |
130 | } | |
131 | ||
c0f587c5 VZ |
132 | if ( chLast ) { |
133 | // trailing '\r' | |
134 | result += eol; | |
135 | } | |
136 | ||
a1b82138 VZ |
137 | return result; |
138 | } | |
139 | ||
140 | #if wxUSE_TEXTFILE | |
c801d85f KB |
141 | |
142 | // ---------------------------------------------------------------------------- | |
143 | // ctors & dtor | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
146 | wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile) | |
147 | { | |
7cc98b3e VZ |
148 | m_nCurLine = 0; |
149 | m_isOpened = FALSE; | |
c801d85f KB |
150 | } |
151 | ||
152 | wxTextFile::~wxTextFile() | |
153 | { | |
154 | // m_file dtor called automatically | |
155 | } | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // file operations | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
ef8d96c2 VZ |
161 | bool wxTextFile::Exists() const |
162 | { | |
163 | return wxFile::Exists(m_strFile); | |
164 | } | |
165 | ||
1b6dea5d MB |
166 | bool wxTextFile::Create(const wxString& strFile) |
167 | { | |
168 | m_strFile = strFile; | |
169 | ||
170 | return Create(); | |
171 | } | |
172 | ||
173 | bool wxTextFile::Create() | |
174 | { | |
175 | // file name must be either given in ctor or in Create(const wxString&) | |
176 | wxASSERT( !m_strFile.IsEmpty() ); | |
177 | ||
178 | // if the file already exists do nothing | |
179 | if ( Exists() ) return FALSE; | |
180 | ||
181 | if ( m_file.Open(m_strFile, wxFile::write) ) | |
182 | { | |
183 | m_file.Close(); | |
184 | return TRUE; | |
185 | } | |
186 | else | |
187 | { | |
188 | return FALSE; | |
189 | } | |
190 | } | |
191 | ||
adfed1ca | 192 | bool wxTextFile::Open(const wxString& strFile, wxMBConv& conv) |
c801d85f KB |
193 | { |
194 | m_strFile = strFile; | |
f42d2aba | 195 | |
adfed1ca | 196 | return Open(conv); |
c801d85f KB |
197 | } |
198 | ||
adfed1ca | 199 | bool wxTextFile::Open(wxMBConv& conv) |
c801d85f KB |
200 | { |
201 | // file name must be either given in ctor or in Open(const wxString&) | |
202 | wxASSERT( !m_strFile.IsEmpty() ); | |
203 | ||
204 | // open file in read-only mode | |
205 | if ( !m_file.Open(m_strFile) ) | |
206 | return FALSE; | |
207 | ||
208 | // read file into memory | |
adfed1ca | 209 | m_isOpened = Read(conv); |
c801d85f KB |
210 | |
211 | m_file.Close(); | |
212 | ||
7cc98b3e | 213 | return m_isOpened; |
c801d85f KB |
214 | } |
215 | ||
216 | // analyse some lines of the file trying to guess it's type. | |
217 | // if it fails, it assumes the native type for our platform. | |
c2389495 | 218 | wxTextFileType wxTextFile::GuessType() const |
c801d85f | 219 | { |
4d5cd6e0 | 220 | wxASSERT( IsOpened() ); |
c801d85f KB |
221 | |
222 | // scan the file lines | |
c86f1403 | 223 | size_t nUnix = 0, // number of '\n's alone |
adfed1ca VS |
224 | nDos = 0, // number of '\r\n' |
225 | nMac = 0; // number of '\r's | |
c801d85f KB |
226 | |
227 | // we take MAX_LINES_SCAN in the beginning, middle and the end of file | |
228 | #define MAX_LINES_SCAN (10) | |
c86f1403 | 229 | size_t nCount = m_aLines.Count() / 3, |
c801d85f KB |
230 | nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3; |
231 | ||
232 | #define AnalyseLine(n) \ | |
233 | switch ( m_aTypes[n] ) { \ | |
c2389495 VZ |
234 | case wxTextFileType_Unix: nUnix++; break; \ |
235 | case wxTextFileType_Dos: nDos++; break; \ | |
236 | case wxTextFileType_Mac: nMac++; break; \ | |
1a5a8367 | 237 | default: wxFAIL_MSG(_("unknown line terminator")); \ |
c801d85f KB |
238 | } |
239 | ||
c86f1403 | 240 | size_t n; |
c801d85f KB |
241 | for ( n = 0; n < nScan; n++ ) // the beginning |
242 | AnalyseLine(n); | |
243 | for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ ) | |
244 | AnalyseLine(n); | |
245 | for ( n = nCount - nScan; n < nCount; n++ ) | |
246 | AnalyseLine(n); | |
247 | ||
248 | #undef AnalyseLine | |
249 | ||
a1b82138 | 250 | // interpret the results (FIXME far from being even 50% fool proof) |
adfed1ca | 251 | if ( nScan > 0 && nDos + nUnix + nMac == 0 ) { |
c801d85f | 252 | // no newlines at all |
1a5a8367 | 253 | wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str()); |
c801d85f KB |
254 | } |
255 | else { | |
256 | #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \ | |
c2389495 | 257 | : n##t1 > n##t2 \ |
f42d2aba | 258 | ? wxTextFileType_##t1 \ |
c2389495 | 259 | : wxTextFileType_##t2 |
c801d85f | 260 | |
f42d2aba VZ |
261 | // Watcom C++ doesn't seem to be able to handle the macro |
262 | #if !defined(__WATCOMC__) | |
c801d85f KB |
263 | if ( nDos > nUnix ) |
264 | return GREATER_OF(Dos, Mac); | |
265 | else if ( nDos < nUnix ) | |
266 | return GREATER_OF(Unix, Mac); | |
267 | else { | |
268 | // nDos == nUnix | |
c2389495 | 269 | return nMac > nDos ? wxTextFileType_Mac : typeDefault; |
c801d85f | 270 | } |
f42d2aba | 271 | #endif // __WATCOMC__ |
c801d85f KB |
272 | |
273 | #undef GREATER_OF | |
274 | } | |
275 | ||
276 | return typeDefault; | |
277 | } | |
278 | ||
adfed1ca | 279 | bool wxTextFile::Read(wxMBConv& conv) |
c801d85f KB |
280 | { |
281 | // file should be opened and we must be in it's beginning | |
282 | wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); | |
283 | ||
adfed1ca VS |
284 | #if wxUSE_UNICODE |
285 | char conv_mbBuf[2]; | |
286 | wchar_t conv_wcBuf[2]; | |
287 | conv_mbBuf[1] = 0; | |
288 | #else | |
289 | (void)conv; | |
290 | #endif | |
291 | ||
c801d85f KB |
292 | wxString str; |
293 | char ch, chLast = '\0'; | |
ba7f9a90 VZ |
294 | char buf[1024]; |
295 | int n, nRead; | |
f6bcfd97 | 296 | do { |
ba7f9a90 | 297 | nRead = m_file.Read(buf, WXSIZEOF(buf)); |
1678ad78 | 298 | if ( nRead == wxInvalidOffset ) { |
ba7f9a90 | 299 | // read error (error message already given in wxFile::Read) |
c801d85f KB |
300 | return FALSE; |
301 | } | |
302 | ||
ba7f9a90 VZ |
303 | for ( n = 0; n < nRead; n++ ) { |
304 | ch = buf[n]; | |
305 | switch ( ch ) { | |
306 | case '\n': | |
307 | // Dos/Unix line termination | |
c801d85f | 308 | m_aLines.Add(str); |
c2389495 VZ |
309 | m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos |
310 | : wxTextFileType_Unix); | |
ba7f9a90 VZ |
311 | str.Empty(); |
312 | chLast = '\n'; | |
313 | break; | |
314 | ||
315 | case '\r': | |
316 | if ( chLast == '\r' ) { | |
317 | // Mac empty line | |
c0f587c5 | 318 | m_aLines.Add(wxEmptyString); |
c2389495 | 319 | m_aTypes.Add(wxTextFileType_Mac); |
ba7f9a90 VZ |
320 | } |
321 | else | |
322 | chLast = '\r'; | |
323 | break; | |
324 | ||
325 | default: | |
326 | if ( chLast == '\r' ) { | |
327 | // Mac line termination | |
328 | m_aLines.Add(str); | |
c2389495 | 329 | m_aTypes.Add(wxTextFileType_Mac); |
7749df1f | 330 | chLast = ch; |
adfed1ca VS |
331 | #if wxUSE_UNICODE |
332 | if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) | |
333 | conv_wcBuf[0] = ch; | |
334 | str = conv_wcBuf[0]; | |
335 | #else | |
ba7f9a90 | 336 | str = ch; |
adfed1ca | 337 | #endif |
ba7f9a90 VZ |
338 | } |
339 | else { | |
340 | // add to the current line | |
adfed1ca VS |
341 | #if wxUSE_UNICODE |
342 | if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) | |
343 | conv_wcBuf[0] = ch; | |
344 | str += conv_wcBuf[0]; | |
345 | #else | |
ba7f9a90 | 346 | str += ch; |
adfed1ca | 347 | #endif |
ba7f9a90 VZ |
348 | } |
349 | } | |
c801d85f | 350 | } |
f6bcfd97 | 351 | } while ( nRead == WXSIZEOF(buf) ); |
c801d85f KB |
352 | |
353 | // anything in the last line? | |
354 | if ( !str.IsEmpty() ) { | |
c2389495 | 355 | m_aTypes.Add(wxTextFileType_None); // no line terminator |
c801d85f KB |
356 | m_aLines.Add(str); |
357 | } | |
358 | ||
359 | return TRUE; | |
360 | } | |
361 | ||
f42d2aba VZ |
362 | bool wxTextFile::Close() |
363 | { | |
364 | m_aTypes.Clear(); | |
365 | m_aLines.Clear(); | |
366 | m_nCurLine = 0; | |
7cc98b3e | 367 | m_isOpened = FALSE; |
f42d2aba VZ |
368 | |
369 | return TRUE; | |
370 | } | |
371 | ||
adfed1ca | 372 | bool wxTextFile::Write(wxTextFileType typeNew, wxMBConv& conv) |
c801d85f KB |
373 | { |
374 | wxTempFile fileTmp(m_strFile); | |
375 | ||
376 | if ( !fileTmp.IsOpened() ) { | |
1a5a8367 | 377 | wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str()); |
c801d85f KB |
378 | return FALSE; |
379 | } | |
380 | ||
c86f1403 VZ |
381 | size_t nCount = m_aLines.Count(); |
382 | for ( size_t n = 0; n < nCount; n++ ) { | |
ba7f9a90 | 383 | fileTmp.Write(m_aLines[n] + |
c2389495 | 384 | GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n] |
adfed1ca | 385 | : typeNew), conv); |
c801d85f KB |
386 | } |
387 | ||
388 | // replace the old file with this one | |
389 | return fileTmp.Commit(); | |
ba7f9a90 | 390 | } |
6164d85e | 391 | |
a1b82138 | 392 | #endif // wxUSE_TEXTFILE |
6164d85e | 393 |