]> git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
change to /usr/share/wx/version
[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 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
140 conv_wcBuf[0] = ch;
141 str = conv_wcBuf[0];
142 #else
143 str = ch;
144 #endif
145 }
146 else {
147 // add to the current line
148 #if wxUSE_UNICODE
149 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
150 conv_wcBuf[0] = ch;
151 str += conv_wcBuf[0];
152 #else
153 str += ch;
154 #endif
155 }
156 }
157 }
158 } while ( nRead == WXSIZEOF(buf) );
159
160 // anything in the last line?
161 if ( !str.IsEmpty() ) {
162 AddLine(str, wxTextFileType_None); // no line terminator
163 }
164
165 return TRUE;
166 }
167
168
169 bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
170 {
171 wxTempFile fileTmp(m_strBufferName);
172
173 if ( !fileTmp.IsOpened() ) {
174 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
175 return FALSE;
176 }
177
178 size_t nCount = GetLineCount();
179 for ( size_t n = 0; n < nCount; n++ ) {
180 fileTmp.Write(GetLine(n) +
181 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
182 : typeNew),
183 conv);
184 }
185
186 // replace the old file with this one
187 return fileTmp.Commit();
188 }
189
190 #endif // wxUSE_TEXTFILE
191