]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textbuf.cpp
e77cf8be0d609e681df497a30c3ea31f4b4d6164
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textbuf.cpp
3 // Purpose: implementation of wxTextBuffer class
5 // Author: Morten Hanssen, Vadim Zeitlin
6 // Copyright: (c) 1998-2001 wxWindows 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()
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 bool wxTextBuffer::Exists() const
162 bool wxTextBuffer::Create(const wxString
& strBufferName
)
164 m_strBufferName
= strBufferName
;
169 bool wxTextBuffer::Create()
171 // buffer name must be either given in ctor or in Create(const wxString&)
172 wxASSERT( !m_strBufferName
.IsEmpty() );
174 // if the buffer already exists do nothing
175 if ( Exists() ) return FALSE
;
177 if ( !OnOpen(m_strBufferName
, WriteAccess
) )
184 bool wxTextBuffer::Open(const wxString
& strBufferName
, wxMBConv
& conv
)
186 m_strBufferName
= strBufferName
;
191 bool wxTextBuffer::Open(wxMBConv
& conv
)
193 // buffer name must be either given in ctor or in Open(const wxString&)
194 wxASSERT( !m_strBufferName
.IsEmpty() );
196 // open buffer in read-only mode
197 if ( !OnOpen(m_strBufferName
, ReadAccess
) )
200 // read buffer into memory
201 m_isOpened
= OnRead(conv
);
208 // analyse some lines of the buffer trying to guess it's type.
209 // if it fails, it assumes the native type for our platform.
210 wxTextFileType
wxTextBuffer::GuessType() const
212 wxASSERT( IsOpened() );
214 // scan the buffer lines
215 size_t nUnix
= 0, // number of '\n's alone
216 nDos
= 0, // number of '\r\n'
217 nMac
= 0; // number of '\r's
219 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
220 #define MAX_LINES_SCAN (10)
221 size_t nCount
= m_aLines
.Count() / 3,
222 nScan
= nCount
> 3*MAX_LINES_SCAN
? MAX_LINES_SCAN
: nCount
/ 3;
224 #define AnalyseLine(n) \
225 switch ( m_aTypes[n] ) { \
226 case wxTextFileType_Unix: nUnix++; break; \
227 case wxTextFileType_Dos: nDos++; break; \
228 case wxTextFileType_Mac: nMac++; break; \
229 default: wxFAIL_MSG(_("unknown line terminator")); \
233 for ( n
= 0; n
< nScan
; n
++ ) // the beginning
235 for ( n
= (nCount
- nScan
)/2; n
< (nCount
+ nScan
)/2; n
++ )
237 for ( n
= nCount
- nScan
; n
< nCount
; n
++ )
242 // interpret the results (FIXME far from being even 50% fool proof)
243 if ( nScan
> 0 && nDos
+ nUnix
+ nMac
== 0 ) {
244 // no newlines at all
245 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName
.c_str());
248 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
250 ? wxTextFileType_##t1 \
251 : wxTextFileType_##t2
253 // Watcom C++ doesn't seem to be able to handle the macro
254 // VS: Watcom 11 doesn't have a problem...
255 #if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
257 return GREATER_OF(Dos
, Mac
);
258 else if ( nDos
< nUnix
)
259 return GREATER_OF(Unix
, Mac
);
262 return nMac
> nDos
? wxTextFileType_Mac
: typeDefault
;
264 #endif // __WATCOMC__
273 bool wxTextBuffer::Close()
283 bool wxTextBuffer::Write(wxTextFileType typeNew
, wxMBConv
& conv
)
285 return OnWrite(typeNew
, conv
);
288 #endif // wxUSE_TEXTBUFFER