]>
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 #include "wx/wxprec.h"
21 #include "wx/string.h"
26 #include "wx/textbuf.h"
28 // ============================================================================
29 // wxTextBuffer class implementation
30 // ============================================================================
32 // ----------------------------------------------------------------------------
33 // static methods (always compiled in)
34 // ----------------------------------------------------------------------------
36 // default type is the native one
37 // the native type under Mac OS X is:
38 // - Unix when compiling with the Apple Developer Tools (__UNIX__)
39 // - Mac when compiling with CodeWarrior (__WXMAC__)
41 const wxTextFileType
wxTextBuffer::typeDefault
=
42 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__PALMOS__)
44 #elif defined(__UNIX__)
46 #elif defined(__WXMAC__)
48 #elif defined(__WXPM__)
52 #error "wxTextBuffer: unsupported platform."
55 const wxChar
*wxTextBuffer::GetEOL(wxTextFileType type
)
59 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
60 // fall through nevertheless - we must return something...
62 case wxTextFileType_None
: return wxEmptyString
;
63 case wxTextFileType_Unix
: return wxT("\n");
64 case wxTextFileType_Dos
: return wxT("\r\n");
65 case wxTextFileType_Mac
: return wxT("\r");
69 wxString
wxTextBuffer::Translate(const wxString
& text
, wxTextFileType type
)
71 // don't do anything if there is nothing to do
72 if ( type
== wxTextFileType_None
)
79 wxString eol
= GetEOL(type
), result
;
81 // optimization: we know that the length of the new string will be about
82 // the same as the length of the old one, so prealloc memory to aviod
83 // unnecessary relocations
84 result
.Alloc(text
.Len());
87 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
92 // Dos/Unix line termination
98 if ( chLast
== _T('\r') ) {
103 // just remember it: we don't know whether it is just "\r"
110 if ( chLast
== _T('\r') ) {
111 // Mac line termination
114 // reset chLast to avoid inserting another eol before the
119 // add to the current line
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxTextBuffer::wxTextBuffer(const wxString
& strBufferName
)
139 : m_strBufferName(strBufferName
)
145 wxTextBuffer::~wxTextBuffer()
147 // required here for Darwin
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 bool wxTextBuffer::Exists() const
159 bool wxTextBuffer::Create(const wxString
& strBufferName
)
161 m_strBufferName
= strBufferName
;
166 bool wxTextBuffer::Create()
168 // buffer name must be either given in ctor or in Create(const wxString&)
169 wxASSERT( !m_strBufferName
.empty() );
171 // if the buffer already exists do nothing
172 if ( Exists() ) return false;
174 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
181 bool wxTextBuffer::Open(const wxString
& strBufferName
, wxMBConv
& conv
)
183 m_strBufferName
= strBufferName
;
188 bool wxTextBuffer::Open(wxMBConv
& conv
)
190 // buffer name must be either given in ctor or in Open(const wxString&)
191 wxASSERT( !m_strBufferName
.empty() );
193 // open buffer in read-only mode
194 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
197 // read buffer into memory
198 m_isOpened
= OnRead(conv
);
205 // analyse some lines of the buffer trying to guess it's type.
206 // if it fails, it assumes the native type for our platform.
207 wxTextFileType
wxTextBuffer::GuessType() const
209 wxASSERT( IsOpened() );
211 // scan the buffer lines
212 size_t nUnix
= 0, // number of '\n's alone
213 nDos
= 0, // number of '\r\n'
214 nMac
= 0; // number of '\r's
216 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
217 #define MAX_LINES_SCAN (10)
218 size_t nCount
= m_aLines
.Count() / 3,
219 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
221 #define AnalyseLine(n) \
222 switch ( m_aTypes[n] ) { \
223 case wxTextFileType_Unix: nUnix++; break; \
224 case wxTextFileType_Dos: nDos++; break; \
225 case wxTextFileType_Mac: nMac++; break; \
226 default: wxFAIL_MSG(_("unknown line terminator")); \
230 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
232 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
234 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
239 // interpret the results (FIXME far from being even 50% fool proof)
240 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
241 // no newlines at all
242 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
245 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
247 ? wxTextFileType_##t1 \
248 : wxTextFileType_##t2
250 // Watcom C++ doesn't seem to be able to handle the macro
251 // VS: Watcom 11 doesn't have a problem...
252 #if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
254 return GREATER_OF(Dos
, Mac
);
255 else if ( nDos
< nUnix
)
256 return GREATER_OF(Unix
, Mac
);
259 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
261 #endif // __WATCOMC__
270 bool wxTextBuffer::Close()
280 bool wxTextBuffer::Write(wxTextFileType typeNew
, wxMBConv
& conv
)
282 return OnWrite(typeNew
, conv
);
285 #endif // wxUSE_TEXTBUFFER