]>
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
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
39 const wxTextFileType
wxTextBuffer::typeDefault
=
40 #if defined(__WINDOWS__) || defined(__DOS__)
42 #elif defined(__UNIX__)
44 #elif defined(__OS2__)
48 #error "wxTextBuffer: unsupported platform."
51 const wxChar
*wxTextBuffer::GetEOL(wxTextFileType type
)
55 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
56 // fall through nevertheless - we must return something...
58 case wxTextFileType_None
: return wxEmptyString
;
59 case wxTextFileType_Unix
: return wxT("\n");
60 case wxTextFileType_Dos
: return wxT("\r\n");
61 case wxTextFileType_Mac
: return wxT("\r");
65 wxString
wxTextBuffer::Translate(const wxString
& text
, wxTextFileType type
)
67 // don't do anything if there is nothing to do
68 if ( type
== wxTextFileType_None
)
75 wxString eol
= GetEOL(type
), result
;
77 // optimization: we know that the length of the new string will be about
78 // the same as the length of the old one, so prealloc memory to avoid
79 // unnecessary relocations
80 result
.Alloc(text
.Len());
83 for ( wxString::const_iterator i
= text
.begin(); i
!= text
.end(); ++i
)
88 // Dos/Unix line termination
94 if ( chLast
== wxT('\r') ) {
99 // just remember it: we don't know whether it is just "\r"
106 if ( chLast
== wxT('\r') ) {
107 // Mac line termination
110 // reset chLast to avoid inserting another eol before the
115 // add to the current line
130 wxString
wxTextBuffer::ms_eof
;
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxTextBuffer::wxTextBuffer(const wxString
& strBufferName
)
137 : m_strBufferName(strBufferName
)
143 wxTextBuffer::~wxTextBuffer()
145 // required here for Darwin
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 bool wxTextBuffer::Exists() const
157 bool wxTextBuffer::Create(const wxString
& strBufferName
)
159 m_strBufferName
= strBufferName
;
164 bool wxTextBuffer::Create()
166 // buffer name must be either given in ctor or in Create(const wxString&)
167 wxASSERT( !m_strBufferName
.empty() );
169 // if the buffer already exists do nothing
170 if ( Exists() ) return false;
172 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
179 bool wxTextBuffer::Open(const wxString
& strBufferName
, const wxMBConv
& conv
)
181 m_strBufferName
= strBufferName
;
186 bool wxTextBuffer::Open(const wxMBConv
& conv
)
188 // buffer name must be either given in ctor or in Open(const wxString&)
189 wxASSERT( !m_strBufferName
.empty() );
191 // open buffer in read-only mode
192 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
195 // read buffer into memory
196 m_isOpened
= OnRead(conv
);
203 // analyse some lines of the buffer trying to guess it's type.
204 // if it fails, it assumes the native type for our platform.
205 wxTextFileType
wxTextBuffer::GuessType() const
207 wxASSERT( IsOpened() );
209 // scan the buffer lines
210 size_t nUnix
= 0, // number of '\n's alone
211 nDos
= 0, // number of '\r\n'
212 nMac
= 0; // number of '\r's
214 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
215 #define MAX_LINES_SCAN (10)
216 size_t nCount
= m_aLines
.GetCount() / 3,
217 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
219 #define AnalyseLine(n) \
220 switch ( m_aTypes[n] ) { \
221 case wxTextFileType_Unix: nUnix++; break; \
222 case wxTextFileType_Dos: nDos++; break; \
223 case wxTextFileType_Mac: nMac++; break; \
224 default: wxFAIL_MSG(wxT("unknown line terminator")); \
228 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
230 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
232 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
237 // interpret the results (FIXME far from being even 50% fool proof)
238 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
239 // no newlines at all
240 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
243 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
245 ? wxTextFileType_##t1 \
246 : wxTextFileType_##t2
248 #if !defined(__WATCOMC__) || wxCHECK_WATCOM_VERSION(1,4)
250 return GREATER_OF(Dos
, Mac
);
251 else if ( nDos
< nUnix
)
252 return GREATER_OF(Unix
, Mac
);
255 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
257 #endif // __WATCOMC__
266 bool wxTextBuffer::Close()
274 bool wxTextBuffer::Write(wxTextFileType typeNew
, const wxMBConv
& conv
)
276 return OnWrite(typeNew
, conv
);
279 #endif // wxUSE_TEXTBUFFER