]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
d45a4cc49897bae4866e763740e780f788d124a8
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"
30 #if wxUSE_TEXTFILE && wxUSE_FILE
32 #include <wx/string.h>
36 #include <wx/textfile.h>
38 // ============================================================================
39 // wxTextFile class implementation
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // default type is the native one
47 const wxTextFileType
wxTextFile::typeDefault
=
48 #if defined(__WINDOWS__)
50 #elif defined(__UNIX__)
52 #elif defined(__WXMAC__)
56 #error "wxTextFile: unsupported platform."
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
68 wxTextFile::~wxTextFile()
70 // m_file dtor called automatically
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 bool wxTextFile::Open(const wxString
& strFile
)
84 bool wxTextFile::Open()
86 // file name must be either given in ctor or in Open(const wxString&)
87 wxASSERT( !m_strFile
.IsEmpty() );
89 // open file in read-only mode
90 if ( !m_file
.Open(m_strFile
) )
93 // read file into memory
101 // analyse some lines of the file trying to guess it's type.
102 // if it fails, it assumes the native type for our platform.
103 wxTextFileType
wxTextFile::GuessType() const
105 // file should be opened and we must be in it's beginning
106 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
108 // scan the file lines
109 size_t nUnix
= 0, // number of '\n's alone
110 nDos
= 0, // number of '\r\n'
111 nMac
= 0; // number of '\r's
113 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
114 #define MAX_LINES_SCAN (10)
115 size_t nCount
= m_aLines
.Count() / 3,
116 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
118 #define AnalyseLine(n) \
119 switch ( m_aTypes[n] ) { \
120 case wxTextFileType_Unix: nUnix++; break; \
121 case wxTextFileType_Dos: nDos++; break; \
122 case wxTextFileType_Mac: nMac++; break; \
123 default: wxFAIL_MSG(_("unknown line terminator")); \
127 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
129 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
131 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
136 // interpret the results (@@ far from being even 50% fool proof)
137 if ( nDos
+ nUnix
+ nMac
== 0 ) {
138 // no newlines at all
139 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
142 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
144 ? wxTextFileType_##t1 \
145 : wxTextFileType_##t2
147 // Watcom C++ doesn't seem to be able to handle the macro
148 #if !defined(__WATCOMC__)
150 return GREATER_OF(Dos
, Mac
);
151 else if ( nDos
< nUnix
)
152 return GREATER_OF(Unix
, Mac
);
155 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
157 #endif // __WATCOMC__
165 bool wxTextFile::Read()
167 // file should be opened and we must be in it's beginning
168 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
171 char ch
, chLast
= '\0';
174 while ( !m_file
.Eof() ) {
175 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
176 if ( nRead
== wxInvalidOffset
) {
177 // read error (error message already given in wxFile::Read)
182 for ( n
= 0; n
< nRead
; n
++ ) {
186 // Dos/Unix line termination
188 m_aTypes
.Add(chLast
== '\r' ? wxTextFileType_Dos
189 : wxTextFileType_Unix
);
195 if ( chLast
== '\r' ) {
198 m_aTypes
.Add(wxTextFileType_Mac
);
205 if ( chLast
== '\r' ) {
206 // Mac line termination
208 m_aTypes
.Add(wxTextFileType_Mac
);
213 // add to the current line
220 // anything in the last line?
221 if ( !str
.IsEmpty() ) {
222 m_aTypes
.Add(wxTextFileType_None
); // no line terminator
229 bool wxTextFile::Close()
238 bool wxTextFile::Write(wxTextFileType typeNew
)
240 wxTempFile
fileTmp(m_strFile
);
242 if ( !fileTmp
.IsOpened() ) {
243 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
247 size_t nCount
= m_aLines
.Count();
248 for ( size_t n
= 0; n
< nCount
; n
++ ) {
249 fileTmp
.Write(m_aLines
[n
] +
250 GetEOL(typeNew
== wxTextFileType_None
? m_aTypes
[n
]
254 // replace the old file with this one
255 return fileTmp
.Commit();
258 const wxChar
*wxTextFile::GetEOL(wxTextFileType type
)
261 case wxTextFileType_None
: return _T("");
262 case wxTextFileType_Unix
: return _T("\n");
263 case wxTextFileType_Dos
: return _T("\r\n");
264 case wxTextFileType_Mac
: return _T("\r");
267 wxFAIL_MSG(_T("bad file type in wxTextFile::GetEOL."));
268 return (const wxChar
*) NULL
;