]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
f9cd34077d22b650c94051aa1933a030f51822b1
[wxWidgets.git] / src / common / textfile.cpp
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 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers
14 // ============================================================================
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif //__BORLANDC__
21
22 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
23 #undef wxUSE_TEXTFILE
24 #define wxUSE_TEXTFILE 0
25 #endif // wxUSE_FILE
26
27 #if wxUSE_TEXTFILE
28
29 #ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #include "wx/intl.h"
32 #include "wx/file.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/textfile.h"
37 #include "wx/filename.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( _T("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(wxMBConv& conv)
89 {
90 // file should be opened and we must be in it's beginning
91 wxASSERT( m_file.IsOpened() &&
92 (m_file.GetKind() != wxFILE_KIND_DISK || m_file.Tell() == 0) );
93
94 static const size_t BUF_SIZE = 1024;
95 #if wxUSE_UNICODE
96 static const size_t NUL_SIZE = 4;
97 #else
98 static const size_t NUL_SIZE = 1;
99 #endif
100
101 char buf[BUF_SIZE + NUL_SIZE];
102 wxChar chLast = '\0';
103 wxString str;
104
105 for ( ;; )
106 {
107 // leave space for trailing NUL
108 ssize_t nRead = m_file.Read(buf, BUF_SIZE);
109
110 if ( nRead == wxInvalidOffset )
111 {
112 // read error (error message already given in wxFile::Read)
113 return false;
114 }
115
116 if ( nRead == 0 )
117 break;
118
119 // save the number characters which we already processed during the
120 // last loop iteration
121 const size_t lenOld = str.length();
122
123 #if wxUSE_UNICODE
124 // we have to properly NUL-terminate the string for any encoding it may
125 // use -- 4 NULs should be enough for everyone (this is why we add 4
126 // extra bytes to the buffer)
127 buf[nRead] =
128 buf[nRead + 1] =
129 buf[nRead + 2] =
130 buf[nRead + 3] = '\0';
131
132 // append to the remains of the last block, don't overwrite
133 wxString strbuf(buf, conv);
134 if ( strbuf.empty() )
135 {
136 // conversion failed
137 return false;
138 }
139
140 str += strbuf;
141 #else // ANSI
142 wxUnusedVar(conv);
143 buf[nRead] = '\0';
144 str += buf;
145 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
146
147
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 + lenOld; 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 {
160 AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos);
161 }
162 else // bare '\n', Unix style
163 {
164 AddLine(wxString(lineStart, p), wxTextFileType_Unix);
165 }
166
167 lineStart = p + 1;
168 break;
169
170 case '\r':
171 if ( chLast == '\r' )
172 {
173 // Mac empty line
174 AddLine(wxEmptyString, wxTextFileType_Mac);
175 lineStart = p + 1;
176 }
177 //else: we don't what this is yet -- could be a Mac EOL or
178 // start of DOS EOL so wait for next char
179 break;
180
181 default:
182 if ( chLast == '\r' )
183 {
184 // Mac line termination
185 AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac);
186 lineStart = p;
187 }
188 }
189
190 chLast = ch;
191 }
192
193 // remove the part we already processed
194 str.erase(0, lineStart - str.begin());
195 }
196
197 // anything in the last line?
198 if ( !str.empty() )
199 {
200 AddLine(str, wxTextFileType_None); // no line terminator
201 }
202
203 return true;
204 }
205
206
207 bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
208 {
209 wxFileName fn = m_strBufferName;
210
211 // We do NOT want wxPATH_NORM_CASE here, or the case will not
212 // be preserved.
213 if ( !fn.IsAbsolute() )
214 fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE |
215 wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG);
216
217 wxTempFile fileTmp(fn.GetFullPath());
218
219 if ( !fileTmp.IsOpened() ) {
220 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
221 return false;
222 }
223
224 size_t nCount = GetLineCount();
225 for ( size_t n = 0; n < nCount; n++ ) {
226 fileTmp.Write(GetLine(n) +
227 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
228 : typeNew),
229 conv);
230 }
231
232 // replace the old file with this one
233 return fileTmp.Commit();
234 }
235
236 #endif // wxUSE_TEXTFILE
237