]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
Unicode compilation fixes
[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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "textfile.h"
18 #endif
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif //__BORLANDC__
25
26 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
27 #undef wxUSE_TEXTFILE
28 #define wxUSE_TEXTFILE 0
29 #endif // wxUSE_FILE
30
31 #if wxUSE_TEXTFILE
32
33 #ifndef WX_PRECOMP
34 #include "wx/string.h"
35 #include "wx/intl.h"
36 #include "wx/file.h"
37 #include "wx/log.h"
38 #endif
39
40 #include "wx/textfile.h"
41
42 // ============================================================================
43 // wxTextFile class implementation
44 // ============================================================================
45
46 wxTextFile::wxTextFile(const wxString& strFileName)
47 : wxTextBuffer(strFileName)
48 {
49 }
50
51
52 // ----------------------------------------------------------------------------
53 // file operations
54 // ----------------------------------------------------------------------------
55
56 bool wxTextFile::OnExists() const
57 {
58 return wxFile::Exists(m_strBufferName);
59 }
60
61
62 bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode OpenMode)
63 {
64 wxFile::OpenMode FileOpenMode = wxFile::read;
65
66 switch (OpenMode)
67 {
68 case ReadAccess :
69 FileOpenMode = wxFile::read;
70 break;
71 case WriteAccess :
72 FileOpenMode = wxFile::write;
73 break;
74 default :
75 wxASSERT(0); // Should not happen.
76 break;
77 }
78
79 return m_file.Open(strBufferName.c_str(), FileOpenMode);
80 }
81
82
83 bool wxTextFile::OnClose()
84 {
85 return m_file.Close();
86 }
87
88
89 bool 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 #if wxUSE_UNICODE
95 char conv_mbBuf[2];
96 wchar_t conv_wcBuf[2];
97 conv_mbBuf[1] = 0;
98 #else
99 (void)conv;
100 #endif
101
102 wxString str;
103 char ch, chLast = '\0';
104 char buf[1024];
105 int n, nRead;
106 do {
107 nRead = m_file.Read(buf, WXSIZEOF(buf));
108 if ( nRead == wxInvalidOffset ) {
109 // read error (error message already given in wxFile::Read)
110 return FALSE;
111 }
112
113 for ( n = 0; n < nRead; n++ ) {
114 ch = buf[n];
115 switch ( ch ) {
116 case '\n':
117 // Dos/Unix line termination
118 AddLine(str, chLast == '\r' ? wxTextFileType_Dos
119 : wxTextFileType_Unix);
120 str.Empty();
121 chLast = '\n';
122 break;
123
124 case '\r':
125 if ( chLast == '\r' ) {
126 // Mac empty line
127 AddLine(wxEmptyString, wxTextFileType_Mac);
128 }
129 else
130 chLast = '\r';
131 break;
132
133 default:
134 if ( chLast == '\r' ) {
135 // Mac line termination
136 AddLine(str, wxTextFileType_Mac);
137 chLast = ch;
138 #if wxUSE_UNICODE
139 conv_mbBuf[0] = ch;
140 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
141 conv_wcBuf[0] = ch;
142 str = conv_wcBuf[0];
143 #else
144 str = ch;
145 #endif
146 }
147 else {
148 // add to the current line
149 #if wxUSE_UNICODE
150 conv_mbBuf[0] = ch;
151 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
152 conv_wcBuf[0] = ch;
153 str += conv_wcBuf[0];
154 #else
155 str += ch;
156 #endif
157 }
158 }
159 }
160 } while ( nRead == WXSIZEOF(buf) );
161
162 // anything in the last line?
163 if ( !str.IsEmpty() ) {
164 AddLine(str, wxTextFileType_None); // no line terminator
165 }
166
167 return TRUE;
168 }
169
170
171 bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
172 {
173 wxTempFile fileTmp(m_strBufferName);
174
175 if ( !fileTmp.IsOpened() ) {
176 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
177 return FALSE;
178 }
179
180 size_t nCount = GetLineCount();
181 for ( size_t n = 0; n < nCount; n++ ) {
182 fileTmp.Write(GetLine(n) +
183 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
184 : typeNew),
185 conv);
186 }
187
188 // replace the old file with this one
189 return fileTmp.Commit();
190 }
191
192 #endif // wxUSE_TEXTFILE
193