]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
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 #include <wx/string.h>
30 #include <wx/textfile.h>
32 // ============================================================================
33 // wxTextFile class implementation
34 // ============================================================================
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // default type is the native one
41 const wxTextFile::Type
wxTextFile::typeDefault
= wxTextFile::
42 #if defined(__WINDOWS__)
44 #elif defined(__UNIX__)
46 #elif defined(__WXMAC__)
48 // if you feel brave, remove the next line
49 #error "wxTextFile: code for Mac files is untested."
52 #error "wxTextFile: unsupported platform."
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
64 wxTextFile::~wxTextFile()
66 // m_file dtor called automatically
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 bool wxTextFile::Open(const wxString
& strFile
)
79 bool wxTextFile::Open()
81 // file name must be either given in ctor or in Open(const wxString&)
82 wxASSERT( !m_strFile
.IsEmpty() );
84 // open file in read-only mode
85 if ( !m_file
.Open(m_strFile
) )
88 // read file into memory
96 // analyse some lines of the file trying to guess it's type.
97 // if it fails, it assumes the native type for our platform.
98 wxTextFile::Type
wxTextFile::GuessType() const
100 // file should be opened and we must be in it's beginning
101 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
103 // scan the file lines
104 uint nUnix
= 0, // number of '\n's alone
105 nDos
= 0, // number of '\r\n'
106 nMac
= 0; // number of '\r's
108 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
109 #define MAX_LINES_SCAN (10)
110 uint nCount
= m_aLines
.Count() / 3,
111 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
113 #define AnalyseLine(n) \
114 switch ( m_aTypes[n] ) { \
115 case Type_Unix: nUnix++; break; \
116 case Type_Dos: nDos++; break; \
117 case Type_Mac: nMac++; break; \
118 default: wxFAIL_MSG(_("unknown line terminator")); \
122 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
124 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
126 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
131 // interpret the results (@@ far from being even 50% fool proof)
132 if ( nDos
+ nUnix
+ nMac
== 0 ) {
133 // no newlines at all
134 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
137 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
138 : n##t1 > n##t2 ? Type_##t1 \
142 return GREATER_OF(Dos
, Mac
);
143 else if ( nDos
< nUnix
)
144 return GREATER_OF(Unix
, Mac
);
147 return nMac
> nDos
? Type_Mac
: typeDefault
;
156 bool wxTextFile::Read()
158 // file should be opened and we must be in it's beginning
159 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
162 char ch
, chLast
= '\0';
165 while ( !m_file
.Eof() ) {
166 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
167 if ( nRead
== wxInvalidOffset
) {
168 // read error (error message already given in wxFile::Read)
174 #pragma message("wxTextFile::Read() hasn't been tested with Mac files.")
177 for ( n
= 0; n
< nRead
; n
++ ) {
181 // Dos/Unix line termination
183 m_aTypes
.Add(chLast
== '\r' ? Type_Dos
: Type_Unix
);
189 if ( chLast
== '\r' ) {
192 m_aTypes
.Add(Type_Mac
);
199 if ( chLast
== '\r' ) {
200 // Mac line termination
202 m_aTypes
.Add(Type_Mac
);
206 // add to the current line
213 // anything in the last line?
214 if ( !str
.IsEmpty() ) {
215 m_aTypes
.Add(Type_None
); // no line terminator
222 bool wxTextFile::Write(Type typeNew
)
224 wxTempFile
fileTmp(m_strFile
);
226 if ( !fileTmp
.IsOpened() ) {
227 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
231 uint nCount
= m_aLines
.Count();
232 for ( uint n
= 0; n
< nCount
; n
++ ) {
233 fileTmp
.Write(m_aLines
[n
] +
234 GetEOL(typeNew
== Type_None
? m_aTypes
[n
] : typeNew
));
237 // replace the old file with this one
238 return fileTmp
.Commit();