]>
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__)
50 #error "wxTextFile: unsupported platform."
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
62 wxTextFile::~wxTextFile()
64 // m_file dtor called automatically
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 bool wxTextFile::Open(const wxString
& strFile
)
77 bool wxTextFile::Open()
79 // file name must be either given in ctor or in Open(const wxString&)
80 wxASSERT( !m_strFile
.IsEmpty() );
82 // open file in read-only mode
83 if ( !m_file
.Open(m_strFile
) )
86 // read file into memory
94 // analyse some lines of the file trying to guess it's type.
95 // if it fails, it assumes the native type for our platform.
96 wxTextFile::Type
wxTextFile::GuessType() const
98 // file should be opened and we must be in it's beginning
99 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
101 // scan the file lines
102 size_t nUnix
= 0, // number of '\n's alone
103 nDos
= 0, // number of '\r\n'
104 nMac
= 0; // number of '\r's
106 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
107 #define MAX_LINES_SCAN (10)
108 size_t nCount
= m_aLines
.Count() / 3,
109 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
111 #define AnalyseLine(n) \
112 switch ( m_aTypes[n] ) { \
113 case Type_Unix: nUnix++; break; \
114 case Type_Dos: nDos++; break; \
115 case Type_Mac: nMac++; break; \
116 default: wxFAIL_MSG(_("unknown line terminator")); \
120 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
122 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
124 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
129 // interpret the results (@@ far from being even 50% fool proof)
130 if ( nDos
+ nUnix
+ nMac
== 0 ) {
131 // no newlines at all
132 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
135 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
136 : n##t1 > n##t2 ? Type_##t1 \
140 return GREATER_OF(Dos
, Mac
);
141 else if ( nDos
< nUnix
)
142 return GREATER_OF(Unix
, Mac
);
145 return nMac
> nDos
? Type_Mac
: typeDefault
;
154 bool wxTextFile::Read()
156 // file should be opened and we must be in it's beginning
157 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
160 char ch
, chLast
= '\0';
163 while ( !m_file
.Eof() ) {
164 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
165 if ( nRead
== wxInvalidOffset
) {
166 // read error (error message already given in wxFile::Read)
171 for ( n
= 0; n
< nRead
; n
++ ) {
175 // Dos/Unix line termination
177 m_aTypes
.Add(chLast
== '\r' ? Type_Dos
: Type_Unix
);
183 if ( chLast
== '\r' ) {
186 m_aTypes
.Add(Type_Mac
);
193 if ( chLast
== '\r' ) {
194 // Mac line termination
196 m_aTypes
.Add(Type_Mac
);
201 // add to the current line
208 // anything in the last line?
209 if ( !str
.IsEmpty() ) {
210 m_aTypes
.Add(Type_None
); // no line terminator
217 bool wxTextFile::Write(Type typeNew
)
219 wxTempFile
fileTmp(m_strFile
);
221 if ( !fileTmp
.IsOpened() ) {
222 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
226 size_t nCount
= m_aLines
.Count();
227 for ( size_t n
= 0; n
< nCount
; n
++ ) {
228 fileTmp
.Write(m_aLines
[n
] +
229 GetEOL(typeNew
== Type_None
? m_aTypes
[n
] : typeNew
));
232 // replace the old file with this one
233 return fileTmp
.Commit();