]>
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("");
71 case wxTextFileType_Unix
: return wxT("\n");
72 case wxTextFileType_Dos
: return wxT("\r\n");
73 case wxTextFileType_Mac
: return wxT("\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 // GRG: don't do anything either if it is empty
87 wxString eol
= GetEOL(type
), result
;
89 // optimization: we know that the length of the new string will be about
90 // the same as the length of the old one, so prealloc memory to aviod
91 // unnecessary relocations
92 result
.Alloc(text
.Len());
95 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
100 // Dos/Unix line termination
106 if ( chLast
== _T('\r') ) {
111 // just remember it: we don't know whether it is just "\r"
118 if ( chLast
== _T('\r') ) {
119 // Mac line termination
122 // reset chLast to avoid inserting another eol before the
127 // add to the current line
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
152 wxTextFile::~wxTextFile()
154 // m_file dtor called automatically
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 bool wxTextFile::Exists() const
163 return wxFile::Exists(m_strFile
);
166 bool wxTextFile::Create(const wxString
& strFile
)
173 bool wxTextFile::Create()
175 // file name must be either given in ctor or in Create(const wxString&)
176 wxASSERT( !m_strFile
.IsEmpty() );
178 // if the file already exists do nothing
179 if ( Exists() ) return FALSE
;
181 if ( m_file
.Open(m_strFile
, wxFile::write
) )
192 bool wxTextFile::Open(const wxString
& strFile
)
199 bool wxTextFile::Open()
201 // file name must be either given in ctor or in Open(const wxString&)
202 wxASSERT( !m_strFile
.IsEmpty() );
204 // open file in read-only mode
205 if ( !m_file
.Open(m_strFile
) )
208 // read file into memory
216 // analyse some lines of the file trying to guess it's type.
217 // if it fails, it assumes the native type for our platform.
218 wxTextFileType
wxTextFile::GuessType() const
220 wxASSERT( IsOpened() );
222 // scan the file lines
223 size_t nUnix
= 0, // number of '\n's alone
224 nDos
= 0, // number of '\r\n'
225 nMac
= 0; // number of '\r's
227 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
228 #define MAX_LINES_SCAN (10)
229 size_t nCount
= m_aLines
.Count() / 3,
230 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
232 #define AnalyseLine(n) \
233 switch ( m_aTypes[n] ) { \
234 case wxTextFileType_Unix: nUnix++; break; \
235 case wxTextFileType_Dos: nDos++; break; \
236 case wxTextFileType_Mac: nMac++; break; \
237 default: wxFAIL_MSG(_("unknown line terminator")); \
241 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
243 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
245 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
250 // interpret the results (FIXME far from being even 50% fool proof)
251 if ( nDos
+ nUnix
+ nMac
== 0 ) {
252 // no newlines at all
253 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
256 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
258 ? wxTextFileType_##t1 \
259 : wxTextFileType_##t2
261 // Watcom C++ doesn't seem to be able to handle the macro
262 #if !defined(__WATCOMC__)
264 return GREATER_OF(Dos
, Mac
);
265 else if ( nDos
< nUnix
)
266 return GREATER_OF(Unix
, Mac
);
269 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
271 #endif // __WATCOMC__
279 bool wxTextFile::Read()
281 // file should be opened and we must be in it's beginning
282 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
285 char ch
, chLast
= '\0';
289 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
290 if ( nRead
== wxInvalidOffset
) {
291 // read error (error message already given in wxFile::Read)
295 for ( n
= 0; n
< nRead
; n
++ ) {
299 // Dos/Unix line termination
301 m_aTypes
.Add(chLast
== '\r' ? wxTextFileType_Dos
302 : wxTextFileType_Unix
);
308 if ( chLast
== '\r' ) {
310 m_aLines
.Add(wxEmptyString
);
311 m_aTypes
.Add(wxTextFileType_Mac
);
318 if ( chLast
== '\r' ) {
319 // Mac line termination
321 m_aTypes
.Add(wxTextFileType_Mac
);
326 // add to the current line
331 } while ( nRead
== WXSIZEOF(buf
) );
333 // anything in the last line?
334 if ( !str
.IsEmpty() ) {
335 m_aTypes
.Add(wxTextFileType_None
); // no line terminator
342 bool wxTextFile::Close()
352 bool wxTextFile::Write(wxTextFileType typeNew
)
354 wxTempFile
fileTmp(m_strFile
);
356 if ( !fileTmp
.IsOpened() ) {
357 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
361 size_t nCount
= m_aLines
.Count();
362 for ( size_t n
= 0; n
< nCount
; n
++ ) {
363 fileTmp
.Write(m_aLines
[n
] +
364 GetEOL(typeNew
== wxTextFileType_None
? m_aTypes
[n
]
368 // replace the old file with this one
369 return fileTmp
.Commit();
372 #endif // wxUSE_TEXTFILE