]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/textfile.cpp | |
3 | // Purpose: implementation of wxTextFile class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 03.04.98 | |
7 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // headers | |
13 | // ============================================================================ | |
14 | ||
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif //__BORLANDC__ | |
20 | ||
21 | #if !wxUSE_FILE || !wxUSE_TEXTBUFFER | |
22 | #undef wxUSE_TEXTFILE | |
23 | #define wxUSE_TEXTFILE 0 | |
24 | #endif // wxUSE_FILE | |
25 | ||
26 | #if wxUSE_TEXTFILE | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/string.h" | |
30 | #include "wx/intl.h" | |
31 | #include "wx/file.h" | |
32 | #include "wx/log.h" | |
33 | #endif | |
34 | ||
35 | #include "wx/textfile.h" | |
36 | #include "wx/filename.h" | |
37 | #include "wx/buffer.h" | |
38 | ||
39 | // ============================================================================ | |
40 | // wxTextFile class implementation | |
41 | // ============================================================================ | |
42 | ||
43 | wxTextFile::wxTextFile(const wxString& strFileName) | |
44 | : wxTextBuffer(strFileName) | |
45 | { | |
46 | } | |
47 | ||
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // file operations | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | bool wxTextFile::OnExists() const | |
54 | { | |
55 | return wxFile::Exists(m_strBufferName); | |
56 | } | |
57 | ||
58 | ||
59 | bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode OpenMode) | |
60 | { | |
61 | wxFile::OpenMode FileOpenMode; | |
62 | ||
63 | switch ( OpenMode ) | |
64 | { | |
65 | default: | |
66 | wxFAIL_MSG( wxT("unknown open mode in wxTextFile::Open") ); | |
67 | // fall through | |
68 | ||
69 | case ReadAccess : | |
70 | FileOpenMode = wxFile::read; | |
71 | break; | |
72 | ||
73 | case WriteAccess : | |
74 | FileOpenMode = wxFile::write; | |
75 | break; | |
76 | } | |
77 | ||
78 | return m_file.Open(strBufferName.c_str(), FileOpenMode); | |
79 | } | |
80 | ||
81 | ||
82 | bool wxTextFile::OnClose() | |
83 | { | |
84 | return m_file.Close(); | |
85 | } | |
86 | ||
87 | ||
88 | bool wxTextFile::OnRead(const wxMBConv& conv) | |
89 | { | |
90 | // file should be opened | |
91 | wxASSERT_MSG( m_file.IsOpened(), wxT("can't read closed file") ); | |
92 | ||
93 | // read the entire file in memory: this is not the most efficient thing to | |
94 | // do it but there is no good way to avoid it in Unicode build because if | |
95 | // we read the file block by block we can't convert each block to Unicode | |
96 | // separately (the last multibyte char in the block might be only partially | |
97 | // read and so the conversion would fail) and, as the file contents is kept | |
98 | // in memory by wxTextFile anyhow, it shouldn't be a big problem to read | |
99 | // the file entirely | |
100 | size_t bufSize = 0; | |
101 | ||
102 | // number of bytes to (try to) read from disk at once | |
103 | static const size_t BLOCK_SIZE = 4096; | |
104 | ||
105 | wxCharBuffer buf; | |
106 | ||
107 | // first determine if the file is seekable or not and so whether we can | |
108 | // determine its length in advance | |
109 | wxFileOffset fileLength; | |
110 | { | |
111 | wxLogNull logNull; | |
112 | fileLength = m_file.Length(); | |
113 | } | |
114 | ||
115 | // some non-seekable files under /proc under Linux pretend that they're | |
116 | // seekable but always return 0; others do return an error | |
117 | const bool seekable = fileLength != wxInvalidOffset && fileLength != 0; | |
118 | if ( seekable ) | |
119 | { | |
120 | // we know the required length, so set the buffer size in advance | |
121 | bufSize = fileLength; | |
122 | if ( !buf.extend(bufSize) ) | |
123 | return false; | |
124 | ||
125 | // if the file is seekable, also check that we're at its beginning | |
126 | wxASSERT_MSG( m_file.Tell() == 0, wxT("should be at start of file") ); | |
127 | ||
128 | char *dst = buf.data(); | |
129 | for ( size_t nRemaining = bufSize; nRemaining > 0; ) | |
130 | { | |
131 | size_t nToRead = BLOCK_SIZE; | |
132 | ||
133 | // the file size could have changed, avoid overflowing the buffer | |
134 | // even if it did | |
135 | if ( nToRead > nRemaining ) | |
136 | nToRead = nRemaining; | |
137 | ||
138 | ssize_t nRead = m_file.Read(dst, nToRead); | |
139 | ||
140 | if ( nRead == wxInvalidOffset ) | |
141 | { | |
142 | // read error (error message already given in wxFile::Read) | |
143 | return false; | |
144 | } | |
145 | ||
146 | if ( nRead == 0 ) | |
147 | { | |
148 | // this file can't be empty because we checked for this above | |
149 | // so this must be the end of file | |
150 | break; | |
151 | } | |
152 | ||
153 | dst += nRead; | |
154 | nRemaining -= nRead; | |
155 | } | |
156 | ||
157 | wxASSERT_MSG( dst - buf.data() == (wxFileOffset)bufSize, | |
158 | wxT("logic error") ); | |
159 | } | |
160 | else // file is not seekable | |
161 | { | |
162 | char block[BLOCK_SIZE]; | |
163 | for ( ;; ) | |
164 | { | |
165 | ssize_t nRead = m_file.Read(block, WXSIZEOF(block)); | |
166 | ||
167 | if ( nRead == wxInvalidOffset ) | |
168 | { | |
169 | // read error (error message already given in wxFile::Read) | |
170 | return false; | |
171 | } | |
172 | ||
173 | if ( nRead == 0 ) | |
174 | { | |
175 | // if no bytes have been read, presumably this is a | |
176 | // valid-but-empty file | |
177 | if ( bufSize == 0 ) | |
178 | return true; | |
179 | ||
180 | // otherwise we've finished reading the file | |
181 | break; | |
182 | } | |
183 | ||
184 | // extend the buffer for new data | |
185 | if ( !buf.extend(bufSize + nRead) ) | |
186 | return false; | |
187 | ||
188 | // and append it to the buffer | |
189 | memcpy(buf.data() + bufSize, block, nRead); | |
190 | bufSize += nRead; | |
191 | } | |
192 | } | |
193 | ||
194 | const wxString str(buf, conv, bufSize); | |
195 | ||
196 | // there's no risk of this happening in ANSI build | |
197 | #if wxUSE_UNICODE | |
198 | if ( bufSize > 4 && str.empty() ) | |
199 | { | |
200 | wxLogError(_("Failed to convert file \"%s\" to Unicode."), GetName()); | |
201 | return false; | |
202 | } | |
203 | #endif // wxUSE_UNICODE | |
204 | ||
205 | // we don't need this memory any more | |
206 | buf.reset(); | |
207 | ||
208 | ||
209 | // now break the buffer in lines | |
210 | ||
211 | // last processed character, we need to know if it was a CR or not | |
212 | wxChar chLast = '\0'; | |
213 | ||
214 | // the beginning of the current line, changes inside the loop | |
215 | wxString::const_iterator lineStart = str.begin(); | |
216 | const wxString::const_iterator end = str.end(); | |
217 | for ( wxString::const_iterator p = lineStart; p != end; p++ ) | |
218 | { | |
219 | const wxChar ch = *p; | |
220 | switch ( ch ) | |
221 | { | |
222 | case '\n': | |
223 | // could be a DOS or Unix EOL | |
224 | if ( chLast == '\r' ) | |
225 | { | |
226 | if ( p - 1 >= lineStart ) | |
227 | { | |
228 | AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos); | |
229 | } | |
230 | else | |
231 | { | |
232 | // there were two line endings, so add an empty line: | |
233 | AddLine(wxEmptyString, wxTextFileType_Dos); | |
234 | } | |
235 | } | |
236 | else // bare '\n', Unix style | |
237 | { | |
238 | AddLine(wxString(lineStart, p), wxTextFileType_Unix); | |
239 | } | |
240 | ||
241 | lineStart = p + 1; | |
242 | break; | |
243 | ||
244 | case '\r': | |
245 | if ( chLast == '\r' ) | |
246 | { | |
247 | // Mac empty line | |
248 | AddLine(wxEmptyString, wxTextFileType_Mac); | |
249 | lineStart = p + 1; | |
250 | } | |
251 | //else: we don't know what this is yet -- could be a Mac EOL or | |
252 | // start of DOS EOL so wait for next char | |
253 | break; | |
254 | ||
255 | default: | |
256 | if ( chLast == '\r' ) | |
257 | { | |
258 | // Mac line termination | |
259 | if ( p - 1 >= lineStart ) | |
260 | { | |
261 | AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac); | |
262 | } | |
263 | else | |
264 | { | |
265 | // there were two line endings, so add an empty line: | |
266 | AddLine(wxEmptyString, wxTextFileType_Mac); | |
267 | } | |
268 | lineStart = p; | |
269 | } | |
270 | } | |
271 | ||
272 | chLast = ch; | |
273 | } | |
274 | ||
275 | // anything in the last line? | |
276 | if ( lineStart != end ) | |
277 | { | |
278 | // add unterminated last line | |
279 | AddLine(wxString(lineStart, end), wxTextFileType_None); | |
280 | } | |
281 | ||
282 | return true; | |
283 | } | |
284 | ||
285 | ||
286 | bool wxTextFile::OnWrite(wxTextFileType typeNew, const wxMBConv& conv) | |
287 | { | |
288 | wxFileName fn = m_strBufferName; | |
289 | ||
290 | // We do NOT want wxPATH_NORM_CASE here, or the case will not | |
291 | // be preserved. | |
292 | if ( !fn.IsAbsolute() ) | |
293 | fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | | |
294 | wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG); | |
295 | ||
296 | wxTempFile fileTmp(fn.GetFullPath()); | |
297 | ||
298 | if ( !fileTmp.IsOpened() ) { | |
299 | wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str()); | |
300 | return false; | |
301 | } | |
302 | ||
303 | size_t nCount = GetLineCount(); | |
304 | for ( size_t n = 0; n < nCount; n++ ) { | |
305 | fileTmp.Write(GetLine(n) + | |
306 | GetEOL(typeNew == wxTextFileType_None ? GetLineType(n) | |
307 | : typeNew), | |
308 | conv); | |
309 | } | |
310 | ||
311 | // replace the old file with this one | |
312 | return fileTmp.Commit(); | |
313 | } | |
314 | ||
315 | #endif // wxUSE_TEXTFILE |