]> git.saurik.com Git - wxWidgets.git/blame - src/common/textfile.cpp
attempts to fix wxStaticBitmap::SetBitmap() - still doesn't work
[wxWidgets.git] / src / common / textfile.cpp
CommitLineData
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__
17#pragma implementation "textfile.h"
18#endif
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif //__BORLANDC__
25
ce4169a4
RR
26#ifndef WX_PRECOMP
27#include "wx/defs.h"
28#endif
29
30#if wxUSE_TEXTFILE && wxUSE_FILE
31
c801d85f
KB
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// ----------------------------------------------------------------------------
ba7f9a90 45
c801d85f 46// default type is the native one
c2389495 47const wxTextFileType wxTextFile::typeDefault =
34138703 48#if defined(__WINDOWS__)
c2389495 49 wxTextFileType_Dos;
c801d85f 50#elif defined(__UNIX__)
c2389495 51 wxTextFileType_Unix;
34138703 52#elif defined(__WXMAC__)
c2389495 53 wxTextFileType_Mac;
c801d85f 54#else
c2389495 55 wxTextFileType_None;
c801d85f
KB
56 #error "wxTextFile: unsupported platform."
57#endif
58
59
60// ----------------------------------------------------------------------------
61// ctors & dtor
62// ----------------------------------------------------------------------------
63
64wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
65{
66}
67
68wxTextFile::~wxTextFile()
69{
70 // m_file dtor called automatically
71}
72
73// ----------------------------------------------------------------------------
74// file operations
75// ----------------------------------------------------------------------------
76
77bool wxTextFile::Open(const wxString& strFile)
78{
79 m_strFile = strFile;
f42d2aba 80
c801d85f
KB
81 return Open();
82}
83
84bool wxTextFile::Open()
85{
86 // file name must be either given in ctor or in Open(const wxString&)
87 wxASSERT( !m_strFile.IsEmpty() );
88
89 // open file in read-only mode
90 if ( !m_file.Open(m_strFile) )
91 return FALSE;
92
93 // read file into memory
94 bool bRet = Read();
95
96 m_file.Close();
97
98 return bRet;
99}
100
101// analyse some lines of the file trying to guess it's type.
102// if it fails, it assumes the native type for our platform.
c2389495 103wxTextFileType wxTextFile::GuessType() const
c801d85f
KB
104{
105 // file should be opened and we must be in it's beginning
106 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
107
108 // scan the file lines
c86f1403 109 size_t nUnix = 0, // number of '\n's alone
c801d85f
KB
110 nDos = 0, // number of '\r\n'
111 nMac = 0; // number of '\r's
112
113 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
114 #define MAX_LINES_SCAN (10)
c86f1403 115 size_t nCount = m_aLines.Count() / 3,
c801d85f
KB
116 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
117
118 #define AnalyseLine(n) \
119 switch ( m_aTypes[n] ) { \
c2389495
VZ
120 case wxTextFileType_Unix: nUnix++; break; \
121 case wxTextFileType_Dos: nDos++; break; \
122 case wxTextFileType_Mac: nMac++; break; \
1a5a8367 123 default: wxFAIL_MSG(_("unknown line terminator")); \
c801d85f
KB
124 }
125
c86f1403 126 size_t n;
c801d85f
KB
127 for ( n = 0; n < nScan; n++ ) // the beginning
128 AnalyseLine(n);
129 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
130 AnalyseLine(n);
131 for ( n = nCount - nScan; n < nCount; n++ )
132 AnalyseLine(n);
133
134 #undef AnalyseLine
135
136 // interpret the results (@@ far from being even 50% fool proof)
137 if ( nDos + nUnix + nMac == 0 ) {
138 // no newlines at all
1a5a8367 139 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
c801d85f
KB
140 }
141 else {
142 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
c2389495 143 : n##t1 > n##t2 \
f42d2aba 144 ? wxTextFileType_##t1 \
c2389495 145 : wxTextFileType_##t2
c801d85f 146
f42d2aba
VZ
147 // Watcom C++ doesn't seem to be able to handle the macro
148#if !defined(__WATCOMC__)
c801d85f
KB
149 if ( nDos > nUnix )
150 return GREATER_OF(Dos, Mac);
151 else if ( nDos < nUnix )
152 return GREATER_OF(Unix, Mac);
153 else {
154 // nDos == nUnix
c2389495 155 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
c801d85f 156 }
f42d2aba 157#endif // __WATCOMC__
c801d85f
KB
158
159 #undef GREATER_OF
160 }
161
162 return typeDefault;
163}
164
165bool wxTextFile::Read()
166{
167 // file should be opened and we must be in it's beginning
168 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
169
170 wxString str;
171 char ch, chLast = '\0';
ba7f9a90
VZ
172 char buf[1024];
173 int n, nRead;
c801d85f 174 while ( !m_file.Eof() ) {
ba7f9a90 175 nRead = m_file.Read(buf, WXSIZEOF(buf));
1678ad78 176 if ( nRead == wxInvalidOffset ) {
ba7f9a90 177 // read error (error message already given in wxFile::Read)
c801d85f
KB
178 m_file.Close();
179 return FALSE;
180 }
181
ba7f9a90
VZ
182 for ( n = 0; n < nRead; n++ ) {
183 ch = buf[n];
184 switch ( ch ) {
185 case '\n':
186 // Dos/Unix line termination
c801d85f 187 m_aLines.Add(str);
c2389495
VZ
188 m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos
189 : wxTextFileType_Unix);
ba7f9a90
VZ
190 str.Empty();
191 chLast = '\n';
192 break;
193
194 case '\r':
195 if ( chLast == '\r' ) {
196 // Mac empty line
197 m_aLines.Add("");
c2389495 198 m_aTypes.Add(wxTextFileType_Mac);
ba7f9a90
VZ
199 }
200 else
201 chLast = '\r';
202 break;
203
204 default:
205 if ( chLast == '\r' ) {
206 // Mac line termination
207 m_aLines.Add(str);
c2389495 208 m_aTypes.Add(wxTextFileType_Mac);
7749df1f 209 chLast = ch;
ba7f9a90
VZ
210 str = ch;
211 }
212 else {
213 // add to the current line
214 str += ch;
215 }
216 }
c801d85f
KB
217 }
218 }
219
220 // anything in the last line?
221 if ( !str.IsEmpty() ) {
c2389495 222 m_aTypes.Add(wxTextFileType_None); // no line terminator
c801d85f
KB
223 m_aLines.Add(str);
224 }
225
226 return TRUE;
227}
228
f42d2aba
VZ
229bool wxTextFile::Close()
230{
231 m_aTypes.Clear();
232 m_aLines.Clear();
233 m_nCurLine = 0;
234
235 return TRUE;
236}
237
c2389495 238bool wxTextFile::Write(wxTextFileType typeNew)
c801d85f
KB
239{
240 wxTempFile fileTmp(m_strFile);
241
242 if ( !fileTmp.IsOpened() ) {
1a5a8367 243 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
c801d85f
KB
244 return FALSE;
245 }
246
c86f1403
VZ
247 size_t nCount = m_aLines.Count();
248 for ( size_t n = 0; n < nCount; n++ ) {
ba7f9a90 249 fileTmp.Write(m_aLines[n] +
c2389495
VZ
250 GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n]
251 : typeNew));
c801d85f
KB
252 }
253
254 // replace the old file with this one
255 return fileTmp.Commit();
ba7f9a90 256}
6164d85e 257
e296ac9f 258const wxChar *wxTextFile::GetEOL(wxTextFileType type)
ce4169a4 259{
6164d85e 260 switch ( type ) {
e296ac9f
OK
261 case wxTextFileType_None: return _T("");
262 case wxTextFileType_Unix: return _T("\n");
263 case wxTextFileType_Dos: return _T("\r\n");
264 case wxTextFileType_Mac: return _T("\r");
6164d85e
JS
265
266 default:
e296ac9f
OK
267 wxFAIL_MSG(_T("bad file type in wxTextFile::GetEOL."));
268 return (const wxChar *) NULL;
6164d85e 269 }
ce4169a4 270}
6164d85e 271
ce4169a4 272#endif