]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
Partial Watcom C++ 10.6 support added (doesn't link for some reason)
[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 wxTextFile::Type wxTextFile::typeDefault = wxTextFile::
42 #if defined(__WINDOWS__)
43 Type_Dos;
44 #elif defined(__UNIX__)
45 Type_Unix;
46 #elif defined(__WXMAC__)
47 Type_Mac;
48 #else
49 Type_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 return Open();
75 }
76
77 bool wxTextFile::Open()
78 {
79 // file name must be either given in ctor or in Open(const wxString&)
80 wxASSERT( !m_strFile.IsEmpty() );
81
82 // open file in read-only mode
83 if ( !m_file.Open(m_strFile) )
84 return FALSE;
85
86 // read file into memory
87 bool bRet = Read();
88
89 m_file.Close();
90
91 return bRet;
92 }
93
94 // analyse some lines of the file trying to guess it's type.
95 // if it fails, it assumes the native type for our platform.
96 wxTextFile::Type wxTextFile::GuessType() const
97 {
98 // file should be opened and we must be in it's beginning
99 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
100
101 // scan the file lines
102 size_t nUnix = 0, // number of '\n's alone
103 nDos = 0, // number of '\r\n'
104 nMac = 0; // number of '\r's
105
106 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
107 #define MAX_LINES_SCAN (10)
108 size_t nCount = m_aLines.Count() / 3,
109 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
110
111 #define AnalyseLine(n) \
112 switch ( m_aTypes[n] ) { \
113 case Type_Unix: nUnix++; break; \
114 case Type_Dos: nDos++; break; \
115 case Type_Mac: nMac++; break; \
116 default: wxFAIL_MSG(_("unknown line terminator")); \
117 }
118
119 size_t n;
120 for ( n = 0; n < nScan; n++ ) // the beginning
121 AnalyseLine(n);
122 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
123 AnalyseLine(n);
124 for ( n = nCount - nScan; n < nCount; n++ )
125 AnalyseLine(n);
126
127 #undef AnalyseLine
128
129 // interpret the results (@@ far from being even 50% fool proof)
130 if ( nDos + nUnix + nMac == 0 ) {
131 // no newlines at all
132 wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str());
133 }
134 else {
135 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
136 : n##t1 > n##t2 ? Type_##t1 \
137 : Type_##t2
138
139 // Watcom C++ doesn't seem to be able to handle the macro
140 #if defined(__WATCOMC__)
141 return typeDefault;
142 #else
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 ? Type_Mac : typeDefault;
150 }
151 #endif
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' ? Type_Dos : Type_Unix);
183 str.Empty();
184 chLast = '\n';
185 break;
186
187 case '\r':
188 if ( chLast == '\r' ) {
189 // Mac empty line
190 m_aLines.Add("");
191 m_aTypes.Add(Type_Mac);
192 }
193 else
194 chLast = '\r';
195 break;
196
197 default:
198 if ( chLast == '\r' ) {
199 // Mac line termination
200 m_aLines.Add(str);
201 m_aTypes.Add(Type_Mac);
202 chLast = ch;
203 str = ch;
204 }
205 else {
206 // add to the current line
207 str += ch;
208 }
209 }
210 }
211 }
212
213 // anything in the last line?
214 if ( !str.IsEmpty() ) {
215 m_aTypes.Add(Type_None); // no line terminator
216 m_aLines.Add(str);
217 }
218
219 return TRUE;
220 }
221
222 bool wxTextFile::Write(Type typeNew)
223 {
224 wxTempFile fileTmp(m_strFile);
225
226 if ( !fileTmp.IsOpened() ) {
227 wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str());
228 return FALSE;
229 }
230
231 size_t nCount = m_aLines.Count();
232 for ( size_t n = 0; n < nCount; n++ ) {
233 fileTmp.Write(m_aLines[n] +
234 GetEOL(typeNew == Type_None ? m_aTypes[n] : typeNew));
235 }
236
237 // replace the old file with this one
238 return fileTmp.Commit();
239 }
240
241 const char *wxTextFile::GetEOL(Type type)
242 {
243 switch ( type ) {
244 case Type_None: return "";
245 case Type_Unix: return "\n";
246 case Type_Dos: return "\r\n";
247 case Type_Mac: return "\r";
248
249 default:
250 wxFAIL_MSG("bad file type in wxTextFile::GetEOL.");
251 return (const char *) NULL;
252 }
253 }
254