]> git.saurik.com Git - wxWidgets.git/blame - src/common/textfile.cpp
fix as discuused on wxdev for bc54
[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>
65571936 9// Licence: wxWindows licence
c801d85f
KB
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers
14// ============================================================================
15
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 93{
d9ade1df
VS
94 // file should be opened and we must be in it's beginning
95 wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 );
adfed1ca 96
d9ade1df
VS
97 char *strBuf, *strPtr, *strEnd;
98 char ch, chLast = '\0';
99 char buf[1024];
30984dea 100 size_t nRead;
c801d85f 101
d9ade1df
VS
102 strPtr = strBuf = new char[1024];
103 strEnd = strBuf + 1024;
ba7f9a90 104
cb719f2e 105 do
d9ade1df
VS
106 {
107 nRead = m_file.Read(buf, WXSIZEOF(buf));
30984dea 108 if ( nRead == (size_t)wxInvalidOffset )
d9ade1df
VS
109 {
110 // read error (error message already given in wxFile::Read)
111 delete[] strBuf;
cb719f2e 112 return false;
d9ade1df
VS
113 }
114
30984dea 115 for (size_t n = 0; n < nRead; n++)
d9ade1df
VS
116 {
117 ch = buf[n];
cb719f2e 118 switch ( ch )
d9ade1df
VS
119 {
120 case '\n':
121 // Dos/Unix line termination
122 *strPtr = '\0';
cb719f2e 123 AddLine(wxString(strBuf, conv),
d9ade1df
VS
124 chLast == '\r' ? wxTextFileType_Dos
125 : wxTextFileType_Unix);
126 strPtr = strBuf;
127 chLast = '\n';
128 break;
129
130 case '\r':
cb719f2e 131 if ( chLast == '\r' )
d9ade1df
VS
132 {
133 // Mac empty line
134 AddLine(wxEmptyString, wxTextFileType_Mac);
135 }
136 else
137 chLast = '\r';
138 break;
139
140 default:
141 if ( chLast == '\r' )
142 {
143 // Mac line termination
144 *strPtr = '\0';
145 AddLine(wxString(strBuf, conv), wxTextFileType_Mac);
146 chLast = ch;
147 strPtr = strBuf;
148 *(strPtr++) = ch;
149 }
cb719f2e 150 else
d9ade1df
VS
151 {
152 // add to the current line
153 *(strPtr++) = ch;
154 if ( strPtr == strEnd )
155 {
156 // we must allocate more memory
157 size_t size = strEnd - strBuf;
158 char *newBuf = new char[size + 1024];
159 memcpy(newBuf, strBuf, size);
160 delete[] strBuf;
161 strBuf = newBuf;
162 strEnd = strBuf + size + 1024;
163 strPtr = strBuf + size;
164 }
165 }
166 }
167 }
168 } while ( nRead == WXSIZEOF(buf) );
169
170 // anything in the last line?
cb719f2e 171 if ( strPtr != strBuf )
d9ade1df
VS
172 {
173 *strPtr = '\0';
cb719f2e 174 AddLine(wxString(strBuf, conv),
d9ade1df 175 wxTextFileType_None); // no line terminator
c801d85f 176 }
c801d85f 177
d9ade1df 178 delete[] strBuf;
cb719f2e 179 return true;
c801d85f
KB
180}
181
f42d2aba 182
a3a584a7 183bool wxTextFile::OnWrite(wxTextFileType typeNew, wxMBConv& conv)
c801d85f 184{
68c97af3 185 wxFileName fn = m_strBufferName;
baed1077
JS
186
187 // We do NOT want wxPATH_NORM_CASE here, or the case will not
188 // be preserved.
68c97af3 189 if ( !fn.IsAbsolute() )
32a0d013
VS
190 fn.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_TILDE |
191 wxPATH_NORM_ABSOLUTE | wxPATH_NORM_LONG);
68c97af3 192
deab4540 193 wxTempFile fileTmp(fn.GetFullPath());
c801d85f 194
a3a584a7
VZ
195 if ( !fileTmp.IsOpened() ) {
196 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName.c_str());
cb719f2e 197 return false;
a3a584a7 198 }
c801d85f 199
a3a584a7
VZ
200 size_t nCount = GetLineCount();
201 for ( size_t n = 0; n < nCount; n++ ) {
202 fileTmp.Write(GetLine(n) +
203 GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
204 : typeNew),
205 conv);
206 }
c801d85f 207
a3a584a7
VZ
208 // replace the old file with this one
209 return fileTmp.Commit();
ba7f9a90 210}
6164d85e 211
a1b82138 212#endif // wxUSE_TEXTFILE
6164d85e 213