]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textbuf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textbuf.cpp
3 // Purpose: implementation of wxTextBuffer class
5 // Author: Morten Hanssen, Vadim Zeitlin
6 // Copyright: (c) 1998-2001 wxWidgets team
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 #include "wx/wxprec.h"
21 #include "wx/string.h"
26 #include "wx/textbuf.h"
28 // ============================================================================
29 // wxTextBuffer class implementation
30 // ============================================================================
32 // ----------------------------------------------------------------------------
33 // static methods (always compiled in)
34 // ----------------------------------------------------------------------------
36 // default type is the native one
38 const wxTextFileType
wxTextBuffer::typeDefault
=
39 #if defined(__WINDOWS__) || defined(__DOS__)
41 #elif defined(__UNIX__)
43 #elif defined(__OS2__)
47 #error "wxTextBuffer: unsupported platform."
50 const wxChar
*wxTextBuffer::GetEOL(wxTextFileType type
)
54 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
55 // fall through nevertheless - we must return something...
57 case wxTextFileType_None
: return wxEmptyString
;
58 case wxTextFileType_Unix
: return wxT("\n");
59 case wxTextFileType_Dos
: return wxT("\r\n");
60 case wxTextFileType_Mac
: return wxT("\r");
64 wxString
wxTextBuffer::Translate(const wxString
& text
, wxTextFileType type
)
66 // don't do anything if there is nothing to do
67 if ( type
== wxTextFileType_None
)
74 wxString eol
= GetEOL(type
), result
;
76 // optimization: we know that the length of the new string will be about
77 // the same as the length of the old one, so prealloc memory to avoid
78 // unnecessary relocations
79 result
.Alloc(text
.Len());
82 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
)
87 // Dos/Unix line termination
93 if ( chLast
== wxT('\r') ) {
98 // just remember it: we don't know whether it is just "\r"
105 if ( chLast
== wxT('\r') ) {
106 // Mac line termination
109 // reset chLast to avoid inserting another eol before the
114 // add to the current line
129 wxString
wxTextBuffer::ms_eof
;
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 wxTextBuffer::wxTextBuffer(const wxString
& strBufferName
)
136 : m_strBufferName(strBufferName
)
142 wxTextBuffer::~wxTextBuffer()
144 // required here for Darwin
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 bool wxTextBuffer::Exists() const
156 bool wxTextBuffer::Create(const wxString
& strBufferName
)
158 m_strBufferName
= strBufferName
;
163 bool wxTextBuffer::Create()
165 // buffer name must be either given in ctor or in Create(const wxString&)
166 wxASSERT( !m_strBufferName
.empty() );
168 // if the buffer already exists do nothing
169 if ( Exists() ) return false;
171 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
178 bool wxTextBuffer::Open(const wxString
& strBufferName
, const wxMBConv
& conv
)
180 m_strBufferName
= strBufferName
;
185 bool wxTextBuffer::Open(const wxMBConv
& conv
)
187 // buffer name must be either given in ctor or in Open(const wxString&)
188 wxASSERT( !m_strBufferName
.empty() );
190 // open buffer in read-only mode
191 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
194 // read buffer into memory
195 m_isOpened
= OnRead(conv
);
202 // analyse some lines of the buffer trying to guess it's type.
203 // if it fails, it assumes the native type for our platform.
204 wxTextFileType
wxTextBuffer::GuessType() const
206 wxASSERT( IsOpened() );
208 // scan the buffer lines
209 size_t nUnix
= 0, // number of '\n's alone
210 nDos
= 0, // number of '\r\n'
211 nMac
= 0; // number of '\r's
213 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
214 #define MAX_LINES_SCAN (10)
215 size_t nCount
= m_aLines
.GetCount() / 3,
216 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
218 #define AnalyseLine(n) \
219 switch ( m_aTypes[n] ) { \
220 case wxTextFileType_Unix: nUnix++; break; \
221 case wxTextFileType_Dos: nDos++; break; \
222 case wxTextFileType_Mac: nMac++; break; \
223 default: wxFAIL_MSG(wxT("unknown line terminator")); \
227 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
229 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
231 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
236 // interpret the results (FIXME far from being even 50% fool proof)
237 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
238 // no newlines at all
239 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
242 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
244 ? wxTextFileType_##t1 \
245 : wxTextFileType_##t2
247 #if !defined(__WATCOMC__) || wxCHECK_WATCOM_VERSION(1,4)
249 return GREATER_OF(Dos
, Mac
);
250 else if ( nDos
< nUnix
)
251 return GREATER_OF(Unix
, Mac
);
254 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
256 #endif // __WATCOMC__
265 bool wxTextBuffer::Close()
273 bool wxTextBuffer::Write(wxTextFileType typeNew
, const wxMBConv
& conv
)
275 return OnWrite(typeNew
, conv
);
278 #endif // wxUSE_TEXTBUFFER