]>
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 // the native type under Mac OS X is:
50 // - Unix when compiling with the Apple Developer Tools (__UNIX__)
51 // - Mac when compiling with CodeWarrior (__WXMAC__)
53 const wxTextFileType
wxTextFile::typeDefault
=
54 #if defined(__WINDOWS__)
56 #elif defined(__UNIX__)
58 #elif defined(__WXMAC__)
60 #elif defined(__WXPM__)
64 #error "wxTextFile: unsupported platform."
67 const wxChar
*wxTextFile::GetEOL(wxTextFileType type
)
71 wxFAIL_MSG(wxT("bad file type in wxTextFile::GetEOL."));
72 // fall through nevertheless - we must return something...
74 case wxTextFileType_None
: return wxT("");
75 case wxTextFileType_Unix
: return wxT("\n");
76 case wxTextFileType_Dos
: return wxT("\r\n");
77 case wxTextFileType_Mac
: return wxT("\r");
81 wxString
wxTextFile::Translate(const wxString
& text
, wxTextFileType type
)
83 // don't do anything if there is nothing to do
84 if ( type
== wxTextFileType_None
)
87 // GRG: don't do anything either if it is empty
91 wxString eol
= GetEOL(type
), result
;
93 // optimization: we know that the length of the new string will be about
94 // the same as the length of the old one, so prealloc memory to aviod
95 // unnecessary relocations
96 result
.Alloc(text
.Len());
99 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
104 // Dos/Unix line termination
110 if ( chLast
== _T('\r') ) {
115 // just remember it: we don't know whether it is just "\r"
122 if ( chLast
== _T('\r') ) {
123 // Mac line termination
126 // reset chLast to avoid inserting another eol before the
131 // add to the current line
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
156 wxTextFile::~wxTextFile()
158 // m_file dtor called automatically
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 bool wxTextFile::Exists() const
167 return wxFile::Exists(m_strFile
);
170 bool wxTextFile::Create(const wxString
& strFile
)
177 bool wxTextFile::Create()
179 // file name must be either given in ctor or in Create(const wxString&)
180 wxASSERT( !m_strFile
.IsEmpty() );
182 // if the file already exists do nothing
183 if ( Exists() ) return FALSE
;
185 if ( m_file
.Open(m_strFile
, wxFile::write
) )
196 bool wxTextFile::Open(const wxString
& strFile
, wxMBConv
& conv
)
203 bool wxTextFile::Open(wxMBConv
& conv
)
205 // file name must be either given in ctor or in Open(const wxString&)
206 wxASSERT( !m_strFile
.IsEmpty() );
208 // open file in read-only mode
209 if ( !m_file
.Open(m_strFile
) )
212 // read file into memory
213 m_isOpened
= Read(conv
);
220 // analyse some lines of the file trying to guess it's type.
221 // if it fails, it assumes the native type for our platform.
222 wxTextFileType
wxTextFile::GuessType() const
224 wxASSERT( IsOpened() );
226 // scan the file lines
227 size_t nUnix
= 0, // number of '\n's alone
228 nDos
= 0, // number of '\r\n'
229 nMac
= 0; // number of '\r's
231 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
232 #define MAX_LINES_SCAN (10)
233 size_t nCount
= m_aLines
.Count() / 3,
234 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
236 #define AnalyseLine(n) \
237 switch ( m_aTypes[n] ) { \
238 case wxTextFileType_Unix: nUnix++; break; \
239 case wxTextFileType_Dos: nDos++; break; \
240 case wxTextFileType_Mac: nMac++; break; \
241 default: wxFAIL_MSG(_("unknown line terminator")); \
245 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
247 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
249 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
254 // interpret the results (FIXME far from being even 50% fool proof)
255 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
256 // no newlines at all
257 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
260 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
262 ? wxTextFileType_##t1 \
263 : wxTextFileType_##t2
265 // Watcom C++ doesn't seem to be able to handle the macro
266 #if !defined(__WATCOMC__)
268 return GREATER_OF(Dos
, Mac
);
269 else if ( nDos
< nUnix
)
270 return GREATER_OF(Unix
, Mac
);
273 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
275 #endif // __WATCOMC__
283 bool wxTextFile::Read(wxMBConv
& conv
)
285 // file should be opened and we must be in it's beginning
286 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
290 wchar_t conv_wcBuf
[2];
297 char ch
, chLast
= '\0';
301 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
302 if ( nRead
== wxInvalidOffset
) {
303 // read error (error message already given in wxFile::Read)
307 for ( n
= 0; n
< nRead
; n
++ ) {
311 // Dos/Unix line termination
313 m_aTypes
.Add(chLast
== '\r' ? wxTextFileType_Dos
314 : wxTextFileType_Unix
);
320 if ( chLast
== '\r' ) {
322 m_aLines
.Add(wxEmptyString
);
323 m_aTypes
.Add(wxTextFileType_Mac
);
330 if ( chLast
== '\r' ) {
331 // Mac line termination
333 m_aTypes
.Add(wxTextFileType_Mac
);
336 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
344 // add to the current line
346 if (conv
.MB2WC(conv_wcBuf
, conv_mbBuf
, 2) == (size_t)-1)
348 str
+= conv_wcBuf
[0];
355 } while ( nRead
== WXSIZEOF(buf
) );
357 // anything in the last line?
358 if ( !str
.IsEmpty() ) {
359 m_aTypes
.Add(wxTextFileType_None
); // no line terminator
366 bool wxTextFile::Close()
376 bool wxTextFile::Write(wxTextFileType typeNew
, wxMBConv
& conv
)
378 wxTempFile
fileTmp(m_strFile
);
380 if ( !fileTmp
.IsOpened() ) {
381 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
385 size_t nCount
= m_aLines
.Count();
386 for ( size_t n
= 0; n
< nCount
; n
++ ) {
387 fileTmp
.Write(m_aLines
[n
] +
388 GetEOL(typeNew
== wxTextFileType_None
? m_aTypes
[n
]
392 // replace the old file with this one
393 return fileTmp
.Commit();
396 #endif // wxUSE_TEXTFILE