]>
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__)
58 #error "wxTextFile: unsupported platform."
61 const wxChar
*wxTextFile::GetEOL(wxTextFileType type
)
65 wxFAIL_MSG(_T("bad file type in wxTextFile::GetEOL."));
66 // fall through nevertheless - we must return something...
68 case wxTextFileType_None
: return _T("");
69 case wxTextFileType_Unix
: return _T("\n");
70 case wxTextFileType_Dos
: return _T("\r\n");
71 case wxTextFileType_Mac
: return _T("\r");
76 wxString
wxTextFile::Translate(const wxString
& text
, wxTextFileType type
)
78 // don't do anything if there is nothing to do
79 if ( type
== wxTextFileType_None
)
82 wxString eol
= GetEOL(type
), result
;
84 // optimization: we know that the length of the new string will be about
85 // the same as the length of the old one, so prealloc memory to aviod
86 // unnecessary relocations
87 result
.Alloc(text
.Len());
90 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
95 // Dos/Unix line termination
101 if ( chLast
== '\r' ) {
110 if ( chLast
== '\r' ) {
111 // Mac line termination
116 // add to the current line
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
137 wxTextFile::~wxTextFile()
139 // m_file dtor called automatically
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 bool wxTextFile::Open(const wxString
& strFile
)
153 bool wxTextFile::Open()
155 // file name must be either given in ctor or in Open(const wxString&)
156 wxASSERT( !m_strFile
.IsEmpty() );
158 // open file in read-only mode
159 if ( !m_file
.Open(m_strFile
) )
162 // read file into memory
170 // analyse some lines of the file trying to guess it's type.
171 // if it fails, it assumes the native type for our platform.
172 wxTextFileType
wxTextFile::GuessType() const
174 // file should be opened and we must be in it's beginning
175 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
177 // scan the file lines
178 size_t nUnix
= 0, // number of '\n's alone
179 nDos
= 0, // number of '\r\n'
180 nMac
= 0; // number of '\r's
182 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
183 #define MAX_LINES_SCAN (10)
184 size_t nCount
= m_aLines
.Count() / 3,
185 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
187 #define AnalyseLine(n) \
188 switch ( m_aTypes[n] ) { \
189 case wxTextFileType_Unix: nUnix++; break; \
190 case wxTextFileType_Dos: nDos++; break; \
191 case wxTextFileType_Mac: nMac++; break; \
192 default: wxFAIL_MSG(_("unknown line terminator")); \
196 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
198 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
200 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
205 // interpret the results (FIXME far from being even 50% fool proof)
206 if ( nDos
+ nUnix
+ nMac
== 0 ) {
207 // no newlines at all
208 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
211 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
213 ? wxTextFileType_##t1 \
214 : wxTextFileType_##t2
216 // Watcom C++ doesn't seem to be able to handle the macro
217 #if !defined(__WATCOMC__)
219 return GREATER_OF(Dos
, Mac
);
220 else if ( nDos
< nUnix
)
221 return GREATER_OF(Unix
, Mac
);
224 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
226 #endif // __WATCOMC__
234 bool wxTextFile::Read()
236 // file should be opened and we must be in it's beginning
237 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
240 char ch
, chLast
= '\0';
243 while ( !m_file
.Eof() ) {
244 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
245 if ( nRead
== wxInvalidOffset
) {
246 // read error (error message already given in wxFile::Read)
250 for ( n
= 0; n
< nRead
; n
++ ) {
254 // Dos/Unix line termination
256 m_aTypes
.Add(chLast
== '\r' ? wxTextFileType_Dos
257 : wxTextFileType_Unix
);
263 if ( chLast
== '\r' ) {
266 m_aTypes
.Add(wxTextFileType_Mac
);
273 if ( chLast
== '\r' ) {
274 // Mac line termination
276 m_aTypes
.Add(wxTextFileType_Mac
);
281 // add to the current line
288 // anything in the last line?
289 if ( !str
.IsEmpty() ) {
290 m_aTypes
.Add(wxTextFileType_None
); // no line terminator
297 bool wxTextFile::Close()
307 bool wxTextFile::Write(wxTextFileType typeNew
)
309 wxTempFile
fileTmp(m_strFile
);
311 if ( !fileTmp
.IsOpened() ) {
312 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
316 size_t nCount
= m_aLines
.Count();
317 for ( size_t n
= 0; n
< nCount
; n
++ ) {
318 fileTmp
.Write(m_aLines
[n
] +
319 GetEOL(typeNew
== wxTextFileType_None
? m_aTypes
[n
]
323 // replace the old file with this one
324 return fileTmp
.Commit();
327 #endif // wxUSE_TEXTFILE