]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
b7ab9e06cad112c7a512126150d9f5d22126246e
[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 char buf[1025];
95 wxChar chLast = '\0';
96 wxString str;
97
98 for ( ;; )
99 {
100 // leave space for trailing NUL
101 ssize_t nRead = m_file.Read(buf, WXSIZEOF(buf) - 1);
102
103 if ( nRead == wxInvalidOffset )
104 {
105 // read error (error message already given in wxFile::Read)
106 return false;
107 }
108
109 if ( nRead == 0 )
110 break;
111
112 buf[nRead] = '\0';
113
114 // append to the remains of the last block, don't overwrite
115 str += wxString(buf, conv);
116
117 // the beginning of the current line, changes inside the loop
118 const wxChar *lineStart = str.begin();
119 const wxChar * const end = str.end();
120 for ( const wxChar *p = lineStart; p != end; p++ )
121 {
122 const wxChar ch = *p;
123 switch ( ch )
124 {
125 case '\n':
126 // could be a DOS or Unix EOL
127 if ( chLast == '\r' )
128 {
129 AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos);
130 }
131 else // bare '\n', Unix style
132 {
133 AddLine(wxString(lineStart, p), wxTextFileType_Unix);
134 }
135
136 lineStart = p + 1;
137 break;
138
139 case '\r':
140 if ( chLast == '\r' )
141 {
142 // Mac empty line
143 AddLine(wxEmptyString, wxTextFileType_Mac);
144 lineStart = p + 1;
145 }
146 //else: we don't what this is yet -- could be a Mac EOL or
147 // start of DOS EOL so wait for next char
148 break;
149
150 default:
151 if ( chLast == '\r' )
152 {
153 // Mac line termination
154 AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac);
155 lineStart = p;
156 }
157 }
158
159 chLast = ch;
160 }
161
162 // remove the part we already processed
163 str.erase(0, lineStart - str.begin());
164 }
165
166 // anything in the last line?
167 if ( !str.empty() )
168 {
169 AddLine(str, wxTextFileType_None); // no line terminator
170 }
171
172 return true;
173 }
174
175
176 bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
177 {
178 wxFileName fn = m_strBufferName;
179
180 // We do NOT want wxPATH_NORM_CASE here, or the case will not
181 // be preserved.
182 if ( !fn.IsAbsolute() )
183 fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE |
184 wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG);
185
186 wxTempFile fileTmp(fn.GetFullPath());
187
188 if ( !fileTmp.IsOpened() ) {
189 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
190 return false;
191 }
192
193 size_t nCount = GetLineCount();
194 for ( size_t n = 0; n < nCount; n++ ) {
195 fileTmp.Write(GetLine(n) +
196 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
197 : typeNew),
198 conv);
199 }
200
201 // replace the old file with this one
202 return fileTmp.Commit();
203 }
204
205 #endif // wxUSE_TEXTFILE
206