]> git.saurik.com Git - wxWidgets.git/blame - src/common/textbuf.cpp
added possibility to throw different objects and crash directly from the main frame...
[wxWidgets.git] / src / common / textbuf.cpp
CommitLineData
a3a584a7
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/textbuf.cpp
3// Purpose: implementation of wxTextBuffer class
4// Created: 14.11.01
5// Author: Morten Hanssen, Vadim Zeitlin
77ffb593 6// Copyright: (c) 1998-2001 wxWidgets team
65571936 7// Licence: wxWindows licence
a3a584a7
VZ
8///////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// headers
12// ============================================================================
13
14f355c2 14#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3a584a7
VZ
15 #pragma implementation "textbuf.h"
16#endif
17
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21 #pragma hdrstop
22#endif //__BORLANDC__
23
24#ifndef WX_PRECOMP
25 #include "wx/string.h"
26 #include "wx/intl.h"
27 #include "wx/log.h"
28#endif
29
30#include "wx/textbuf.h"
31
32// ============================================================================
33// wxTextBuffer class implementation
34// ============================================================================
35
36// ----------------------------------------------------------------------------
37// static methods (always compiled in)
38// ----------------------------------------------------------------------------
39
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__)
44
45const wxTextFileType wxTextBuffer::typeDefault =
4055ed82 46#if defined(__WINDOWS__) || defined(__DOS__) || defined(__PALMOS__)
a3a584a7
VZ
47 wxTextFileType_Dos;
48#elif defined(__UNIX__)
49 wxTextFileType_Unix;
50#elif defined(__WXMAC__)
51 wxTextFileType_Mac;
52#elif defined(__WXPM__)
53 wxTextFileType_Os2;
54#else
55 wxTextFileType_None;
56 #error "wxTextBuffer: unsupported platform."
57#endif
58
59const wxChar *wxTextBuffer::GetEOL(wxTextFileType type)
60{
61 switch ( type ) {
62 default:
63 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
64 // fall through nevertheless - we must return something...
65
525d8583 66 case wxTextFileType_None: return wxEmptyString;
a3a584a7
VZ
67 case wxTextFileType_Unix: return wxT("\n");
68 case wxTextFileType_Dos: return wxT("\r\n");
69 case wxTextFileType_Mac: return wxT("\r");
70 }
71}
72
73wxString wxTextBuffer::Translate(const wxString& text, wxTextFileType type)
74{
75 // don't do anything if there is nothing to do
76 if ( type == wxTextFileType_None )
77 return text;
78
79 // nor if it is empty
4055ed82 80 if ( text.empty() )
a3a584a7
VZ
81 return text;
82
83 wxString eol = GetEOL(type), result;
84
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());
89
90 wxChar chLast = 0;
91 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
92 {
93 wxChar ch = *pc;
94 switch ( ch ) {
95 case _T('\n'):
96 // Dos/Unix line termination
97 result += eol;
98 chLast = 0;
99 break;
100
101 case _T('\r'):
102 if ( chLast == _T('\r') ) {
103 // Mac empty line
104 result += eol;
105 }
106 else {
107 // just remember it: we don't know whether it is just "\r"
108 // or "\r\n" yet
109 chLast = _T('\r');
110 }
111 break;
112
113 default:
114 if ( chLast == _T('\r') ) {
115 // Mac line termination
116 result += eol;
117
118 // reset chLast to avoid inserting another eol before the
119 // next character
120 chLast = 0;
121 }
122
123 // add to the current line
124 result += ch;
125 }
126 }
127
128 if ( chLast ) {
129 // trailing '\r'
130 result += eol;
131 }
132
133 return result;
134}
135
136#if wxUSE_TEXTBUFFER
137
138// ----------------------------------------------------------------------------
139// ctors & dtor
140// ----------------------------------------------------------------------------
141
142wxTextBuffer::wxTextBuffer(const wxString& strBufferName)
143 : m_strBufferName(strBufferName)
144{
145 m_nCurLine = 0;
cb719f2e 146 m_isOpened = false;
a3a584a7
VZ
147}
148
5e233068
WS
149wxTextBuffer::~wxTextBuffer()
150{
151 // required here for Darwin
152}
153
a3a584a7
VZ
154// ----------------------------------------------------------------------------
155// buffer operations
156// ----------------------------------------------------------------------------
157
158bool wxTextBuffer::Exists() const
159{
160 return OnExists();
161}
162
163bool wxTextBuffer::Create(const wxString& strBufferName)
164{
165 m_strBufferName = strBufferName;
166
167 return Create();
168}
169
170bool wxTextBuffer::Create()
171{
172 // buffer name must be either given in ctor or in Create(const wxString&)
4055ed82 173 wxASSERT( !m_strBufferName.empty() );
a3a584a7
VZ
174
175 // if the buffer already exists do nothing
cb719f2e
WS
176 if ( Exists() ) return false;
177
a3a584a7 178 if ( !OnOpen(m_strBufferName, WriteAccess) )
cb719f2e 179 return false;
a3a584a7
VZ
180
181 OnClose();
cb719f2e 182 return true;
a3a584a7
VZ
183}
184
185bool wxTextBuffer::Open(const wxString& strBufferName, wxMBConv& conv)
186{
187 m_strBufferName = strBufferName;
188
189 return Open(conv);
190}
191
192bool wxTextBuffer::Open(wxMBConv& conv)
193{
194 // buffer name must be either given in ctor or in Open(const wxString&)
4055ed82 195 wxASSERT( !m_strBufferName.empty() );
a3a584a7
VZ
196
197 // open buffer in read-only mode
198 if ( !OnOpen(m_strBufferName, ReadAccess) )
cb719f2e 199 return false;
a3a584a7
VZ
200
201 // read buffer into memory
202 m_isOpened = OnRead(conv);
203
204 OnClose();
205
206 return m_isOpened;
207}
208
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.
211wxTextFileType wxTextBuffer::GuessType() const
212{
213 wxASSERT( IsOpened() );
214
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
219
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;
224
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")); \
231 }
232
233 size_t n;
234 for ( n = 0; n < nScan; n++ ) // the beginning
235 AnalyseLine(n);
236 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
237 AnalyseLine(n);
238 for ( n = nCount - nScan; n < nCount; n++ )
239 AnalyseLine(n);
240
241 #undef AnalyseLine
242
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());
247 }
248 else {
249 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
250 : n##t1 > n##t2 \
251 ? wxTextFileType_##t1 \
252 : wxTextFileType_##t2
253
254 // Watcom C++ doesn't seem to be able to handle the macro
c5124240
VS
255 // VS: Watcom 11 doesn't have a problem...
256#if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
a3a584a7
VZ
257 if ( nDos > nUnix )
258 return GREATER_OF(Dos, Mac);
259 else if ( nDos < nUnix )
260 return GREATER_OF(Unix, Mac);
261 else {
262 // nDos == nUnix
263 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
264 }
265#endif // __WATCOMC__
266
267 #undef GREATER_OF
268 }
269
270 return typeDefault;
271}
272
273
274bool wxTextBuffer::Close()
275{
276 m_aTypes.Clear();
277 m_aLines.Clear();
278 m_nCurLine = 0;
cb719f2e 279 m_isOpened = false;
a3a584a7 280
cb719f2e 281 return true;
a3a584a7
VZ
282}
283
284bool wxTextBuffer::Write(wxTextFileType typeNew, wxMBConv& conv)
285{
286 return OnWrite(typeNew, conv);
287}
288
289#endif // wxUSE_TEXTBUFFER
290