]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma implementation "textbuf.h"
18 #include "wx/wxprec.h"
25 #include "wx/string.h"
30 #include "wx/textbuf.h"
32 // ============================================================================
33 // wxTextBuffer class implementation
34 // ============================================================================
36 // ----------------------------------------------------------------------------
37 // static methods (always compiled in)
38 // ----------------------------------------------------------------------------
40 // default type is the native one
41 // the native type under Mac OS X is:
42 // - Unix when compiling with the Apple Developer Tools (__UNIX__)
43 // - Mac when compiling with CodeWarrior (__WXMAC__)
45 const wxTextFileType
wxTextBuffer::typeDefault
=
46 #if defined(__WINDOWS__) || defined(__DOS__)
48 #elif defined(__UNIX__)
50 #elif defined(__WXMAC__)
52 #elif defined(__WXPM__)
56 #error "wxTextBuffer: unsupported platform."
59 const wxChar
*wxTextBuffer::GetEOL(wxTextFileType type
)
63 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
64 // fall through nevertheless - we must return something...
66 case wxTextFileType_None
: return wxT("");
67 case wxTextFileType_Unix
: return wxT("\n");
68 case wxTextFileType_Dos
: return wxT("\r\n");
69 case wxTextFileType_Mac
: return wxT("\r");
73 wxString
wxTextBuffer::Translate(const wxString
& text
, wxTextFileType type
)
75 // don't do anything if there is nothing to do
76 if ( type
== wxTextFileType_None
)
83 wxString eol
= GetEOL(type
), result
;
85 // optimization: we know that the length of the new string will be about
86 // the same as the length of the old one, so prealloc memory to aviod
87 // unnecessary relocations
88 result
.Alloc(text
.Len());
91 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
96 // Dos/Unix line termination
102 if ( chLast
== _T('\r') ) {
107 // just remember it: we don't know whether it is just "\r"
114 if ( chLast
== _T('\r') ) {
115 // Mac line termination
118 // reset chLast to avoid inserting another eol before the
123 // add to the current line
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 wxTextBuffer::wxTextBuffer(const wxString
& strBufferName
)
143 : m_strBufferName(strBufferName
)
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 bool wxTextBuffer::Exists() const
158 bool wxTextBuffer::Create(const wxString
& strBufferName
)
160 m_strBufferName
= strBufferName
;
165 bool wxTextBuffer::Create()
167 // buffer name must be either given in ctor or in Create(const wxString&)
168 wxASSERT( !m_strBufferName
.IsEmpty() );
170 // if the buffer already exists do nothing
171 if ( Exists() ) return false;
173 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
180 bool wxTextBuffer::Open(const wxString
& strBufferName
, wxMBConv
& conv
)
182 m_strBufferName
= strBufferName
;
187 bool wxTextBuffer::Open(wxMBConv
& conv
)
189 // buffer name must be either given in ctor or in Open(const wxString&)
190 wxASSERT( !m_strBufferName
.IsEmpty() );
192 // open buffer in read-only mode
193 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
196 // read buffer into memory
197 m_isOpened
= OnRead(conv
);
204 // analyse some lines of the buffer trying to guess it's type.
205 // if it fails, it assumes the native type for our platform.
206 wxTextFileType
wxTextBuffer::GuessType() const
208 wxASSERT( IsOpened() );
210 // scan the buffer lines
211 size_t nUnix
= 0, // number of '\n's alone
212 nDos
= 0, // number of '\r\n'
213 nMac
= 0; // number of '\r's
215 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
216 #define MAX_LINES_SCAN (10)
217 size_t nCount
= m_aLines
.Count() / 3,
218 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
220 #define AnalyseLine(n) \
221 switch ( m_aTypes[n] ) { \
222 case wxTextFileType_Unix: nUnix++; break; \
223 case wxTextFileType_Dos: nDos++; break; \
224 case wxTextFileType_Mac: nMac++; break; \
225 default: wxFAIL_MSG(_("unknown line terminator")); \
229 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
231 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
233 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
238 // interpret the results (FIXME far from being even 50% fool proof)
239 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
240 // no newlines at all
241 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
244 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
246 ? wxTextFileType_##t1 \
247 : wxTextFileType_##t2
249 // Watcom C++ doesn't seem to be able to handle the macro
250 // VS: Watcom 11 doesn't have a problem...
251 #if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
253 return GREATER_OF(Dos
, Mac
);
254 else if ( nDos
< nUnix
)
255 return GREATER_OF(Unix
, Mac
);
258 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
260 #endif // __WATCOMC__
269 bool wxTextBuffer::Close()
279 bool wxTextBuffer::Write(wxTextFileType typeNew
, wxMBConv
& conv
)
281 return OnWrite(typeNew
, conv
);
284 #endif // wxUSE_TEXTBUFFER