1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textfile.cpp
3 // Purpose: implementation of wxTextFile class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #include "wx/wxprec.h"
22 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
24 #define wxUSE_TEXTFILE 0
30 #include "wx/string.h"
36 #include "wx/textfile.h"
37 #include "wx/filename.h"
39 // ============================================================================
40 // wxTextFile class implementation
41 // ============================================================================
43 wxTextFile::wxTextFile(const wxString
& strFileName
)
44 : wxTextBuffer(strFileName
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 bool wxTextFile::OnExists() const
55 return wxFile::Exists(m_strBufferName
);
59 bool wxTextFile::OnOpen(const wxString
&strBufferName
, wxTextBufferOpenMode OpenMode
)
61 wxFile::OpenMode FileOpenMode
;
66 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
70 FileOpenMode
= wxFile::read
;
74 FileOpenMode
= wxFile::write
;
78 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
82 bool wxTextFile::OnClose()
84 return m_file
.Close();
88 bool wxTextFile::OnRead(wxMBConv
& conv
)
90 // file should be opened and we must be in it's beginning
91 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
93 char *strBuf
, *strPtr
, *strEnd
;
94 char ch
, chLast
= '\0';
98 strPtr
= strBuf
= new char[1024];
99 strEnd
= strBuf
+ 1024;
103 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
104 if ( nRead
== (size_t)wxInvalidOffset
)
106 // read error (error message already given in wxFile::Read)
111 for (size_t n
= 0; n
< nRead
; n
++)
117 // Dos/Unix line termination
119 AddLine(wxString(strBuf
, conv
),
120 chLast
== '\r' ? wxTextFileType_Dos
121 : wxTextFileType_Unix
);
127 if ( chLast
== '\r' )
130 AddLine(wxEmptyString
, wxTextFileType_Mac
);
137 if ( chLast
== '\r' )
139 // Mac line termination
141 AddLine(wxString(strBuf
, conv
), wxTextFileType_Mac
);
148 // add to the current line
150 if ( strPtr
== strEnd
)
152 // we must allocate more memory
153 size_t size
= strEnd
- strBuf
;
154 char *newBuf
= new char[size
+ 1024];
155 memcpy(newBuf
, strBuf
, size
);
158 strEnd
= strBuf
+ size
+ 1024;
159 strPtr
= strBuf
+ size
;
164 } while ( nRead
== WXSIZEOF(buf
) );
166 // anything in the last line?
167 if ( strPtr
!= strBuf
)
170 AddLine(wxString(strBuf
, conv
),
171 wxTextFileType_None
); // no line terminator
179 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
181 wxFileName fn
= m_strBufferName
;
183 // We do NOT want wxPATH_NORM_CASE here, or the case will not
185 if ( !fn
.IsAbsolute() )
186 fn
.Normalize(wxPATH_NORM_ENV_VARS
| wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
|
187 wxPATH_NORM_ABSOLUTE
| wxPATH_NORM_LONG
);
189 wxTempFile
fileTmp(fn
.GetFullPath());
191 if ( !fileTmp
.IsOpened() ) {
192 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
196 size_t nCount
= GetLineCount();
197 for ( size_t n
= 0; n
< nCount
; n
++ ) {
198 fileTmp
.Write(GetLine(n
) +
199 GetEOL(typeNew
== wxTextFileType_None
? GetLineType(n
)
204 // replace the old file with this one
205 return fileTmp
.Commit();
208 #endif // wxUSE_TEXTFILE