]> git.saurik.com Git - wxWidgets.git/blob - src/common/sstream.cpp
(blind) fix for GTK2/Unicode crashes when copying text to the clipboard
[wxWidgets.git] / src / common / sstream.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/sstream.cpp
3 // Purpose: string-based streams implementation
4 // Author: Vadim Zeitlin
5 // Modified by: Ryan Norton (UTF8 UNICODE)
6 // Created: 2004-09-19
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STREAMS
28
29 #include "wx/sstream.h"
30
31 // ============================================================================
32 // wxStringInputStream implementation
33 // ============================================================================
34
35 // ----------------------------------------------------------------------------
36 // construction/destruction
37 // ----------------------------------------------------------------------------
38
39 wxStringInputStream::wxStringInputStream(const wxString& s)
40 #if wxUSE_UNICODE
41 : m_str(s), m_buf(wxMBConvUTF8().cWX2MB(s).release()), m_len(strlen(m_buf))
42 #else
43 : m_str(s), m_buf((char*)s.c_str()), m_len(s.length())
44 #endif
45 {
46 #if wxUSE_UNICODE
47 wxASSERT_MSG(m_buf != NULL, _T("Could not convert string to UTF8!"));
48 #endif
49 m_pos = 0;
50 }
51
52 wxStringInputStream::~wxStringInputStream()
53 {
54 #if wxUSE_UNICODE
55 delete[] m_buf;
56 #endif
57 }
58
59 // ----------------------------------------------------------------------------
60 // getlength
61 // ----------------------------------------------------------------------------
62
63 wxFileOffset wxStringInputStream::GetLength() const
64 {
65 return m_len;
66 }
67
68 // ----------------------------------------------------------------------------
69 // seek/tell
70 // ----------------------------------------------------------------------------
71
72 wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode)
73 {
74 switch ( mode )
75 {
76 case wxFromStart:
77 // nothing to do, ofs already ok
78 break;
79
80 case wxFromEnd:
81 ofs += m_len;
82 break;
83
84 case wxFromCurrent:
85 ofs += m_pos;
86 break;
87
88 default:
89 wxFAIL_MSG( _T("invalid seek mode") );
90 return wxInvalidOffset;
91 }
92
93 if ( ofs < 0 || wx_static_cast(size_t, ofs) >= m_len )
94 return wxInvalidOffset;
95
96 m_pos = wx_static_cast(size_t, ofs);
97
98 return ofs;
99 }
100
101 wxFileOffset wxStringInputStream::OnSysTell() const
102 {
103 return wx_static_cast(wxFileOffset, m_pos);
104 }
105
106 // ----------------------------------------------------------------------------
107 // actual IO
108 // ----------------------------------------------------------------------------
109
110 size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
111 {
112 const size_t sizeMax = m_len - m_pos;
113
114 if ( size >= sizeMax )
115 {
116 if ( sizeMax == 0 )
117 {
118 m_lasterror = wxSTREAM_EOF;
119 return 0;
120 }
121
122 size = sizeMax;
123 }
124
125 memcpy(buffer, m_buf + m_pos, size);
126 m_pos += size;
127
128 return size;
129 }
130
131 // ============================================================================
132 // wxStringOutputStream implementation
133 // ============================================================================
134
135 // ----------------------------------------------------------------------------
136 // seek/tell
137 // ----------------------------------------------------------------------------
138
139 wxFileOffset wxStringOutputStream::OnSysTell() const
140 {
141 return wx_static_cast(wxFileOffset, m_pos);
142 }
143
144 // ----------------------------------------------------------------------------
145 // actual IO
146 // ----------------------------------------------------------------------------
147
148 size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
149 {
150 const char *p = wx_static_cast(const char *, buffer);
151
152 // append the input buffer (may not be null terminated - thus
153 // the literal length
154 m_str->Append(wxString(p, m_conv, size));
155
156 // return number of bytes actually written
157 m_pos += size;
158
159 return size;
160 }
161
162 #endif // wxUSE_STREAMS
163