]>
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");
78 wxString
wxTextFile::Translate(const wxString
& text
, wxTextFileType type
)
80 // don't do anything if there is nothing to do
81 if ( type
== wxTextFileType_None
)
84 wxString eol
= GetEOL(type
), result
;
86 // optimization: we know that the length of the new string will be about
87 // the same as the length of the old one, so prealloc memory to aviod
88 // unnecessary relocations
89 result
.Alloc(text
.Len());
92 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
97 // Dos/Unix line termination
103 if ( chLast
== '\r' ) {
112 if ( chLast
== '\r' ) {
113 // Mac line termination
118 // add to the current line
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 wxTextFile::wxTextFile(const wxString
& strFile
) : m_strFile(strFile
)
139 wxTextFile::~wxTextFile()
141 // m_file dtor called automatically
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 bool wxTextFile::Exists() const
150 return wxFile::Exists(m_strFile
);
153 bool wxTextFile::Open(const wxString
& strFile
)
160 bool wxTextFile::Open()
162 // file name must be either given in ctor or in Open(const wxString&)
163 wxASSERT( !m_strFile
.IsEmpty() );
165 // open file in read-only mode
166 if ( !m_file
.Open(m_strFile
) )
169 // read file into memory
177 // analyse some lines of the file trying to guess it's type.
178 // if it fails, it assumes the native type for our platform.
179 wxTextFileType
wxTextFile::GuessType() const
181 // file should be opened and we must be in it's beginning
182 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
184 // scan the file lines
185 size_t nUnix
= 0, // number of '\n's alone
186 nDos
= 0, // number of '\r\n'
187 nMac
= 0; // number of '\r's
189 // we take MAX_LINES_SCAN in the beginning, middle and the end of file
190 #define MAX_LINES_SCAN (10)
191 size_t nCount
= m_aLines
.Count() / 3,
192 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
194 #define AnalyseLine(n) \
195 switch ( m_aTypes[n] ) { \
196 case wxTextFileType_Unix: nUnix++; break; \
197 case wxTextFileType_Dos: nDos++; break; \
198 case wxTextFileType_Mac: nMac++; break; \
199 default: wxFAIL_MSG(_("unknown line terminator")); \
203 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
205 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
207 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
212 // interpret the results (FIXME far from being even 50% fool proof)
213 if ( nDos
+ nUnix
+ nMac
== 0 ) {
214 // no newlines at all
215 wxLogWarning(_("'%s' is probably a binary file."), m_strFile
.c_str());
218 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
220 ? wxTextFileType_##t1 \
221 : wxTextFileType_##t2
223 // Watcom C++ doesn't seem to be able to handle the macro
224 #if !defined(__WATCOMC__)
226 return GREATER_OF(Dos
, Mac
);
227 else if ( nDos
< nUnix
)
228 return GREATER_OF(Unix
, Mac
);
231 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
233 #endif // __WATCOMC__
241 bool wxTextFile::Read()
243 // file should be opened and we must be in it's beginning
244 wxASSERT( m_file
.IsOpened() && m_file
.Tell() == 0 );
247 char ch
, chLast
= '\0';
250 while ( !m_file
.Eof() ) {
251 nRead
= m_file
.Read(buf
, WXSIZEOF(buf
));
252 if ( nRead
== wxInvalidOffset
) {
253 // read error (error message already given in wxFile::Read)
257 for ( n
= 0; n
< nRead
; n
++ ) {
261 // Dos/Unix line termination
263 m_aTypes
.Add(chLast
== '\r' ? wxTextFileType_Dos
264 : wxTextFileType_Unix
);
270 if ( chLast
== '\r' ) {
273 m_aTypes
.Add(wxTextFileType_Mac
);
280 if ( chLast
== '\r' ) {
281 // Mac line termination
283 m_aTypes
.Add(wxTextFileType_Mac
);
288 // add to the current line
295 // anything in the last line?
296 if ( !str
.IsEmpty() ) {
297 m_aTypes
.Add(wxTextFileType_None
); // no line terminator
304 bool wxTextFile::Close()
314 bool wxTextFile::Write(wxTextFileType typeNew
)
316 wxTempFile
fileTmp(m_strFile
);
318 if ( !fileTmp
.IsOpened() ) {
319 wxLogError(_("can't write file '%s' to disk."), m_strFile
.c_str());
323 size_t nCount
= m_aLines
.Count();
324 for ( size_t n
= 0; n
< nCount
; n
++ ) {
325 fileTmp
.Write(m_aLines
[n
] +
326 GetEOL(typeNew
== wxTextFileType_None
? m_aTypes
[n
]
330 // replace the old file with this one
331 return fileTmp
.Commit();
334 #endif // wxUSE_TEXTFILE