]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textbuf.cpp
f7ca5e081882883ade4d32f6835bcd209dd3eb20
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 aviod
84 // unnecessary relocations
85 result
.Alloc(text
.Len());
88 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
93 // Dos/Unix line termination
99 if ( chLast
== _T('\r') ) {
104 // just remember it: we don't know whether it is just "\r"
111 if ( chLast
== _T('\r') ) {
112 // Mac line termination
115 // reset chLast to avoid inserting another eol before the
120 // add to the current line
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 wxTextBuffer::wxTextBuffer(const wxString
& strBufferName
)
140 : m_strBufferName(strBufferName
)
146 wxTextBuffer::~wxTextBuffer()
148 // required here for Darwin
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 bool wxTextBuffer::Exists() const
160 bool wxTextBuffer::Create(const wxString
& strBufferName
)
162 m_strBufferName
= strBufferName
;
167 bool wxTextBuffer::Create()
169 // buffer name must be either given in ctor or in Create(const wxString&)
170 wxASSERT( !m_strBufferName
.empty() );
172 // if the buffer already exists do nothing
173 if ( Exists() ) return false;
175 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
182 bool wxTextBuffer::Open(const wxString
& strBufferName
, wxMBConv
& conv
)
184 m_strBufferName
= strBufferName
;
189 bool wxTextBuffer::Open(wxMBConv
& conv
)
191 // buffer name must be either given in ctor or in Open(const wxString&)
192 wxASSERT( !m_strBufferName
.empty() );
194 // open buffer in read-only mode
195 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
198 // read buffer into memory
199 m_isOpened
= OnRead(conv
);
206 // analyse some lines of the buffer trying to guess it's type.
207 // if it fails, it assumes the native type for our platform.
208 wxTextFileType
wxTextBuffer::GuessType() const
210 wxASSERT( IsOpened() );
212 // scan the buffer lines
213 size_t nUnix
= 0, // number of '\n's alone
214 nDos
= 0, // number of '\r\n'
215 nMac
= 0; // number of '\r's
217 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
218 #define MAX_LINES_SCAN (10)
219 size_t nCount
= m_aLines
.Count() / 3,
220 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
222 #define AnalyseLine(n) \
223 switch ( m_aTypes[n] ) { \
224 case wxTextFileType_Unix: nUnix++; break; \
225 case wxTextFileType_Dos: nDos++; break; \
226 case wxTextFileType_Mac: nMac++; break; \
227 default: wxFAIL_MSG(_("unknown line terminator")); \
231 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
233 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
235 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
240 // interpret the results (FIXME far from being even 50% fool proof)
241 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
242 // no newlines at all
243 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
246 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
248 ? wxTextFileType_##t1 \
249 : wxTextFileType_##t2
251 // Watcom C++ doesn't seem to be able to handle the macro
252 // VS: Watcom 11 doesn't have a problem...
253 #if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
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()
281 bool wxTextBuffer::Write(wxTextFileType typeNew
, wxMBConv
& conv
)
283 return OnWrite(typeNew
, conv
);
286 #endif // wxUSE_TEXTBUFFER