]>
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__ | |
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 | // ---------------------------------------------------------------------------- | |
ba7f9a90 | 39 | |
c801d85f KB |
40 | // default type is the native one |
41 | const wxTextFile::Type wxTextFile::typeDefault = wxTextFile:: | |
34138703 | 42 | #if defined(__WINDOWS__) |
c801d85f KB |
43 | Type_Dos; |
44 | #elif defined(__UNIX__) | |
45 | Type_Unix; | |
34138703 | 46 | #elif defined(__WXMAC__) |
c801d85f KB |
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 | |
c86f1403 | 104 | size_t nUnix = 0, // number of '\n's alone |
c801d85f KB |
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) | |
c86f1403 | 110 | size_t nCount = m_aLines.Count() / 3, |
c801d85f KB |
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; \ | |
1a5a8367 | 118 | default: wxFAIL_MSG(_("unknown line terminator")); \ |
c801d85f KB |
119 | } |
120 | ||
c86f1403 | 121 | size_t n; |
c801d85f KB |
122 | for ( n = 0; n < nScan; n++ ) // the beginning |
123 | AnalyseLine(n); | |
124 | for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ ) | |
125 | AnalyseLine(n); | |
126 | for ( n = nCount - nScan; n < nCount; n++ ) | |
127 | AnalyseLine(n); | |
128 | ||
129 | #undef AnalyseLine | |
130 | ||
131 | // interpret the results (@@ far from being even 50% fool proof) | |
132 | if ( nDos + nUnix + nMac == 0 ) { | |
133 | // no newlines at all | |
1a5a8367 | 134 | wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str()); |
c801d85f KB |
135 | } |
136 | else { | |
137 | #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \ | |
138 | : n##t1 > n##t2 ? Type_##t1 \ | |
139 | : Type_##t2 | |
140 | ||
141 | if ( nDos > nUnix ) | |
142 | return GREATER_OF(Dos, Mac); | |
143 | else if ( nDos < nUnix ) | |
144 | return GREATER_OF(Unix, Mac); | |
145 | else { | |
146 | // nDos == nUnix | |
147 | return nMac > nDos ? Type_Mac : typeDefault; | |
148 | } | |
149 | ||
150 | #undef GREATER_OF | |
151 | } | |
152 | ||
153 | return typeDefault; | |
154 | } | |
155 | ||
156 | bool wxTextFile::Read() | |
157 | { | |
158 | // file should be opened and we must be in it's beginning | |
159 | wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); | |
160 | ||
161 | wxString str; | |
162 | char ch, chLast = '\0'; | |
ba7f9a90 VZ |
163 | char buf[1024]; |
164 | int n, nRead; | |
c801d85f | 165 | while ( !m_file.Eof() ) { |
ba7f9a90 | 166 | nRead = m_file.Read(buf, WXSIZEOF(buf)); |
1678ad78 | 167 | if ( nRead == wxInvalidOffset ) { |
ba7f9a90 | 168 | // read error (error message already given in wxFile::Read) |
c801d85f KB |
169 | m_file.Close(); |
170 | return FALSE; | |
171 | } | |
172 | ||
173 | #ifdef __MAC__ | |
174 | #pragma message("wxTextFile::Read() hasn't been tested with Mac files.") | |
175 | #endif | |
176 | ||
ba7f9a90 VZ |
177 | for ( n = 0; n < nRead; n++ ) { |
178 | ch = buf[n]; | |
179 | switch ( ch ) { | |
180 | case '\n': | |
181 | // Dos/Unix line termination | |
c801d85f | 182 | m_aLines.Add(str); |
ba7f9a90 VZ |
183 | m_aTypes.Add(chLast == '\r' ? Type_Dos : Type_Unix); |
184 | str.Empty(); | |
185 | chLast = '\n'; | |
186 | break; | |
187 | ||
188 | case '\r': | |
189 | if ( chLast == '\r' ) { | |
190 | // Mac empty line | |
191 | m_aLines.Add(""); | |
192 | m_aTypes.Add(Type_Mac); | |
193 | } | |
194 | else | |
195 | chLast = '\r'; | |
196 | break; | |
197 | ||
198 | default: | |
199 | if ( chLast == '\r' ) { | |
200 | // Mac line termination | |
201 | m_aLines.Add(str); | |
202 | m_aTypes.Add(Type_Mac); | |
203 | str = ch; | |
204 | } | |
205 | else { | |
206 | // add to the current line | |
207 | str += ch; | |
208 | } | |
209 | } | |
c801d85f KB |
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() ) { | |
1a5a8367 | 227 | wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str()); |
c801d85f KB |
228 | return FALSE; |
229 | } | |
230 | ||
c86f1403 VZ |
231 | size_t nCount = m_aLines.Count(); |
232 | for ( size_t n = 0; n < nCount; n++ ) { | |
ba7f9a90 | 233 | fileTmp.Write(m_aLines[n] + |
c801d85f KB |
234 | GetEOL(typeNew == Type_None ? m_aTypes[n] : typeNew)); |
235 | } | |
236 | ||
237 | // replace the old file with this one | |
238 | return fileTmp.Commit(); | |
ba7f9a90 | 239 | } |