]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/textfile.cpp
Unicode compilation fixes
[wxWidgets.git] / src / common / textfile.cpp
... / ...
CommitLineData
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#include "wx/buffer.h"
39
40// ============================================================================
41// wxTextFile class implementation
42// ============================================================================
43
44wxTextFile::wxTextFile(const wxString& strFileName)
45 : wxTextBuffer(strFileName)
46{
47}
48
49
50// ----------------------------------------------------------------------------
51// file operations
52// ----------------------------------------------------------------------------
53
54bool wxTextFile::OnExists() const
55{
56 return wxFile::Exists(m_strBufferName);
57}
58
59
60bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode OpenMode)
61{
62 wxFile::OpenMode FileOpenMode;
63
64 switch ( OpenMode )
65 {
66 default:
67 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
68 // fall through
69
70 case ReadAccess :
71 FileOpenMode = wxFile::read;
72 break;
73
74 case WriteAccess :
75 FileOpenMode = wxFile::write;
76 break;
77 }
78
79 return m_file.Open(strBufferName.c_str(), FileOpenMode);
80}
81
82
83bool wxTextFile::OnClose()
84{
85 return m_file.Close();
86}
87
88
89bool wxTextFile::OnRead(wxMBConv& conv)
90{
91 // file should be opened and we must be in it's beginning
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
101 const size_t bufSize = m_file.Length() + 4 /* for trailing NULs */;
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; )
107 {
108 // try to read up to the size of the entire block
109 ssize_t nRead = m_file.Read(block, WXSIZEOF(block));
110
111 if ( nRead == wxInvalidOffset )
112 {
113 // read error (error message already given in wxFile::Read)
114 return false;
115 }
116
117 eof = nRead == 0;
118 if ( eof )
119 {
120 // append 4 trailing NUL bytes: this is needed to ensure that the
121 // string is going to be NUL-terminated, whatever is the encoding
122 // used (even UTF-32)
123 block[0] =
124 block[1] =
125 block[2] =
126 block[3] = '\0';
127 nRead = 4;
128 }
129
130 // this shouldn't happen but don't overwrite the buffer if it does
131 wxCHECK_MSG( bufPos + nRead <= bufSize, false,
132 _T("read more than file length?") );
133
134 // append to the buffer
135 memcpy(buf.data() + bufPos, block, nRead);
136 bufPos += nRead;
137 }
138
139 const wxString str(buf, conv);
140#if wxUSE_UNICODE
141 if ( str.empty() )
142 {
143 wxLogError(_("Failed to convert file contents to Unicode."));
144 return false;
145 }
146#endif // wxUSE_UNICODE
147
148 free(buf.release()); // we don't need this memory any more
149
150
151 // now break the buffer in lines
152
153 // last processed character, we need to know if it was a CR or not
154 wxChar chLast = '\0';
155
156 // the beginning of the current line, changes inside the loop
157 wxString::const_iterator lineStart = str.begin();
158 const wxString::const_iterator end = str.end();
159 for ( wxString::const_iterator p = lineStart; p != end; p++ )
160 {
161 const wxChar ch = *p;
162 switch ( ch )
163 {
164 case '\n':
165 // could be a DOS or Unix EOL
166 if ( chLast == '\r' )
167 {
168 AddLine(wxString(lineStart, p - 1), wxTextFileType_Dos);
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);
183 lineStart = p + 1;
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
193 AddLine(wxString(lineStart, p - 1), wxTextFileType_Mac);
194 lineStart = p;
195 }
196 }
197
198 chLast = ch;
199 }
200
201 // anything in the last line?
202 if ( lineStart != end )
203 {
204 // add unterminated last line
205 AddLine(wxString(lineStart, end), wxTextFileType_None);
206 }
207
208 return true;
209}
210
211
212bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
213{
214 wxFileName fn = m_strBufferName;
215
216 // We do NOT want wxPATH_NORM_CASE here, or the case will not
217 // be preserved.
218 if ( !fn.IsAbsolute() )
219 fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE |
220 wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG);
221
222 wxTempFile fileTmp(fn.GetFullPath());
223
224 if ( !fileTmp.IsOpened() ) {
225 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
226 return false;
227 }
228
229 size_t nCount = GetLineCount();
230 for ( size_t n = 0; n < nCount; n++ ) {
231 fileTmp.Write(GetLine(n) +
232 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
233 : typeNew),
234 conv);
235 }
236
237 // replace the old file with this one
238 return fileTmp.Commit();
239}
240
241#endif // wxUSE_TEXTFILE
242