]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textbuf.cpp
542e19db1e628d58c28739565732382299e7e7a0
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textbuf.cpp
3 // Purpose: implementation of wxTextBuffer class
5 // Author: Morten Hanssen, Vadim Zeitlin
7 // Copyright: (c) 1998-2001 wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 #include "wx/wxprec.h"
22 #include "wx/string.h"
27 #include "wx/textbuf.h"
29 // ============================================================================
30 // wxTextBuffer class implementation
31 // ============================================================================
33 // ----------------------------------------------------------------------------
34 // static methods (always compiled in)
35 // ----------------------------------------------------------------------------
37 // default type is the native one
38 // the native type under Mac OS X is:
39 // - Unix when compiling with the Apple Developer Tools (__UNIX__)
40 // - Mac when compiling with CodeWarrior (__WXMAC__)
42 const wxTextFileType
wxTextBuffer::typeDefault
=
43 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__PALMOS__)
45 #elif defined(__UNIX__)
47 #elif defined(__WXMAC__)
49 #elif defined(__OS2__)
53 #error "wxTextBuffer: unsupported platform."
56 const wxChar
*wxTextBuffer::GetEOL(wxTextFileType type
)
60 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
61 // fall through nevertheless - we must return something...
63 case wxTextFileType_None
: return wxEmptyString
;
64 case wxTextFileType_Unix
: return wxT("\n");
65 case wxTextFileType_Dos
: return wxT("\r\n");
66 case wxTextFileType_Mac
: return wxT("\r");
70 wxString
wxTextBuffer::Translate(const wxString
& text
, wxTextFileType type
)
72 // don't do anything if there is nothing to do
73 if ( type
== wxTextFileType_None
)
80 wxString eol
= GetEOL(type
), result
;
82 // optimization: we know that the length of the new string will be about
83 // the same as the length of the old one, so prealloc memory to avoid
84 // unnecessary relocations
85 result
.Alloc(text
.Len());
88 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
)
93 // Dos/Unix line termination
99 if ( chLast
== wxT('\r') ) {
104 // just remember it: we don't know whether it is just "\r"
111 if ( chLast
== wxT('\r') ) {
112 // Mac line termination
115 // reset chLast to avoid inserting another eol before the
120 // add to the current line
135 wxString
wxTextBuffer::ms_eof
;
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 wxTextBuffer::wxTextBuffer(const wxString
& strBufferName
)
142 : m_strBufferName(strBufferName
)
148 wxTextBuffer::~wxTextBuffer()
150 // required here for Darwin
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 bool wxTextBuffer::Exists() const
162 bool wxTextBuffer::Create(const wxString
& strBufferName
)
164 m_strBufferName
= strBufferName
;
169 bool wxTextBuffer::Create()
171 // buffer name must be either given in ctor or in Create(const wxString&)
172 wxASSERT( !m_strBufferName
.empty() );
174 // if the buffer already exists do nothing
175 if ( Exists() ) return false;
177 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
184 bool wxTextBuffer::Open(const wxString
& strBufferName
, const wxMBConv
& conv
)
186 m_strBufferName
= strBufferName
;
191 bool wxTextBuffer::Open(const wxMBConv
& conv
)
193 // buffer name must be either given in ctor or in Open(const wxString&)
194 wxASSERT( !m_strBufferName
.empty() );
196 // open buffer in read-only mode
197 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
200 // read buffer into memory
201 m_isOpened
= OnRead(conv
);
208 // analyse some lines of the buffer trying to guess it's type.
209 // if it fails, it assumes the native type for our platform.
210 wxTextFileType
wxTextBuffer::GuessType() const
212 wxASSERT( IsOpened() );
214 // scan the buffer lines
215 size_t nUnix
= 0, // number of '\n's alone
216 nDos
= 0, // number of '\r\n'
217 nMac
= 0; // number of '\r's
219 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
220 #define MAX_LINES_SCAN (10)
221 size_t nCount
= m_aLines
.GetCount() / 3,
222 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
224 #define AnalyseLine(n) \
225 switch ( m_aTypes[n] ) { \
226 case wxTextFileType_Unix: nUnix++; break; \
227 case wxTextFileType_Dos: nDos++; break; \
228 case wxTextFileType_Mac: nMac++; break; \
229 default: wxFAIL_MSG(wxT("unknown line terminator")); \
233 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
235 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
237 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
242 // interpret the results (FIXME far from being even 50% fool proof)
243 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
244 // no newlines at all
245 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
248 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
250 ? wxTextFileType_##t1 \
251 : wxTextFileType_##t2
253 #if !defined(__WATCOMC__) || wxCHECK_WATCOM_VERSION(1,4)
255 return GREATER_OF(Dos
, Mac
);
256 else if ( nDos
< nUnix
)
257 return GREATER_OF(Unix
, Mac
);
260 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
262 #endif // __WATCOMC__
271 bool wxTextBuffer::Close()
279 bool wxTextBuffer::Write(wxTextFileType typeNew
, const wxMBConv
& conv
)
281 return OnWrite(typeNew
, conv
);
284 #endif // wxUSE_TEXTBUFFER