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