]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sstream.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/sstream.cpp
3 // Purpose: string-based streams implementation
4 // Author: Vadim Zeitlin
5 // Modified by: Ryan Norton (UTF8 UNICODE)
7 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/sstream.h"
30 // ============================================================================
31 // wxStringInputStream implementation
32 // ============================================================================
34 // ----------------------------------------------------------------------------
35 // construction/destruction
36 // ----------------------------------------------------------------------------
38 // TODO: Do we want to include the null char in the stream? If so then
39 // just add +1 to m_len in the ctor
40 wxStringInputStream::wxStringInputStream(const wxString
& s
)
42 // FIXME-UTF8: use wxCharBufferWithLength if we have it
43 : m_str(s
), m_buf(s
.utf8_str()), m_len(strlen(m_buf
))
45 : m_str(s
), m_buf(s
.mb_str()), m_len(s
.length())
49 wxASSERT_MSG(m_buf
.data() != NULL
, wxT("Could not convert string to UTF8!"));
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxFileOffset
wxStringInputStream::GetLength() const
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 wxFileOffset
wxStringInputStream::OnSysSeek(wxFileOffset ofs
, wxSeekMode mode
)
72 // nothing to do, ofs already ok
84 wxFAIL_MSG( wxT("invalid seek mode") );
85 return wxInvalidOffset
;
88 if ( ofs
< 0 || ofs
> static_cast<wxFileOffset
>(m_len
) )
89 return wxInvalidOffset
;
91 // FIXME: this can't be right
92 m_pos
= wx_truncate_cast(size_t, ofs
);
97 wxFileOffset
wxStringInputStream::OnSysTell() const
99 return static_cast<wxFileOffset
>(m_pos
);
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 size_t wxStringInputStream::OnSysRead(void *buffer
, size_t size
)
108 const size_t sizeMax
= m_len
- m_pos
;
110 if ( size
>= sizeMax
)
114 m_lasterror
= wxSTREAM_EOF
;
121 memcpy(buffer
, m_buf
.data() + m_pos
, size
);
127 // ============================================================================
128 // wxStringOutputStream implementation
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 wxFileOffset
wxStringOutputStream::OnSysTell() const
137 return static_cast<wxFileOffset
>(m_pos
);
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 size_t wxStringOutputStream::OnSysWrite(const void *buffer
, size_t size
)
146 const char *p
= static_cast<const char *>(buffer
);
149 // the part of the string we have here may be incomplete, i.e. it can stop
150 // in the middle of an UTF-8 character and so converting it would fail; if
151 // this is the case, accumulate the part which we failed to convert until
152 // we get the rest (and also take into account the part which we might have
153 // left unconverted before)
156 if ( m_unconv
.GetDataLen() )
158 // append the new data to the data remaining since the last time
159 m_unconv
.AppendData(p
, size
);
161 srcLen
= m_unconv
.GetDataLen();
163 else // no unconverted data left, avoid extra copy
170 wxWCharBuffer
wbuf(m_conv
.cMB2WC(src
, srcLen
, &wlen
));
173 // conversion succeeded, clear the unconverted buffer
174 m_unconv
= wxMemoryBuffer(0);
176 m_str
->append(wbuf
, wlen
);
178 else // conversion failed
180 // remember unconverted data if there had been none before (otherwise
181 // we've already got it in the buffer)
183 m_unconv
.AppendData(src
, srcLen
);
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
190 #else // !wxUSE_UNICODE
191 // no recoding necessary
192 m_str
->append(p
, size
);
193 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
198 // return number of bytes actually written
202 #endif // wxUSE_STREAMS