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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "textfile.h"
20 #include "wx/wxprec.h"
26 #if !wxUSE_FILE || !wxUSE_TEXTBUFFER
28 #define wxUSE_TEXTFILE 0
34 #include "wx/string.h"
40 #include "wx/textfile.h"
41 #include "wx/filename.h"
43 // ============================================================================
44 // wxTextFile class implementation
45 // ============================================================================
47 wxTextFile::wxTextFile(const wxString
& strFileName
)
48 : wxTextBuffer(strFileName
)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 bool wxTextFile::OnExists() const
59 return wxFile::Exists(m_strBufferName
);
63 bool wxTextFile::OnOpen(const wxString
&strBufferName
, wxTextBufferOpenMode OpenMode
)
65 wxFile::OpenMode FileOpenMode
;
70 wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") );
74 FileOpenMode
= wxFile::read
;
78 FileOpenMode
= wxFile::write
;
82 return m_file
.Open(strBufferName
.c_str(), FileOpenMode
);
86 bool wxTextFile::OnClose()
88 return m_file
.Close();
92 bool wxTextFile::OnRead(wxMBConv
& conv
)
94 // file should be opened and we must be in it's beginning
95 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
97 char *strBuf
, *strPtr
, *strEnd
;
98 char ch
, chLast
= '\0';
102 strPtr
= strBuf
= new char[1024];
103 strEnd
= strBuf
+ 1024;
107 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
108 if ( nRead
== wxInvalidOffset
)
110 // read error (error message already given in wxFile::Read)
115 for (n
= 0; n
< nRead
; n
++)
121 // Dos/Unix line termination
123 AddLine(wxString(strBuf
, conv
),
124 chLast
== '\r' ? wxTextFileType_Dos
125 : wxTextFileType_Unix
);
131 if ( chLast
== '\r' )
134 AddLine(wxEmptyString
, wxTextFileType_Mac
);
141 if ( chLast
== '\r' )
143 // Mac line termination
145 AddLine(wxString(strBuf
, conv
), wxTextFileType_Mac
);
152 // add to the current line
154 if ( strPtr
== strEnd
)
156 // we must allocate more memory
157 size_t size
= strEnd
- strBuf
;
158 char *newBuf
= new char[size
+ 1024];
159 memcpy(newBuf
, strBuf
, size
);
162 strEnd
= strBuf
+ size
+ 1024;
163 strPtr
= strBuf
+ size
;
168 } while ( nRead
== WXSIZEOF(buf
) );
170 // anything in the last line?
171 if ( strPtr
!= strBuf
)
174 AddLine(wxString(strBuf
, conv
),
175 wxTextFileType_None
); // no line terminator
183 bool wxTextFile::OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
)
185 wxFileName fn
= m_strBufferName
;
187 // We do NOT want wxPATH_NORM_CASE here, or the case will not
189 if ( !fn
.IsAbsolute() )
190 fn
.Normalize(wxPATH_NORM_ENV_VARS
| wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
|
191 wxPATH_NORM_ABSOLUTE
| wxPATH_NORM_LONG
);
193 wxTempFile
fileTmp(fn
.GetFullPath());
195 if ( !fileTmp
.IsOpened() ) {
196 wxLogError(_("can't write buffer '%s' to disk."), m_strBufferName
.c_str());
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
)
208 // replace the old file with this one
209 return fileTmp
.Commit();
212 #endif // wxUSE_TEXTFILE