]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | /////////////////////////////////////////////////////////////////////////////// |
a3a584a7 | 2 | // Name: src/common/textfile.cpp |
c801d85f KB |
3 | // Purpose: implementation of wxTextFile class |
4 | // Author: Vadim Zeitlin | |
ba7f9a90 | 5 | // Modified by: |
c801d85f KB |
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__ | |
a1b82138 | 17 | #pragma implementation "textfile.h" |
c801d85f KB |
18 | #endif |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
a1b82138 | 23 | #pragma hdrstop |
c801d85f KB |
24 | #endif //__BORLANDC__ |
25 | ||
a3a584a7 | 26 | #if !wxUSE_FILE || !wxUSE_TEXTBUFFER |
a1b82138 VZ |
27 | #undef wxUSE_TEXTFILE |
28 | #define wxUSE_TEXTFILE 0 | |
29 | #endif // wxUSE_FILE | |
30 | ||
a3a584a7 VZ |
31 | #if wxUSE_TEXTFILE |
32 | ||
ce4169a4 | 33 | #ifndef WX_PRECOMP |
a1b82138 VZ |
34 | #include "wx/string.h" |
35 | #include "wx/intl.h" | |
36 | #include "wx/file.h" | |
37 | #include "wx/log.h" | |
ce4169a4 RR |
38 | #endif |
39 | ||
a3a584a7 | 40 | #include "wx/textfile.h" |
c801d85f KB |
41 | |
42 | // ============================================================================ | |
43 | // wxTextFile class implementation | |
44 | // ============================================================================ | |
45 | ||
a3a584a7 VZ |
46 | wxTextFile::wxTextFile(const wxString& strFileName) |
47 | : wxTextBuffer(strFileName) | |
a1b82138 | 48 | { |
a1b82138 VZ |
49 | } |
50 | ||
c801d85f KB |
51 | |
52 | // ---------------------------------------------------------------------------- | |
53 | // file operations | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
a3a584a7 | 56 | bool wxTextFile::OnExists() const |
ef8d96c2 | 57 | { |
a3a584a7 | 58 | return wxFile::Exists(m_strBufferName); |
ef8d96c2 VZ |
59 | } |
60 | ||
1b6dea5d | 61 | |
a3a584a7 | 62 | bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode OpenMode) |
1b6dea5d | 63 | { |
a3a584a7 | 64 | wxFile::OpenMode FileOpenMode = wxFile::read; |
27752aab DW |
65 | int nAssertVal = 0; |
66 | ||
a3a584a7 | 67 | switch (OpenMode) |
1b6dea5d | 68 | { |
a3a584a7 VZ |
69 | case ReadAccess : |
70 | FileOpenMode = wxFile::read; | |
71 | break; | |
72 | case WriteAccess : | |
73 | FileOpenMode = wxFile::write; | |
74 | break; | |
75 | default : | |
27752aab | 76 | wxASSERT(nAssertVal); // Should not happen. |
a3a584a7 | 77 | break; |
1b6dea5d | 78 | } |
1b6dea5d | 79 | |
a3a584a7 | 80 | return m_file.Open(strBufferName.c_str(), FileOpenMode); |
c801d85f KB |
81 | } |
82 | ||
c801d85f | 83 | |
a3a584a7 | 84 | bool wxTextFile::OnClose() |
c801d85f | 85 | { |
a3a584a7 | 86 | return m_file.Close(); |
c801d85f KB |
87 | } |
88 | ||
a3a584a7 VZ |
89 | |
90 | bool wxTextFile::OnRead(wxMBConv& conv) | |
c801d85f KB |
91 | { |
92 | // file should be opened and we must be in it's beginning | |
93 | wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); | |
94 | ||
adfed1ca VS |
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 | ||
c801d85f KB |
103 | wxString str; |
104 | char ch, chLast = '\0'; | |
ba7f9a90 VZ |
105 | char buf[1024]; |
106 | int n, nRead; | |
f6bcfd97 | 107 | do { |
ba7f9a90 | 108 | nRead = m_file.Read(buf, WXSIZEOF(buf)); |
1678ad78 | 109 | if ( nRead == wxInvalidOffset ) { |
ba7f9a90 | 110 | // read error (error message already given in wxFile::Read) |
c801d85f KB |
111 | return FALSE; |
112 | } | |
113 | ||
ba7f9a90 VZ |
114 | for ( n = 0; n < nRead; n++ ) { |
115 | ch = buf[n]; | |
116 | switch ( ch ) { | |
117 | case '\n': | |
118 | // Dos/Unix line termination | |
a3a584a7 | 119 | AddLine(str, chLast == '\r' ? wxTextFileType_Dos |
c2389495 | 120 | : wxTextFileType_Unix); |
ba7f9a90 VZ |
121 | str.Empty(); |
122 | chLast = '\n'; | |
123 | break; | |
124 | ||
125 | case '\r': | |
126 | if ( chLast == '\r' ) { | |
127 | // Mac empty line | |
a3a584a7 | 128 | AddLine(wxEmptyString, wxTextFileType_Mac); |
ba7f9a90 VZ |
129 | } |
130 | else | |
131 | chLast = '\r'; | |
132 | break; | |
133 | ||
134 | default: | |
135 | if ( chLast == '\r' ) { | |
136 | // Mac line termination | |
a3a584a7 | 137 | AddLine(str, wxTextFileType_Mac); |
7749df1f | 138 | chLast = ch; |
adfed1ca | 139 | #if wxUSE_UNICODE |
9298c435 | 140 | conv_mbBuf[0] = ch; |
adfed1ca VS |
141 | if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) |
142 | conv_wcBuf[0] = ch; | |
143 | str = conv_wcBuf[0]; | |
144 | #else | |
ba7f9a90 | 145 | str = ch; |
adfed1ca | 146 | #endif |
ba7f9a90 VZ |
147 | } |
148 | else { | |
149 | // add to the current line | |
adfed1ca | 150 | #if wxUSE_UNICODE |
9298c435 | 151 | conv_mbBuf[0] = ch; |
adfed1ca VS |
152 | if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) |
153 | conv_wcBuf[0] = ch; | |
154 | str += conv_wcBuf[0]; | |
155 | #else | |
ba7f9a90 | 156 | str += ch; |
adfed1ca | 157 | #endif |
ba7f9a90 VZ |
158 | } |
159 | } | |
c801d85f | 160 | } |
f6bcfd97 | 161 | } while ( nRead == WXSIZEOF(buf) ); |
c801d85f KB |
162 | |
163 | // anything in the last line? | |
164 | if ( !str.IsEmpty() ) { | |
a3a584a7 | 165 | AddLine(str, wxTextFileType_None); // no line terminator |
c801d85f KB |
166 | } |
167 | ||
168 | return TRUE; | |
169 | } | |
170 | ||
f42d2aba | 171 | |
a3a584a7 | 172 | bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv) |
c801d85f | 173 | { |
a3a584a7 | 174 | wxTempFile fileTmp(m_strBufferName); |
c801d85f | 175 | |
a3a584a7 VZ |
176 | if ( !fileTmp.IsOpened() ) { |
177 | wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str()); | |
178 | return FALSE; | |
179 | } | |
c801d85f | 180 | |
a3a584a7 VZ |
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 | } | |
c801d85f | 188 | |
a3a584a7 VZ |
189 | // replace the old file with this one |
190 | return fileTmp.Commit(); | |
ba7f9a90 | 191 | } |
6164d85e | 192 | |
a1b82138 | 193 | #endif // wxUSE_TEXTFILE |
6164d85e | 194 |