]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
ExpandEnvVars was eating backslashes - no more.
[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(__WXMSW__)
43 Type_Dos;
44 #elif defined(__UNIX__)
45 Type_Unix;
46 #elif defined(__MAC__)
47 Type_Mac;
48 // if you feel brave, remove the next line
49 #error "wxTextFile: code for Mac files is untested."
50 #else
51 Type_None;
52 #error "wxTextFile: unsupported platform."
53 #endif
54
55
56 // ----------------------------------------------------------------------------
57 // ctors & dtor
58 // ----------------------------------------------------------------------------
59
60 wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile)
61 {
62 }
63
64 wxTextFile::~wxTextFile()
65 {
66 // m_file dtor called automatically
67 }
68
69 // ----------------------------------------------------------------------------
70 // file operations
71 // ----------------------------------------------------------------------------
72
73 bool wxTextFile::Open(const wxString& strFile)
74 {
75 m_strFile = strFile;
76 return Open();
77 }
78
79 bool wxTextFile::Open()
80 {
81 // file name must be either given in ctor or in Open(const wxString&)
82 wxASSERT( !m_strFile.IsEmpty() );
83
84 // open file in read-only mode
85 if ( !m_file.Open(m_strFile) )
86 return FALSE;
87
88 // read file into memory
89 bool bRet = Read();
90
91 m_file.Close();
92
93 return bRet;
94 }
95
96 // analyse some lines of the file trying to guess it's type.
97 // if it fails, it assumes the native type for our platform.
98 wxTextFile::Type wxTextFile::GuessType() const
99 {
100 // file should be opened and we must be in it's beginning
101 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
102
103 // scan the file lines
104 uint nUnix = 0, // number of '\n's alone
105 nDos = 0, // number of '\r\n'
106 nMac = 0; // number of '\r's
107
108 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
109 #define MAX_LINES_SCAN (10)
110 uint nCount = m_aLines.Count() / 3,
111 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
112
113 #define AnalyseLine(n) \
114 switch ( m_aTypes[n] ) { \
115 case Type_Unix: nUnix++; break; \
116 case Type_Dos: nDos++; break; \
117 case Type_Mac: nMac++; break; \
118 }
119
120 uint n;
121 for ( n = 0; n < nScan; n++ ) // the beginning
122 AnalyseLine(n);
123 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
124 AnalyseLine(n);
125 for ( n = nCount - nScan; n < nCount; n++ )
126 AnalyseLine(n);
127
128 #undef AnalyseLine
129
130 // interpret the results (@@ far from being even 50% fool proof)
131 if ( nDos + nUnix + nMac == 0 ) {
132 // no newlines at all
133 wxLogWarning("'%s' is probably a binary file.", m_strFile.c_str());
134 }
135 else {
136 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
137 : n##t1 > n##t2 ? Type_##t1 \
138 : Type_##t2
139
140 if ( nDos > nUnix )
141 return GREATER_OF(Dos, Mac);
142 else if ( nDos < nUnix )
143 return GREATER_OF(Unix, Mac);
144 else {
145 // nDos == nUnix
146 return nMac > nDos ? Type_Mac : typeDefault;
147 }
148
149 #undef GREATER_OF
150 }
151
152 return typeDefault;
153 }
154
155 bool wxTextFile::Read()
156 {
157 // file should be opened and we must be in it's beginning
158 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
159
160 wxString str;
161 char ch, chLast = '\0';
162 while ( !m_file.Eof() ) {
163 // @@ should really use a buffer for efficiency
164 if ( m_file.Read(&ch, sizeof(ch)) == ofsInvalid ) {
165 // read error
166 m_file.Close();
167 return FALSE;
168 }
169
170 #ifdef __MAC__
171 #pragma message("wxTextFile::Read() hasn't been tested with Mac files.")
172 #endif
173
174 switch ( ch ) {
175 case '\n':
176 // Dos/Unix line termination
177 m_aLines.Add(str);
178 m_aTypes.Add(chLast == '\r' ? Type_Dos : Type_Unix);
179 str.Empty();
180 chLast = '\n';
181 break;
182
183 case '\r':
184 if ( chLast == '\r' ) {
185 // Mac empty line
186 m_aLines.Add("");
187 m_aTypes.Add(Type_Mac);
188 }
189 else
190 chLast = '\r';
191 break;
192
193 default:
194 if ( chLast == '\r' ) {
195 // Mac line termination
196 m_aLines.Add(str);
197 m_aTypes.Add(Type_Mac);
198 str = ch;
199 }
200 else {
201 // add to the current line
202 str += ch;
203 }
204 }
205 }
206
207 // anything in the last line?
208 if ( !str.IsEmpty() ) {
209 m_aTypes.Add(Type_None); // no line terminator
210 m_aLines.Add(str);
211 }
212
213 return TRUE;
214 }
215
216 bool wxTextFile::Write(Type typeNew)
217 {
218 wxTempFile fileTmp(m_strFile);
219
220 if ( !fileTmp.IsOpened() ) {
221 wxLogError("can't write file '%s' to disk.", m_strFile.c_str());
222 return FALSE;
223 }
224
225 uint nCount = m_aLines.Count();
226 for ( uint n = 0; n < nCount; n++ ) {
227 fileTmp.Write(m_aLines[n] +
228 GetEOL(typeNew == Type_None ? m_aTypes[n] : typeNew));
229 }
230
231 // replace the old file with this one
232 return fileTmp.Commit();
233 }