]> git.saurik.com Git - wxWidgets.git/blob - src/common/textbuf.cpp
Hopefully fixed library names generated by wx-config for OS/2's PM port.
[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__) || defined(__PALMOS__)
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 wxEmptyString;
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.empty() )
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 wxTextBuffer::~wxTextBuffer()
150 {
151 // required here for Darwin
152 }
153
154 // ----------------------------------------------------------------------------
155 // buffer operations
156 // ----------------------------------------------------------------------------
157
158 bool wxTextBuffer::Exists() const
159 {
160 return OnExists();
161 }
162
163 bool wxTextBuffer::Create(const wxString& strBufferName)
164 {
165 m_strBufferName = strBufferName;
166
167 return Create();
168 }
169
170 bool wxTextBuffer::Create()
171 {
172 // buffer name must be either given in ctor or in Create(const wxString&)
173 wxASSERT( !m_strBufferName.empty() );
174
175 // if the buffer already exists do nothing
176 if ( Exists() ) return false;
177
178 if ( !OnOpen(m_strBufferName, WriteAccess) )
179 return false;
180
181 OnClose();
182 return true;
183 }
184
185 bool wxTextBuffer::Open(const wxString& strBufferName, wxMBConv& conv)
186 {
187 m_strBufferName = strBufferName;
188
189 return Open(conv);
190 }
191
192 bool wxTextBuffer::Open(wxMBConv& conv)
193 {
194 // buffer name must be either given in ctor or in Open(const wxString&)
195 wxASSERT( !m_strBufferName.empty() );
196
197 // open buffer in read-only mode
198 if ( !OnOpen(m_strBufferName, ReadAccess) )
199 return false;
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.
211 wxTextFileType 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
255 // VS: Watcom 11 doesn't have a problem...
256 #if !(defined(__WATCOMC__) && (__WATCOMC__ < 1100))
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
274 bool wxTextBuffer::Close()
275 {
276 m_aTypes.Clear();
277 m_aLines.Clear();
278 m_nCurLine = 0;
279 m_isOpened = false;
280
281 return true;
282 }
283
284 bool wxTextBuffer::Write(wxTextFileType typeNew, wxMBConv& conv)
285 {
286 return OnWrite(typeNew, conv);
287 }
288
289 #endif // wxUSE_TEXTBUFFER
290