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