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