]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/textbuf.cpp
leave i386 compiler to default
[wxWidgets.git] / src / common / textbuf.cpp
... / ...
CommitLineData
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#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif //__BORLANDC__
19
20#ifndef WX_PRECOMP
21 #include "wx/string.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24#endif
25
26#include "wx/textbuf.h"
27
28// ============================================================================
29// wxTextBuffer class implementation
30// ============================================================================
31
32// ----------------------------------------------------------------------------
33// static methods (always compiled in)
34// ----------------------------------------------------------------------------
35
36// default type is the native one
37
38const wxTextFileType wxTextBuffer::typeDefault =
39#if defined(__WINDOWS__) || defined(__DOS__)
40 wxTextFileType_Dos;
41#elif defined(__UNIX__)
42 wxTextFileType_Unix;
43#elif defined(__OS2__)
44 wxTextFileType_Os2;
45#else
46 wxTextFileType_None;
47 #error "wxTextBuffer: unsupported platform."
48#endif
49
50const wxChar *wxTextBuffer::GetEOL(wxTextFileType type)
51{
52 switch ( type ) {
53 default:
54 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
55 // fall through nevertheless - we must return something...
56
57 case wxTextFileType_None: return wxEmptyString;
58 case wxTextFileType_Unix: return wxT("\n");
59 case wxTextFileType_Dos: return wxT("\r\n");
60 case wxTextFileType_Mac: return wxT("\r");
61 }
62}
63
64wxString wxTextBuffer::Translate(const wxString& text, wxTextFileType type)
65{
66 // don't do anything if there is nothing to do
67 if ( type == wxTextFileType_None )
68 return text;
69
70 // nor if it is empty
71 if ( text.empty() )
72 return text;
73
74 wxString eol = GetEOL(type), result;
75
76 // optimization: we know that the length of the new string will be about
77 // the same as the length of the old one, so prealloc memory to avoid
78 // unnecessary relocations
79 result.Alloc(text.Len());
80
81 wxChar chLast = 0;
82 for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i )
83 {
84 wxChar ch = *i;
85 switch ( ch ) {
86 case wxT('\n'):
87 // Dos/Unix line termination
88 result += eol;
89 chLast = 0;
90 break;
91
92 case wxT('\r'):
93 if ( chLast == wxT('\r') ) {
94 // Mac empty line
95 result += eol;
96 }
97 else {
98 // just remember it: we don't know whether it is just "\r"
99 // or "\r\n" yet
100 chLast = wxT('\r');
101 }
102 break;
103
104 default:
105 if ( chLast == wxT('\r') ) {
106 // Mac line termination
107 result += eol;
108
109 // reset chLast to avoid inserting another eol before the
110 // next character
111 chLast = 0;
112 }
113
114 // add to the current line
115 result += ch;
116 }
117 }
118
119 if ( chLast ) {
120 // trailing '\r'
121 result += eol;
122 }
123
124 return result;
125}
126
127#if wxUSE_TEXTBUFFER
128
129wxString wxTextBuffer::ms_eof;
130
131// ----------------------------------------------------------------------------
132// ctors & dtor
133// ----------------------------------------------------------------------------
134
135wxTextBuffer::wxTextBuffer(const wxString& strBufferName)
136 : m_strBufferName(strBufferName)
137{
138 m_nCurLine = 0;
139 m_isOpened = false;
140}
141
142wxTextBuffer::~wxTextBuffer()
143{
144 // required here for Darwin
145}
146
147// ----------------------------------------------------------------------------
148// buffer operations
149// ----------------------------------------------------------------------------
150
151bool wxTextBuffer::Exists() const
152{
153 return OnExists();
154}
155
156bool wxTextBuffer::Create(const wxString& strBufferName)
157{
158 m_strBufferName = strBufferName;
159
160 return Create();
161}
162
163bool wxTextBuffer::Create()
164{
165 // buffer name must be either given in ctor or in Create(const wxString&)
166 wxASSERT( !m_strBufferName.empty() );
167
168 // if the buffer already exists do nothing
169 if ( Exists() ) return false;
170
171 if ( !OnOpen(m_strBufferName, WriteAccess) )
172 return false;
173
174 OnClose();
175 return true;
176}
177
178bool wxTextBuffer::Open(const wxString& strBufferName, const wxMBConv& conv)
179{
180 m_strBufferName = strBufferName;
181
182 return Open(conv);
183}
184
185bool wxTextBuffer::Open(const wxMBConv& conv)
186{
187 // buffer name must be either given in ctor or in Open(const wxString&)
188 wxASSERT( !m_strBufferName.empty() );
189
190 // open buffer in read-only mode
191 if ( !OnOpen(m_strBufferName, ReadAccess) )
192 return false;
193
194 // read buffer into memory
195 m_isOpened = OnRead(conv);
196
197 OnClose();
198
199 return m_isOpened;
200}
201
202// analyse some lines of the buffer trying to guess it's type.
203// if it fails, it assumes the native type for our platform.
204wxTextFileType wxTextBuffer::GuessType() const
205{
206 wxASSERT( IsOpened() );
207
208 // scan the buffer lines
209 size_t nUnix = 0, // number of '\n's alone
210 nDos = 0, // number of '\r\n'
211 nMac = 0; // number of '\r's
212
213 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
214 #define MAX_LINES_SCAN (10)
215 size_t nCount = m_aLines.GetCount() / 3,
216 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
217
218 #define AnalyseLine(n) \
219 switch ( m_aTypes[n] ) { \
220 case wxTextFileType_Unix: nUnix++; break; \
221 case wxTextFileType_Dos: nDos++; break; \
222 case wxTextFileType_Mac: nMac++; break; \
223 default: wxFAIL_MSG(wxT("unknown line terminator")); \
224 }
225
226 size_t n;
227 for ( n = 0; n < nScan; n++ ) // the beginning
228 AnalyseLine(n);
229 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
230 AnalyseLine(n);
231 for ( n = nCount - nScan; n < nCount; n++ )
232 AnalyseLine(n);
233
234 #undef AnalyseLine
235
236 // interpret the results (FIXME far from being even 50% fool proof)
237 if ( nScan > 0 && nDos + nUnix + nMac == 0 ) {
238 // no newlines at all
239 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName.c_str());
240 }
241 else {
242 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
243 : n##t1 > n##t2 \
244 ? wxTextFileType_##t1 \
245 : wxTextFileType_##t2
246
247#if !defined(__WATCOMC__) || wxCHECK_WATCOM_VERSION(1,4)
248 if ( nDos > nUnix )
249 return GREATER_OF(Dos, Mac);
250 else if ( nDos < nUnix )
251 return GREATER_OF(Unix, Mac);
252 else {
253 // nDos == nUnix
254 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
255 }
256#endif // __WATCOMC__
257
258 #undef GREATER_OF
259 }
260
261 return typeDefault;
262}
263
264
265bool wxTextBuffer::Close()
266{
267 Clear();
268 m_isOpened = false;
269
270 return true;
271}
272
273bool wxTextBuffer::Write(wxTextFileType typeNew, const wxMBConv& conv)
274{
275 return OnWrite(typeNew, conv);
276}
277
278#endif // wxUSE_TEXTBUFFER