]> git.saurik.com Git - wxWidgets.git/blob - src/common/sstream.cpp
avoiding nesting dcs on the same window concurrently
[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 // TODO: Do we want to include the null char in the stream? If so then
40 // just add +1 to m_len in the ctor
41 wxStringInputStream::wxStringInputStream(const wxString& s)
42 #if wxUSE_UNICODE
43 : m_str(s), m_buf(wxMBConvUTF8().cWX2MB(s).release()), m_len(strlen(m_buf))
44 #else
45 : m_str(s), m_buf((char*)s.c_str()), m_len(s.length())
46 #endif
47 {
48 #if wxUSE_UNICODE
49 wxASSERT_MSG(m_buf != NULL, _T("Could not convert string to UTF8!"));
50 #endif
51 m_pos = 0;
52 }
53
54 wxStringInputStream::~wxStringInputStream()
55 {
56 #if wxUSE_UNICODE
57 // Note: wx[W]CharBuffer uses malloc()/free()
58 free(m_buf);
59 #endif
60 }
61
62 // ----------------------------------------------------------------------------
63 // getlength
64 // ----------------------------------------------------------------------------
65
66 wxFileOffset wxStringInputStream::GetLength() const
67 {
68 return m_len;
69 }
70
71 // ----------------------------------------------------------------------------
72 // seek/tell
73 // ----------------------------------------------------------------------------
74
75 wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode)
76 {
77 switch ( mode )
78 {
79 case wxFromStart:
80 // nothing to do, ofs already ok
81 break;
82
83 case wxFromEnd:
84 ofs += m_len;
85 break;
86
87 case wxFromCurrent:
88 ofs += m_pos;
89 break;
90
91 default:
92 wxFAIL_MSG( _T("invalid seek mode") );
93 return wxInvalidOffset;
94 }
95
96 if ( ofs < 0 || ofs > wx_static_cast(wxFileOffset, m_len) )
97 return wxInvalidOffset;
98
99 // FIXME: this can't be right
100 m_pos = wx_truncate_cast(size_t, ofs);
101
102 return ofs;
103 }
104
105 wxFileOffset wxStringInputStream::OnSysTell() const
106 {
107 return wx_static_cast(wxFileOffset, m_pos);
108 }
109
110 // ----------------------------------------------------------------------------
111 // actual IO
112 // ----------------------------------------------------------------------------
113
114 size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
115 {
116 const size_t sizeMax = m_len - m_pos;
117
118 if ( size >= sizeMax )
119 {
120 if ( sizeMax == 0 )
121 {
122 m_lasterror = wxSTREAM_EOF;
123 return 0;
124 }
125
126 size = sizeMax;
127 }
128
129 memcpy(buffer, m_buf + m_pos, size);
130 m_pos += size;
131
132 return size;
133 }
134
135 // ============================================================================
136 // wxStringOutputStream implementation
137 // ============================================================================
138
139 // ----------------------------------------------------------------------------
140 // seek/tell
141 // ----------------------------------------------------------------------------
142
143 wxFileOffset wxStringOutputStream::OnSysTell() const
144 {
145 return wx_static_cast(wxFileOffset, m_pos);
146 }
147
148 // ----------------------------------------------------------------------------
149 // actual IO
150 // ----------------------------------------------------------------------------
151
152 size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
153 {
154 const char *p = wx_static_cast(const char *, buffer);
155
156 // append the input buffer (may not be null terminated - thus
157 // the literal length
158 m_str->Append(wxString(p, m_conv, size));
159
160 // update position
161 m_pos += size;
162
163 // return number of bytes actually written
164 return size;
165 }
166
167 #endif // wxUSE_STREAMS
168