]> git.saurik.com Git - wxWidgets.git/blame - src/common/textbuf.cpp
Converting uninitialized data is not a Good Idea
[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
6// Copyright: (c) 1998-2001 wxWindows team
7// Licence: wxWindows license
8///////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// headers
12// ============================================================================
13
14#ifdef __GNUG__
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 =
46#if defined(__WINDOWS__)
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
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
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
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
142wxTextBuffer::wxTextBuffer(const wxString& strBufferName)
143 : m_strBufferName(strBufferName)
144{
145 m_nCurLine = 0;
146 m_isOpened = FALSE;
147}
148
149wxTextBuffer::~wxTextBuffer()
150{
151}
152
153// ----------------------------------------------------------------------------
154// buffer operations
155// ----------------------------------------------------------------------------
156
157bool wxTextBuffer::Exists() const
158{
159 return OnExists();
160}
161
162bool wxTextBuffer::Create(const wxString& strBufferName)
163{
164 m_strBufferName = strBufferName;
165
166 return Create();
167}
168
169bool wxTextBuffer::Create()
170{
171 // buffer name must be either given in ctor or in Create(const wxString&)
172 wxASSERT( !m_strBufferName.IsEmpty() );
173
174 // if the buffer already exists do nothing
175 if ( Exists() ) return FALSE;
176
177 if ( !OnOpen(m_strBufferName, WriteAccess) )
178 return FALSE;
179
180 OnClose();
181 return TRUE;
182}
183
184bool wxTextBuffer::Open(const wxString& strBufferName, wxMBConv& conv)
185{
186 m_strBufferName = strBufferName;
187
188 return Open(conv);
189}
190
191bool wxTextBuffer::Open(wxMBConv& conv)
192{
193 // buffer name must be either given in ctor or in Open(const wxString&)
194 wxASSERT( !m_strBufferName.IsEmpty() );
195
196 // open buffer in read-only mode
197 if ( !OnOpen(m_strBufferName, ReadAccess) )
198 return FALSE;
199
200 // read buffer into memory
201 m_isOpened = OnRead(conv);
202
203 OnClose();
204
205 return m_isOpened;
206}
207
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.
210wxTextFileType wxTextBuffer::GuessType() const
211{
212 wxASSERT( IsOpened() );
213
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
218
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;
223
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")); \
230 }
231
232 size_t n;
233 for ( n = 0; n < nScan; n++ ) // the beginning
234 AnalyseLine(n);
235 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
236 AnalyseLine(n);
237 for ( n = nCount - nScan; n < nCount; n++ )
238 AnalyseLine(n);
239
240 #undef AnalyseLine
241
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());
246 }
247 else {
248 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
249 : n##t1 > n##t2 \
250 ? wxTextFileType_##t1 \
251 : wxTextFileType_##t2
252
253 // Watcom C++ doesn't seem to be able to handle the macro
254#if !defined(__WATCOMC__)
255 if ( nDos > nUnix )
256 return GREATER_OF(Dos, Mac);
257 else if ( nDos < nUnix )
258 return GREATER_OF(Unix, Mac);
259 else {
260 // nDos == nUnix
261 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
262 }
263#endif // __WATCOMC__
264
265 #undef GREATER_OF
266 }
267
268 return typeDefault;
269}
270
271
272bool wxTextBuffer::Close()
273{
274 m_aTypes.Clear();
275 m_aLines.Clear();
276 m_nCurLine = 0;
277 m_isOpened = FALSE;
278
279 return TRUE;
280}
281
282bool wxTextBuffer::Write(wxTextFileType typeNew, wxMBConv& conv)
283{
284 return OnWrite(typeNew, conv);
285}
286
287#endif // wxUSE_TEXTBUFFER
288