]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
compilation fix for Watcom
[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;
65
66 switch ( OpenMode )
67 {
68 default:
69 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
70 // fall through
71
72 case ReadAccess :
73 FileOpenMode = wxFile::read;
74 break;
75
76 case WriteAccess :
77 FileOpenMode = wxFile::write;
78 break;
79 }
80
81 return m_file.Open(strBufferName.c_str(), FileOpenMode);
82 }
83
84
85 bool wxTextFile::OnClose()
86 {
87 return m_file.Close();
88 }
89
90
91 bool wxTextFile::OnRead(wxMBConv& conv)
92 {
93 // file should be opened and we must be in it's beginning
94 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
95
96 #if wxUSE_UNICODE
97 char conv_mbBuf[2];
98 wchar_t conv_wcBuf[2];
99 conv_mbBuf[1] = 0;
100 #else
101 (void)conv;
102 #endif
103
104 wxString str;
105 char ch, chLast = '\0';
106 char buf[1024];
107 int n, nRead;
108 do {
109 nRead = m_file.Read(buf, WXSIZEOF(buf));
110 if ( nRead == wxInvalidOffset ) {
111 // read error (error message already given in wxFile::Read)
112 return FALSE;
113 }
114
115 for ( n = 0; n < nRead; n++ ) {
116 ch = buf[n];
117 switch ( ch ) {
118 case '\n':
119 // Dos/Unix line termination
120 AddLine(str, chLast == '\r' ? wxTextFileType_Dos
121 : wxTextFileType_Unix);
122 str.Empty();
123 chLast = '\n';
124 break;
125
126 case '\r':
127 if ( chLast == '\r' ) {
128 // Mac empty line
129 AddLine(wxEmptyString, wxTextFileType_Mac);
130 }
131 else
132 chLast = '\r';
133 break;
134
135 default:
136 if ( chLast == '\r' ) {
137 // Mac line termination
138 AddLine(str, wxTextFileType_Mac);
139 chLast = ch;
140 #if wxUSE_UNICODE
141 conv_mbBuf[0] = ch;
142 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
143 conv_wcBuf[0] = ch;
144 str = conv_wcBuf[0];
145 #else
146 str = ch;
147 #endif
148 }
149 else {
150 // add to the current line
151 #if wxUSE_UNICODE
152 conv_mbBuf[0] = ch;
153 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
154 conv_wcBuf[0] = ch;
155 str += conv_wcBuf[0];
156 #else
157 str += ch;
158 #endif
159 }
160 }
161 }
162 } while ( nRead == WXSIZEOF(buf) );
163
164 // anything in the last line?
165 if ( !str.IsEmpty() ) {
166 AddLine(str, wxTextFileType_None); // no line terminator
167 }
168
169 return TRUE;
170 }
171
172
173 bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
174 {
175 wxTempFile fileTmp(m_strBufferName);
176
177 if ( !fileTmp.IsOpened() ) {
178 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
179 return FALSE;
180 }
181
182 size_t nCount = GetLineCount();
183 for ( size_t n = 0; n < nCount; n++ ) {
184 fileTmp.Write(GetLine(n) +
185 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
186 : typeNew),
187 conv);
188 }
189
190 // replace the old file with this one
191 return fileTmp.Commit();
192 }
193
194 #endif // wxUSE_TEXTFILE
195