]> git.saurik.com Git - wxWidgets.git/blame - src/common/textbuf.cpp
Misc validity fixes to samples/xrc/rc/*.xrc.
[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
a3a584a7
VZ
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
a3a584a7
VZ
37
38const wxTextFileType wxTextBuffer::typeDefault =
bd362275 39#if defined(__WINDOWS__) || defined(__DOS__)
a3a584a7
VZ
40 wxTextFileType_Dos;
41#elif defined(__UNIX__)
42 wxTextFileType_Unix;
55034339 43#elif defined(__OS2__)
a3a584a7
VZ
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
525d8583 57 case wxTextFileType_None: return wxEmptyString;
a3a584a7
VZ
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
4055ed82 71 if ( text.empty() )
a3a584a7
VZ
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
769dd0ce 77 // the same as the length of the old one, so prealloc memory to avoid
a3a584a7
VZ
78 // unnecessary relocations
79 result.Alloc(text.Len());
80
81 wxChar chLast = 0;
769dd0ce 82 for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i )
a3a584a7 83 {
769dd0ce 84 wxChar ch = *i;
a3a584a7 85 switch ( ch ) {
9a83f860 86 case wxT('\n'):
a3a584a7
VZ
87 // Dos/Unix line termination
88 result += eol;
89 chLast = 0;
90 break;
91
9a83f860
VZ
92 case wxT('\r'):
93 if ( chLast == wxT('\r') ) {
a3a584a7
VZ
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
9a83f860 100 chLast = wxT('\r');
a3a584a7
VZ
101 }
102 break;
103
104 default:
9a83f860 105 if ( chLast == wxT('\r') ) {
a3a584a7
VZ
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
4ae776b7
VZ
129wxString wxTextBuffer::ms_eof;
130
a3a584a7
VZ
131// ----------------------------------------------------------------------------
132// ctors & dtor
133// ----------------------------------------------------------------------------
134
135wxTextBuffer::wxTextBuffer(const wxString& strBufferName)
136 : m_strBufferName(strBufferName)
137{
138 m_nCurLine = 0;
cb719f2e 139 m_isOpened = false;
a3a584a7
VZ
140}
141
5e233068
WS
142wxTextBuffer::~wxTextBuffer()
143{
144 // required here for Darwin
145}
146
a3a584a7
VZ
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&)
4055ed82 166 wxASSERT( !m_strBufferName.empty() );
a3a584a7
VZ
167
168 // if the buffer already exists do nothing
cb719f2e
WS
169 if ( Exists() ) return false;
170
a3a584a7 171 if ( !OnOpen(m_strBufferName, WriteAccess) )
cb719f2e 172 return false;
a3a584a7
VZ
173
174 OnClose();
cb719f2e 175 return true;
a3a584a7
VZ
176}
177
830f8f11 178bool wxTextBuffer::Open(const wxString& strBufferName, const wxMBConv& conv)
a3a584a7
VZ
179{
180 m_strBufferName = strBufferName;
181
182 return Open(conv);
183}
184
830f8f11 185bool wxTextBuffer::Open(const wxMBConv& conv)
a3a584a7
VZ
186{
187 // buffer name must be either given in ctor or in Open(const wxString&)
4055ed82 188 wxASSERT( !m_strBufferName.empty() );
a3a584a7
VZ
189
190 // open buffer in read-only mode
191 if ( !OnOpen(m_strBufferName, ReadAccess) )
cb719f2e 192 return false;
a3a584a7
VZ
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)
b4a980f4 215 size_t nCount = m_aLines.GetCount() / 3,
a3a584a7
VZ
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; \
9a83f860 223 default: wxFAIL_MSG(wxT("unknown line terminator")); \
a3a584a7
VZ
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
6d3d756a 247#if !defined(__WATCOMC__) || wxCHECK_WATCOM_VERSION(1,4)
a3a584a7
VZ
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{
3ae00f5b 267 Clear();
cb719f2e 268 m_isOpened = false;
a3a584a7 269
cb719f2e 270 return true;
a3a584a7
VZ
271}
272
830f8f11 273bool wxTextBuffer::Write(wxTextFileType typeNew, const wxMBConv& conv)
a3a584a7
VZ
274{
275 return OnWrite(typeNew, conv);
276}
277
278#endif // wxUSE_TEXTBUFFER