]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textfile.cpp
931c5048b66963dc7e505eb0eef01c8b041a24b7
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 wxTextFileType
wxTextFile::typeDefault
=
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
)
78 bool wxTextFile::Open()
80 // file name must be either given in ctor or in Open(const wxString&)
81 wxASSERT( !m_strFile
.IsEmpty() );
83 // open file in read-only mode
84 if ( !m_file
.Open(m_strFile
) )
87 // read file into memory
95 // analyse some lines of the file trying to guess it's type.
96 // if it fails, it assumes the native type for our platform.
97 wxTextFileType
wxTextFile::GuessType() const
99 // file should be opened and we must be in it's beginning
100 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
102 // scan the file lines
103 size_t nUnix
= 0, // number of '\n's alone
104 nDos
= 0, // number of '\r\n'
105 nMac
= 0; // number of '\r's
107 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
108 #define MAX_LINES_SCAN (10)
109 size_t nCount
= m_aLines
.Count() / 3,
110 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
112 #define AnalyseLine(n) \
113 switch ( m_aTypes[n] ) { \
114 case wxTextFileType_Unix: nUnix++; break; \
115 case wxTextFileType_Dos: nDos++; break; \
116 case wxTextFileType_Mac: nMac++; break; \
117 default: wxFAIL_MSG(_("unknown line terminator")); \
121 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
123 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
125 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
130 // interpret the results (@@ far from being even 50% fool proof)
131 if ( nDos
+ nUnix
+ nMac
== 0 ) {
132 // no newlines at all
133 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
136 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
138 ? wxTextFileType_##t1 \
139 : wxTextFileType_##t2
141 // Watcom C++ doesn't seem to be able to handle the macro
142 #if !defined(__WATCOMC__)
144 return GREATER_OF(Dos
, Mac
);
145 else if ( nDos
< nUnix
)
146 return GREATER_OF(Unix
, Mac
);
149 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
151 #endif // __WATCOMC__
159 bool wxTextFile::Read()
161 // file should be opened and we must be in it's beginning
162 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
165 char ch
, chLast
= '\0';
168 while ( !m_file
.Eof() ) {
169 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
170 if ( nRead
== wxInvalidOffset
) {
171 // read error (error message already given in wxFile::Read)
176 for ( n
= 0; n
< nRead
; n
++ ) {
180 // Dos/Unix line termination
182 m_aTypes
.Add(chLast
== '\r' ? wxTextFileType_Dos
183 : wxTextFileType_Unix
);
189 if ( chLast
== '\r' ) {
192 m_aTypes
.Add(wxTextFileType_Mac
);
199 if ( chLast
== '\r' ) {
200 // Mac line termination
202 m_aTypes
.Add(wxTextFileType_Mac
);
207 // add to the current line
214 // anything in the last line?
215 if ( !str
.IsEmpty() ) {
216 m_aTypes
.Add(wxTextFileType_None
); // no line terminator
223 bool wxTextFile::Close()
232 bool wxTextFile::Write(wxTextFileType typeNew
)
234 wxTempFile
fileTmp(m_strFile
);
236 if ( !fileTmp
.IsOpened() ) {
237 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
241 size_t nCount
= m_aLines
.Count();
242 for ( size_t n
= 0; n
< nCount
; n
++ ) {
243 fileTmp
.Write(m_aLines
[n
] +
244 GetEOL(typeNew
== wxTextFileType_None
? m_aTypes
[n
]
248 // replace the old file with this one
249 return fileTmp
.Commit();
252 const char *wxTextFile::GetEOL(wxTextFileType type
)
255 case wxTextFileType_None
: return "";
256 case wxTextFileType_Unix
: return "\n";
257 case wxTextFileType_Dos
: return "\r\n";
258 case wxTextFileType_Mac
: return "\r";
261 wxFAIL_MSG("bad file type in wxTextFile::GetEOL.");
262 return (const char *) NULL
;