]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | /////////////////////////////////////////////////////////////////////////////// |
a3a584a7 | 2 | // Name: src/common/textfile.cpp |
c801d85f KB |
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> | |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // headers | |
14 | // ============================================================================ | |
15 | ||
c801d85f KB |
16 | #include "wx/wxprec.h" |
17 | ||
18 | #ifdef __BORLANDC__ | |
a1b82138 | 19 | #pragma hdrstop |
c801d85f KB |
20 | #endif //__BORLANDC__ |
21 | ||
a3a584a7 | 22 | #if !wxUSE_FILE || !wxUSE_TEXTBUFFER |
a1b82138 VZ |
23 | #undef wxUSE_TEXTFILE |
24 | #define wxUSE_TEXTFILE 0 | |
25 | #endif // wxUSE_FILE | |
26 | ||
a3a584a7 VZ |
27 | #if wxUSE_TEXTFILE |
28 | ||
ce4169a4 | 29 | #ifndef WX_PRECOMP |
68c97af3 VZ |
30 | #include "wx/string.h" |
31 | #include "wx/intl.h" | |
32 | #include "wx/file.h" | |
33 | #include "wx/log.h" | |
ce4169a4 RR |
34 | #endif |
35 | ||
a3a584a7 | 36 | #include "wx/textfile.h" |
68c97af3 | 37 | #include "wx/filename.h" |
dbcf443c | 38 | #include "wx/buffer.h" |
c801d85f KB |
39 | |
40 | // ============================================================================ | |
41 | // wxTextFile class implementation | |
42 | // ============================================================================ | |
43 | ||
a3a584a7 VZ |
44 | wxTextFile::wxTextFile(const wxString& strFileName) |
45 | : wxTextBuffer(strFileName) | |
a1b82138 | 46 | { |
a1b82138 VZ |
47 | } |
48 | ||
c801d85f KB |
49 | |
50 | // ---------------------------------------------------------------------------- | |
51 | // file operations | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
a3a584a7 | 54 | bool wxTextFile::OnExists() const |
ef8d96c2 | 55 | { |
a3a584a7 | 56 | return wxFile::Exists(m_strBufferName); |
ef8d96c2 VZ |
57 | } |
58 | ||
1b6dea5d | 59 | |
a3a584a7 | 60 | bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode OpenMode) |
1b6dea5d | 61 | { |
77f859c3 VZ |
62 | wxFile::OpenMode FileOpenMode; |
63 | ||
64 | switch ( OpenMode ) | |
65 | { | |
66 | default: | |
67 | wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") ); | |
68 | // fall through | |
27752aab | 69 | |
a3a584a7 VZ |
70 | case ReadAccess : |
71 | FileOpenMode = wxFile::read; | |
72 | break; | |
77f859c3 | 73 | |
a3a584a7 VZ |
74 | case WriteAccess : |
75 | FileOpenMode = wxFile::write; | |
76 | break; | |
77f859c3 | 77 | } |
1b6dea5d | 78 | |
a3a584a7 | 79 | return m_file.Open(strBufferName.c_str(), FileOpenMode); |
c801d85f KB |
80 | } |
81 | ||
c801d85f | 82 | |
a3a584a7 | 83 | bool wxTextFile::OnClose() |
c801d85f | 84 | { |
a3a584a7 | 85 | return m_file.Close(); |
c801d85f KB |
86 | } |
87 | ||
a3a584a7 | 88 | |
830f8f11 | 89 | bool wxTextFile::OnRead(const wxMBConv& conv) |
c801d85f | 90 | { |
d9ade1df | 91 | // file should be opened and we must be in it's beginning |
dbcf443c VZ |
92 | wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); |
93 | ||
94 | // read the entire file in memory: this is not the most efficient thing to | |
95 | // do but there is no good way to avoid it in Unicode build because if we | |
96 | // read the file block by block we can't convert each block to Unicode | |
97 | // separately (the last multibyte char in the block might be only partially | |
98 | // read and so the conversion would fail) and, as the file contents is kept | |
99 | // in memory by wxTextFile anyhow, it shouldn't be a big problem to read | |
100 | // the file entirely | |
47eb93d0 | 101 | const size_t bufSize = (size_t)(m_file.Length() + 4 /* for trailing NULs */ ); |
dbcf443c VZ |
102 | size_t bufPos = 0; |
103 | wxCharBuffer buf(bufSize - 1 /* it adds 1 internally */); | |
104 | ||
105 | char block[1024]; | |
106 | for ( bool eof = false; !eof; ) | |
d9ade1df | 107 | { |
dbcf443c VZ |
108 | // try to read up to the size of the entire block |
109 | ssize_t nRead = m_file.Read(block, WXSIZEOF(block)); | |
86948c99 VZ |
110 | |
111 | if ( nRead == wxInvalidOffset ) | |
d9ade1df VS |
112 | { |
113 | // read error (error message already given in wxFile::Read) | |
cb719f2e | 114 | return false; |
d9ade1df VS |
115 | } |
116 | ||
830f8f11 VZ |
117 | if ( nRead == 0 ) |
118 | break; | |
86948c99 | 119 | |
dbcf443c VZ |
120 | // this shouldn't happen but don't overwrite the buffer if it does |
121 | wxCHECK_MSG( bufPos + nRead <= bufSize, false, | |
122 | _T("read more than file length?") ); | |
123 | ||
124 | // append to the buffer | |
125 | memcpy(buf.data() + bufPos, block, nRead); | |
126 | bufPos += nRead; | |
127 | } | |
c1981a2f | 128 | |
830f8f11 | 129 | const wxString str(buf, conv, bufPos); |
44327ff3 VZ |
130 | |
131 | // this doesn't risk to happen in ANSI build | |
b260e323 | 132 | #if wxUSE_UNICODE |
44327ff3 | 133 | if ( bufSize > 4 && str.empty() ) |
dbcf443c VZ |
134 | { |
135 | wxLogError(_("Failed to convert file contents to Unicode.")); | |
136 | return false; | |
137 | } | |
138 | #endif // wxUSE_UNICODE | |
b260e323 | 139 | |
dbcf443c | 140 | free(buf.release()); // we don't need this memory any more |
b260e323 | 141 | |
86948c99 | 142 | |
dbcf443c VZ |
143 | // now break the buffer in lines |
144 | ||
145 | // last processed character, we need to know if it was a CR or not | |
146 | wxChar chLast = '\0'; | |
86948c99 | 147 | |
dbcf443c VZ |
148 | // the beginning of the current line, changes inside the loop |
149 | wxString::const_iterator lineStart = str.begin(); | |
150 | const wxString::const_iterator end = str.end(); | |
151 | for ( wxString::const_iterator p = lineStart; p != end; p++ ) | |
152 | { | |
153 | const wxChar ch = *p; | |
154 | switch ( ch ) | |
155 | { | |
156 | case '\n': | |
157 | // could be a DOS or Unix EOL | |
158 | if ( chLast == '\r' ) | |
159 | { | |
82bf96f5 VS |
160 | if ( p - 1 >= lineStart ) |
161 | { | |
162 | AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos); | |
163 | } | |
164 | else | |
165 | { | |
166 | // there were two line endings, so add an empty line: | |
167 | AddLine(wxEmptyString, wxTextFileType_Dos); | |
168 | } | |
dbcf443c VZ |
169 | } |
170 | else // bare '\n', Unix style | |
171 | { | |
172 | AddLine(wxString(lineStart, p), wxTextFileType_Unix); | |
173 | } | |
174 | ||
175 | lineStart = p + 1; | |
176 | break; | |
177 | ||
178 | case '\r': | |
179 | if ( chLast == '\r' ) | |
180 | { | |
181 | // Mac empty line | |
182 | AddLine(wxEmptyString, wxTextFileType_Mac); | |
86948c99 | 183 | lineStart = p + 1; |
dbcf443c VZ |
184 | } |
185 | //else: we don't know what this is yet -- could be a Mac EOL or | |
186 | // start of DOS EOL so wait for next char | |
187 | break; | |
188 | ||
189 | default: | |
190 | if ( chLast == '\r' ) | |
191 | { | |
192 | // Mac line termination | |
82bf96f5 VS |
193 | if ( p - 1 >= lineStart ) |
194 | { | |
195 | AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac); | |
196 | } | |
197 | else | |
198 | { | |
199 | // there were two line endings, so add an empty line: | |
200 | AddLine(wxEmptyString, wxTextFileType_Mac); | |
201 | } | |
dbcf443c VZ |
202 | lineStart = p; |
203 | } | |
d9ade1df | 204 | } |
86948c99 | 205 | |
dbcf443c | 206 | chLast = ch; |
86948c99 | 207 | } |
d9ade1df VS |
208 | |
209 | // anything in the last line? | |
dbcf443c | 210 | if ( lineStart != end ) |
d9ade1df | 211 | { |
dbcf443c VZ |
212 | // add unterminated last line |
213 | AddLine(wxString(lineStart, end), wxTextFileType_None); | |
c801d85f | 214 | } |
c801d85f | 215 | |
cb719f2e | 216 | return true; |
c801d85f KB |
217 | } |
218 | ||
f42d2aba | 219 | |
830f8f11 | 220 | bool wxTextFile::OnWrite(wxTextFileType typeNew, const wxMBConv& conv) |
c801d85f | 221 | { |
68c97af3 | 222 | wxFileName fn = m_strBufferName; |
baed1077 JS |
223 | |
224 | // We do NOT want wxPATH_NORM_CASE here, or the case will not | |
225 | // be preserved. | |
68c97af3 | 226 | if ( !fn.IsAbsolute() ) |
32a0d013 VS |
227 | fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | |
228 | wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG); | |
68c97af3 | 229 | |
deab4540 | 230 | wxTempFile fileTmp(fn.GetFullPath()); |
c801d85f | 231 | |
a3a584a7 VZ |
232 | if ( !fileTmp.IsOpened() ) { |
233 | wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str()); | |
cb719f2e | 234 | return false; |
a3a584a7 | 235 | } |
c801d85f | 236 | |
a3a584a7 VZ |
237 | size_t nCount = GetLineCount(); |
238 | for ( size_t n = 0; n < nCount; n++ ) { | |
239 | fileTmp.Write(GetLine(n) + | |
240 | GetEOL(typeNew == wxTextFileType_None ? GetLineType(n) | |
241 | : typeNew), | |
242 | conv); | |
243 | } | |
c801d85f | 244 | |
a3a584a7 VZ |
245 | // replace the old file with this one |
246 | return fileTmp.Commit(); | |
ba7f9a90 | 247 | } |
6164d85e | 248 | |
a1b82138 | 249 | #endif // wxUSE_TEXTFILE |