]>
Commit | Line | Data |
---|---|---|
121fa06a VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/sstream.cpp | |
3 | // Purpose: string-based streams implementation | |
4 | // Author: Vadim Zeitlin | |
c5d99d7b | 5 | // Modified by: Ryan Norton (UTF8 UNICODE) |
121fa06a VZ |
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 | ||
c5d99d7b RN |
35 | // ---------------------------------------------------------------------------- |
36 | // construction/destruction | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
cb28cc9e RN |
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 | |
86501081 | 41 | wxStringInputStream::wxStringInputStream(const wxString& s) |
c5d99d7b | 42 | #if wxUSE_UNICODE |
86501081 VS |
43 | // FIXME-UTF8: use wxCharBufferWithLength if we have it |
44 | : m_str(s), m_buf(s.utf8_str()), m_len(strlen(m_buf)) | |
c5d99d7b | 45 | #else |
86501081 | 46 | : m_str(s), m_buf(s.mb_str()), m_len(s.length()) |
c5d99d7b RN |
47 | #endif |
48 | { | |
49 | #if wxUSE_UNICODE | |
86501081 | 50 | wxASSERT_MSG(m_buf.data() != NULL, _T("Could not convert string to UTF8!")); |
c5d99d7b RN |
51 | #endif |
52 | m_pos = 0; | |
53 | } | |
54 | ||
c5d99d7b RN |
55 | // ---------------------------------------------------------------------------- |
56 | // getlength | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | wxFileOffset wxStringInputStream::GetLength() const | |
60 | { | |
61 | return m_len; | |
62 | } | |
63 | ||
121fa06a VZ |
64 | // ---------------------------------------------------------------------------- |
65 | // seek/tell | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
4004775e | 68 | wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode) |
121fa06a VZ |
69 | { |
70 | switch ( mode ) | |
71 | { | |
72 | case wxFromStart: | |
73 | // nothing to do, ofs already ok | |
74 | break; | |
75 | ||
76 | case wxFromEnd: | |
c5d99d7b | 77 | ofs += m_len; |
121fa06a VZ |
78 | break; |
79 | ||
80 | case wxFromCurrent: | |
81 | ofs += m_pos; | |
82 | break; | |
83 | ||
84 | default: | |
85 | wxFAIL_MSG( _T("invalid seek mode") ); | |
86 | return wxInvalidOffset; | |
87 | } | |
88 | ||
c781c316 | 89 | if ( ofs < 0 || ofs > wx_static_cast(wxFileOffset, m_len) ) |
5a4c7f71 VZ |
90 | return wxInvalidOffset; |
91 | ||
c781c316 VZ |
92 | // FIXME: this can't be right |
93 | m_pos = wx_truncate_cast(size_t, ofs); | |
121fa06a VZ |
94 | |
95 | return ofs; | |
96 | } | |
97 | ||
4004775e | 98 | wxFileOffset wxStringInputStream::OnSysTell() const |
121fa06a | 99 | { |
4004775e | 100 | return wx_static_cast(wxFileOffset, m_pos); |
121fa06a VZ |
101 | } |
102 | ||
103 | // ---------------------------------------------------------------------------- | |
104 | // actual IO | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | size_t wxStringInputStream::OnSysRead(void *buffer, size_t size) | |
108 | { | |
c5d99d7b | 109 | const size_t sizeMax = m_len - m_pos; |
121fa06a VZ |
110 | |
111 | if ( size >= sizeMax ) | |
112 | { | |
113 | if ( sizeMax == 0 ) | |
114 | { | |
115 | m_lasterror = wxSTREAM_EOF; | |
116 | return 0; | |
117 | } | |
118 | ||
119 | size = sizeMax; | |
120 | } | |
121 | ||
86501081 | 122 | memcpy(buffer, m_buf.data() + m_pos, size); |
121fa06a VZ |
123 | m_pos += size; |
124 | ||
125 | return size; | |
126 | } | |
127 | ||
128 | // ============================================================================ | |
129 | // wxStringOutputStream implementation | |
130 | // ============================================================================ | |
131 | ||
a5ea75bc VZ |
132 | // ---------------------------------------------------------------------------- |
133 | // seek/tell | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
4004775e | 136 | wxFileOffset wxStringOutputStream::OnSysTell() const |
a5ea75bc | 137 | { |
4004775e | 138 | return wx_static_cast(wxFileOffset, m_pos); |
a5ea75bc VZ |
139 | } |
140 | ||
121fa06a VZ |
141 | // ---------------------------------------------------------------------------- |
142 | // actual IO | |
143 | // ---------------------------------------------------------------------------- | |
144 | ||
145 | size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size) | |
146 | { | |
c5d99d7b | 147 | const char *p = wx_static_cast(const char *, buffer); |
121fa06a | 148 | |
2c17722e VZ |
149 | #if wxUSE_UNICODE_WCHAR |
150 | // the part of the string we have here may be incomplete, i.e. it can stop | |
151 | // in the middle of an UTF-8 character and so converting it would fail; if | |
152 | // this is the case, accumulate the part which we failed to convert until | |
153 | // we get the rest (and also take into account the part which we might have | |
154 | // left unconverted before) | |
155 | const char *src; | |
156 | size_t srcLen; | |
157 | if ( m_unconv.GetDataLen() ) | |
158 | { | |
159 | // append the new data to the data remaining since the last time | |
160 | m_unconv.AppendData(p, size); | |
161 | src = m_unconv; | |
162 | srcLen = m_unconv.GetDataLen(); | |
163 | } | |
164 | else // no unconverted data left, avoid extra copy | |
165 | { | |
166 | src = p; | |
167 | srcLen = size; | |
168 | } | |
169 | ||
170 | wxWCharBuffer wbuf(m_conv.cMB2WC(src, srcLen, NULL /* out len */)); | |
171 | if ( wbuf ) | |
172 | { | |
173 | // conversion succeeded, clear the unconverted buffer | |
174 | m_unconv = wxMemoryBuffer(0); | |
175 | ||
176 | *m_str += wbuf; | |
177 | } | |
178 | else // conversion failed | |
179 | { | |
180 | // remember unconverted data if there had been none before (otherwise | |
181 | // we've already got it in the buffer) | |
182 | if ( src == p ) | |
183 | m_unconv.AppendData(src, srcLen); | |
184 | ||
185 | // pretend that we wrote the data anyhow, otherwise the caller would | |
186 | // believe there was an error and this might not be the case, but do | |
187 | // not update m_pos as m_str hasn't changed | |
188 | return size; | |
189 | } | |
190 | #else // !wxUSE_UNICODE_WCHAR | |
191 | // no recoding necessary, the data is supposed to already be in UTF-8 (if | |
192 | // supported) or ASCII otherwise | |
193 | m_str->append(p, size); | |
194 | #endif // wxUSE_UNICODE_WCHAR/!wxUSE_UNICODE_WCHAR | |
121fa06a | 195 | |
cb28cc9e | 196 | // update position |
c5d99d7b | 197 | m_pos += size; |
a5ea75bc | 198 | |
cb28cc9e | 199 | // return number of bytes actually written |
c5d99d7b | 200 | return size; |
121fa06a VZ |
201 | } |
202 | ||
203 | #endif // wxUSE_STREAMS | |
204 |