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