]>
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; |
1b6dea5d | 65 | |
a3a584a7 | 66 | switch (OpenMode) |
1b6dea5d | 67 | { |
a3a584a7 VZ |
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; | |
1b6dea5d | 77 | } |
1b6dea5d | 78 | |
a3a584a7 | 79 | return m_file.Open(strBufferName.c_str(), FileOpenMode); |
c801d85f KB |
80 | } |
81 | ||
c801d85f | 82 | |
a3a584a7 | 83 | bool wxTextFile::OnClose() |
c801d85f | 84 | { |
a3a584a7 | 85 | return m_file.Close(); |
c801d85f KB |
86 | } |
87 | ||
a3a584a7 VZ |
88 | |
89 | bool wxTextFile::OnRead(wxMBConv& conv) | |
c801d85f KB |
90 | { |
91 | // file should be opened and we must be in it's beginning | |
92 | wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); | |
93 | ||
adfed1ca VS |
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 | ||
c801d85f KB |
102 | wxString str; |
103 | char ch, chLast = '\0'; | |
ba7f9a90 VZ |
104 | char buf[1024]; |
105 | int n, nRead; | |
f6bcfd97 | 106 | do { |
ba7f9a90 | 107 | nRead = m_file.Read(buf, WXSIZEOF(buf)); |
1678ad78 | 108 | if ( nRead == wxInvalidOffset ) { |
ba7f9a90 | 109 | // read error (error message already given in wxFile::Read) |
c801d85f KB |
110 | return FALSE; |
111 | } | |
112 | ||
ba7f9a90 VZ |
113 | for ( n = 0; n < nRead; n++ ) { |
114 | ch = buf[n]; | |
115 | switch ( ch ) { | |
116 | case '\n': | |
117 | // Dos/Unix line termination | |
a3a584a7 | 118 | AddLine(str, chLast == '\r' ? wxTextFileType_Dos |
c2389495 | 119 | : wxTextFileType_Unix); |
ba7f9a90 VZ |
120 | str.Empty(); |
121 | chLast = '\n'; | |
122 | break; | |
123 | ||
124 | case '\r': | |
125 | if ( chLast == '\r' ) { | |
126 | // Mac empty line | |
a3a584a7 | 127 | AddLine(wxEmptyString, wxTextFileType_Mac); |
ba7f9a90 VZ |
128 | } |
129 | else | |
130 | chLast = '\r'; | |
131 | break; | |
132 | ||
133 | default: | |
134 | if ( chLast == '\r' ) { | |
135 | // Mac line termination | |
a3a584a7 | 136 | AddLine(str, wxTextFileType_Mac); |
7749df1f | 137 | chLast = ch; |
adfed1ca | 138 | #if wxUSE_UNICODE |
9298c435 | 139 | conv_mbBuf[0] = ch; |
adfed1ca VS |
140 | if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) |
141 | conv_wcBuf[0] = ch; | |
142 | str = conv_wcBuf[0]; | |
143 | #else | |
ba7f9a90 | 144 | str = ch; |
adfed1ca | 145 | #endif |
ba7f9a90 VZ |
146 | } |
147 | else { | |
148 | // add to the current line | |
adfed1ca | 149 | #if wxUSE_UNICODE |
9298c435 | 150 | conv_mbBuf[0] = ch; |
adfed1ca VS |
151 | if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1) |
152 | conv_wcBuf[0] = ch; | |
153 | str += conv_wcBuf[0]; | |
154 | #else | |
ba7f9a90 | 155 | str += ch; |
adfed1ca | 156 | #endif |
ba7f9a90 VZ |
157 | } |
158 | } | |
c801d85f | 159 | } |
f6bcfd97 | 160 | } while ( nRead == WXSIZEOF(buf) ); |
c801d85f KB |
161 | |
162 | // anything in the last line? | |
163 | if ( !str.IsEmpty() ) { | |
a3a584a7 | 164 | AddLine(str, wxTextFileType_None); // no line terminator |
c801d85f KB |
165 | } |
166 | ||
167 | return TRUE; | |
168 | } | |
169 | ||
f42d2aba | 170 | |
a3a584a7 | 171 | bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv) |
c801d85f | 172 | { |
a3a584a7 | 173 | wxTempFile fileTmp(m_strBufferName); |
c801d85f | 174 | |
a3a584a7 VZ |
175 | if ( !fileTmp.IsOpened() ) { |
176 | wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str()); | |
177 | return FALSE; | |
178 | } | |
c801d85f | 179 | |
a3a584a7 VZ |
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 | } | |
c801d85f | 187 | |
a3a584a7 VZ |
188 | // replace the old file with this one |
189 | return fileTmp.Commit(); | |
ba7f9a90 | 190 | } |
6164d85e | 191 | |
a1b82138 | 192 | #endif // wxUSE_TEXTFILE |
6164d85e | 193 |