]>
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"
28 #define wxUSE_TEXTFILE 0
32 #include "wx/string.h"
38 #include "wx/textfile.h"
40 // ============================================================================
41 // wxTextFile class implementation
42 // ============================================================================
44 // ----------------------------------------------------------------------------
45 // static methods (always compiled in)
46 // ----------------------------------------------------------------------------
48 // default type is the native one
49 const wxTextFileType
wxTextFile::typeDefault
=
50 #if defined(__WINDOWS__)
52 #elif defined(__UNIX__)
54 #elif defined(__WXMAC__)
56 #elif defined(__WXPM__)
60 #error "wxTextFile: unsupported platform."
63 const wxChar
*wxTextFile::GetEOL(wxTextFileType type
)
67 wxFAIL_MSG(wxT("bad file type in wxTextFile::GetEOL."));
68 // fall through nevertheless - we must return something...
70 case wxTextFileType_None
: return wxT(_T(""));
71 case wxTextFileType_Unix
: return wxT(_T("\n"));
72 case wxTextFileType_Dos
: return wxT(_T("\r\n"));
73 case wxTextFileType_Mac
: return wxT(_T("\r"));
77 wxString
wxTextFile::Translate(const wxString
& text
, wxTextFileType type
)
79 // don't do anything if there is nothing to do
80 if ( type
== wxTextFileType_None
)
83 wxString eol
= GetEOL(type
), result
;
85 // optimization: we know that the length of the new string will be about
86 // the same as the length of the old one, so prealloc memory to aviod
87 // unnecessary relocations
88 result
.Alloc(text
.Len());
91 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
96 // Dos/Unix line termination
102 if ( chLast
== _T('\r') ) {
107 // just remember it: we don't know whether it is just "\r"
114 if ( chLast
== _T('\r') ) {
115 // Mac line termination
119 // add to the current line
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
144 wxTextFile::~wxTextFile()
146 // m_file dtor called automatically
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 bool wxTextFile::Exists() const
155 return wxFile::Exists(m_strFile
);
158 bool wxTextFile::Open(const wxString
& strFile
)
165 bool wxTextFile::Open()
167 // file name must be either given in ctor or in Open(const wxString&)
168 wxASSERT( !m_strFile
.IsEmpty() );
170 // open file in read-only mode
171 if ( !m_file
.Open(m_strFile
) )
174 // read file into memory
182 // analyse some lines of the file trying to guess it's type.
183 // if it fails, it assumes the native type for our platform.
184 wxTextFileType
wxTextFile::GuessType() const
186 // file should be opened and we must be in it's beginning
187 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
189 // scan the file lines
190 size_t nUnix
= 0, // number of '\n's alone
191 nDos
= 0, // number of '\r\n'
192 nMac
= 0; // number of '\r's
194 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
195 #define MAX_LINES_SCAN (10)
196 size_t nCount
= m_aLines
.Count() / 3,
197 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
199 #define AnalyseLine(n) \
200 switch ( m_aTypes[n] ) { \
201 case wxTextFileType_Unix: nUnix++; break; \
202 case wxTextFileType_Dos: nDos++; break; \
203 case wxTextFileType_Mac: nMac++; break; \
204 default: wxFAIL_MSG(_("unknown line terminator")); \
208 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
210 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
212 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
217 // interpret the results (FIXME far from being even 50% fool proof)
218 if ( nDos
+ nUnix
+ nMac
== 0 ) {
219 // no newlines at all
220 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
223 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
225 ? wxTextFileType_##t1 \
226 : wxTextFileType_##t2
228 // Watcom C++ doesn't seem to be able to handle the macro
229 #if !defined(__WATCOMC__)
231 return GREATER_OF(Dos
, Mac
);
232 else if ( nDos
< nUnix
)
233 return GREATER_OF(Unix
, Mac
);
236 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
238 #endif // __WATCOMC__
246 bool wxTextFile::Read()
248 // file should be opened and we must be in it's beginning
249 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
252 char ch
, chLast
= '\0';
255 while ( !m_file
.Eof() ) {
256 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
257 if ( nRead
== wxInvalidOffset
) {
258 // read error (error message already given in wxFile::Read)
262 for ( n
= 0; n
< nRead
; n
++ ) {
266 // Dos/Unix line termination
268 m_aTypes
.Add(chLast
== '\r' ? wxTextFileType_Dos
269 : wxTextFileType_Unix
);
275 if ( chLast
== '\r' ) {
277 m_aLines
.Add(wxEmptyString
);
278 m_aTypes
.Add(wxTextFileType_Mac
);
285 if ( chLast
== '\r' ) {
286 // Mac line termination
288 m_aTypes
.Add(wxTextFileType_Mac
);
293 // add to the current line
300 // anything in the last line?
301 if ( !str
.IsEmpty() ) {
302 m_aTypes
.Add(wxTextFileType_None
); // no line terminator
309 bool wxTextFile::Close()
319 bool wxTextFile::Write(wxTextFileType typeNew
)
321 wxTempFile
fileTmp(m_strFile
);
323 if ( !fileTmp
.IsOpened() ) {
324 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
328 size_t nCount
= m_aLines
.Count();
329 for ( size_t n
= 0; n
< nCount
; n
++ ) {
330 fileTmp
.Write(m_aLines
[n
] +
331 GetEOL(typeNew
== wxTextFileType_None
? m_aTypes
[n
]
335 // replace the old file with this one
336 return fileTmp
.Commit();
339 #endif // wxUSE_TEXTFILE