]> git.saurik.com Git - wxWidgets.git/blob - src/common/textbuf.cpp
Reverted due to Darwin conflict.
[wxWidgets.git] / src / common / textbuf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textbuf.cpp
3 // Purpose: implementation of wxTextBuffer class
4 // Created: 14.11.01
5 // Author: Morten Hanssen, Vadim Zeitlin
6 // Copyright: (c) 1998-2001 wxWidgets team
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // headers
12 // ============================================================================
13
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
45 const wxTextFileType wxTextBuffer::typeDefault =
46 #if defined(__WINDOWS__) || defined(__DOS__)
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
59 const 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
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");
70 }
71 }
72
73 wxString 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
80 if ( text.IsEmpty() )
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
142 wxTextBuffer::wxTextBuffer(const wxString& strBufferName)
143 : m_strBufferName(strBufferName)
144 {
145 m_nCurLine = 0;
146 m_isOpened = false;
147 }
148
149 // ----------------------------------------------------------------------------
150 // buffer operations
151 // ----------------------------------------------------------------------------
152
153 bool wxTextBuffer::Exists() const
154 {
155 return OnExists();
156 }
157
158 bool wxTextBuffer::Create(const wxString& strBufferName)
159 {
160 m_strBufferName = strBufferName;
161
162 return Create();
163 }
164
165 bool wxTextBuffer::Create()
166 {
167 // buffer name must be either given in ctor or in Create(const wxString&)
168 wxASSERT( !m_strBufferName.IsEmpty() );
169
170 // if the buffer already exists do nothing
171 if ( Exists() ) return false;
172
173 if ( !OnOpen(m_strBufferName, WriteAccess) )
174 return false;
175
176 OnClose();
177 return true;
178 }
179
180 bool wxTextBuffer::Open(const wxString& strBufferName, wxMBConv& conv)
181 {
182 m_strBufferName = strBufferName;
183
184 return Open(conv);
185 }
186
187 bool wxTextBuffer::Open(wxMBConv& conv)
188 {
189 // buffer name must be either given in ctor or in Open(const wxString&)
190 wxASSERT( !m_strBufferName.IsEmpty() );
191
192 // open buffer in read-only mode
193 if ( !OnOpen(m_strBufferName, ReadAccess) )
194 return false;
195
196 // read buffer into memory
197 m_isOpened = OnRead(conv);
198
199 OnClose();
200
201 return m_isOpened;
202 }
203
204 // analyse some lines of the buffer trying to guess it's type.
205 // if it fails, it assumes the native type for our platform.
206 wxTextFileType wxTextBuffer::GuessType() const
207 {
208 wxASSERT( IsOpened() );
209
210 // scan the buffer lines
211 size_t nUnix = 0, // number of '\n's alone
212 nDos = 0, // number of '\r\n'
213 nMac = 0; // number of '\r's
214
215 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
216 #define MAX_LINES_SCAN (10)
217 size_t nCount = m_aLines.Count() / 3,
218 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
219
220 #define AnalyseLine(n) \
221 switch ( m_aTypes[n] ) { \
222 case wxTextFileType_Unix: nUnix++; break; \
223 case wxTextFileType_Dos: nDos++; break; \
224 case wxTextFileType_Mac: nMac++; break; \
225 default: wxFAIL_MSG(_("unknown line terminator")); \
226 }
227
228 size_t n;
229 for ( n = 0; n < nScan; n++ ) // the beginning
230 AnalyseLine(n);
231 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
232 AnalyseLine(n);
233 for ( n = nCount - nScan; n < nCount; n++ )
234 AnalyseLine(n);
235
236 #undef AnalyseLine
237
238 // interpret the results (FIXME far from being even 50% fool proof)
239 if ( nScan > 0 && nDos + nUnix + nMac == 0 ) {
240 // no newlines at all
241 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName.c_str());
242 }
243 else {
244 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
245 : n##t1 > n##t2 \
246 ? wxTextFileType_##t1 \
247 : wxTextFileType_##t2
248
249 // Watcom C++ doesn't seem to be able to handle the macro
250 // VS: Watcom 11 doesn't have a problem...
251 #if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
252 if ( nDos > nUnix )
253 return GREATER_OF(Dos, Mac);
254 else if ( nDos < nUnix )
255 return GREATER_OF(Unix, Mac);
256 else {
257 // nDos == nUnix
258 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
259 }
260 #endif // __WATCOMC__
261
262 #undef GREATER_OF
263 }
264
265 return typeDefault;
266 }
267
268
269 bool wxTextBuffer::Close()
270 {
271 m_aTypes.Clear();
272 m_aLines.Clear();
273 m_nCurLine = 0;
274 m_isOpened = false;
275
276 return true;
277 }
278
279 bool wxTextBuffer::Write(wxTextFileType typeNew, wxMBConv& conv)
280 {
281 return OnWrite(typeNew, conv);
282 }
283
284 #endif // wxUSE_TEXTBUFFER
285