]>
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 wxTextBuffer::~wxTextBuffer()
151 // required here for Darwin
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 bool wxTextBuffer::Exists() const
163 bool wxTextBuffer::Create(const wxString
& strBufferName
)
165 m_strBufferName
= strBufferName
;
170 bool wxTextBuffer::Create()
172 // buffer name must be either given in ctor or in Create(const wxString&)
173 wxASSERT( !m_strBufferName
.IsEmpty() );
175 // if the buffer already exists do nothing
176 if ( Exists() ) return false;
178 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
185 bool wxTextBuffer::Open(const wxString
& strBufferName
, wxMBConv
& conv
)
187 m_strBufferName
= strBufferName
;
192 bool wxTextBuffer::Open(wxMBConv
& conv
)
194 // buffer name must be either given in ctor or in Open(const wxString&)
195 wxASSERT( !m_strBufferName
.IsEmpty() );
197 // open buffer in read-only mode
198 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
201 // read buffer into memory
202 m_isOpened
= OnRead(conv
);
209 // analyse some lines of the buffer trying to guess it's type.
210 // if it fails, it assumes the native type for our platform.
211 wxTextFileType
wxTextBuffer::GuessType() const
213 wxASSERT( IsOpened() );
215 // scan the buffer lines
216 size_t nUnix
= 0, // number of '\n's alone
217 nDos
= 0, // number of '\r\n'
218 nMac
= 0; // number of '\r's
220 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
221 #define MAX_LINES_SCAN (10)
222 size_t nCount
= m_aLines
.Count() / 3,
223 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
225 #define AnalyseLine(n) \
226 switch ( m_aTypes[n] ) { \
227 case wxTextFileType_Unix: nUnix++; break; \
228 case wxTextFileType_Dos: nDos++; break; \
229 case wxTextFileType_Mac: nMac++; break; \
230 default: wxFAIL_MSG(_("unknown line terminator")); \
234 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
236 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
238 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
243 // interpret the results (FIXME far from being even 50% fool proof)
244 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
245 // no newlines at all
246 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
249 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
251 ? wxTextFileType_##t1 \
252 : wxTextFileType_##t2
254 // Watcom C++ doesn't seem to be able to handle the macro
255 // VS: Watcom 11 doesn't have a problem...
256 #if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
258 return GREATER_OF(Dos
, Mac
);
259 else if ( nDos
< nUnix
)
260 return GREATER_OF(Unix
, Mac
);
263 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
265 #endif // __WATCOMC__
274 bool wxTextBuffer::Close()
284 bool wxTextBuffer::Write(wxTextFileType typeNew
, wxMBConv
& conv
)
286 return OnWrite(typeNew
, conv
);
289 #endif // wxUSE_TEXTBUFFER