]> git.saurik.com Git - wxWidgets.git/blame - src/common/textfile.cpp
changes to match listctrl.h changes (forgot to commit yesterday, sorry)
[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{
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 83bool wxTextFile::OnClose()
c801d85f 84{
a3a584a7 85 return m_file.Close();
c801d85f
KB
86}
87
a3a584a7
VZ
88
89bool 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
VS
138#if wxUSE_UNICODE
139 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
140 conv_wcBuf[0] = ch;
141 str = conv_wcBuf[0];
142#else
ba7f9a90 143 str = ch;
adfed1ca 144#endif
ba7f9a90
VZ
145 }
146 else {
147 // add to the current line
adfed1ca
VS
148#if wxUSE_UNICODE
149 if (conv.MB2WC(conv_wcBuf, conv_mbBuf, 2) == (size_t)-1)
150 conv_wcBuf[0] = ch;
151 str += conv_wcBuf[0];
152#else
ba7f9a90 153 str += ch;
adfed1ca 154#endif
ba7f9a90
VZ
155 }
156 }
c801d85f 157 }
f6bcfd97 158 } while ( nRead == WXSIZEOF(buf) );
c801d85f
KB
159
160 // anything in the last line?
161 if ( !str.IsEmpty() ) {
a3a584a7 162 AddLine(str, wxTextFileType_None); // no line terminator
c801d85f
KB
163 }
164
165 return TRUE;
166}
167
f42d2aba 168
a3a584a7 169bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
c801d85f 170{
a3a584a7 171 wxTempFile fileTmp(m_strBufferName);
c801d85f 172
a3a584a7
VZ
173 if ( !fileTmp.IsOpened() ) {
174 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
175 return FALSE;
176 }
c801d85f 177
a3a584a7
VZ
178 size_t nCount = GetLineCount();
179 for ( size_t n = 0; n < nCount; n++ ) {
180 fileTmp.Write(GetLine(n) +
181 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
182 : typeNew),
183 conv);
184 }
c801d85f 185
a3a584a7
VZ
186 // replace the old file with this one
187 return fileTmp.Commit();
ba7f9a90 188}
6164d85e 189
a1b82138 190#endif // wxUSE_TEXTFILE
6164d85e 191