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