]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sstream.cpp
aabe01e977646ca3a7f715467933458a1bea3cf3
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/sstream.cpp
3 // Purpose: string-based streams implementation
4 // Author: Vadim Zeitlin
5 // Modified by: Ryan Norton (UTF8 UNICODE)
8 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/sstream.h"
31 // ============================================================================
32 // wxStringInputStream implementation
33 // ============================================================================
35 // ----------------------------------------------------------------------------
36 // construction/destruction
37 // ----------------------------------------------------------------------------
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
)
43 // FIXME-UTF8: use wxCharBufferWithLength if we have it
44 : m_str(s
), m_buf(s
.utf8_str()), m_len(strlen(m_buf
))
46 : m_str(s
), m_buf(s
.mb_str()), m_len(s
.length())
50 wxASSERT_MSG(m_buf
.data() != NULL
, _T("Could not convert string to UTF8!"));
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 wxFileOffset
wxStringInputStream::GetLength() const
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 wxFileOffset
wxStringInputStream::OnSysSeek(wxFileOffset ofs
, wxSeekMode mode
)
73 // nothing to do, ofs already ok
85 wxFAIL_MSG( _T("invalid seek mode") );
86 return wxInvalidOffset
;
89 if ( ofs
< 0 || ofs
> wx_static_cast(wxFileOffset
, m_len
) )
90 return wxInvalidOffset
;
92 // FIXME: this can't be right
93 m_pos
= wx_truncate_cast(size_t, ofs
);
98 wxFileOffset
wxStringInputStream::OnSysTell() const
100 return wx_static_cast(wxFileOffset
, m_pos
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 size_t wxStringInputStream::OnSysRead(void *buffer
, size_t size
)
109 const size_t sizeMax
= m_len
- m_pos
;
111 if ( size
>= sizeMax
)
115 m_lasterror
= wxSTREAM_EOF
;
122 memcpy(buffer
, m_buf
.data() + m_pos
, size
);
128 // ============================================================================
129 // wxStringOutputStream implementation
130 // ============================================================================
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxFileOffset
wxStringOutputStream::OnSysTell() const
138 return wx_static_cast(wxFileOffset
, m_pos
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 size_t wxStringOutputStream::OnSysWrite(const void *buffer
, size_t size
)
147 const char *p
= wx_static_cast(const char *, buffer
);
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)
157 if ( m_unconv
.GetDataLen() )
159 // append the new data to the data remaining since the last time
160 m_unconv
.AppendData(p
, size
);
162 srcLen
= m_unconv
.GetDataLen();
164 else // no unconverted data left, avoid extra copy
171 wxWCharBuffer
wbuf(m_conv
.cMB2WC(src
, srcLen
, &wlen
));
174 // conversion succeeded, clear the unconverted buffer
175 m_unconv
= wxMemoryBuffer(0);
177 m_str
->append(wbuf
, wlen
);
179 else // conversion failed
181 // remember unconverted data if there had been none before (otherwise
182 // we've already got it in the buffer)
184 m_unconv
.AppendData(src
, srcLen
);
186 // pretend that we wrote the data anyhow, otherwise the caller would
187 // believe there was an error and this might not be the case, but do
188 // not update m_pos as m_str hasn't changed
191 #else // !wxUSE_UNICODE_WCHAR
192 // no recoding necessary, the data is supposed to already be in UTF-8 (if
193 // supported) or ASCII otherwise
194 m_str
->append(p
, size
);
195 #endif // wxUSE_UNICODE_WCHAR/!wxUSE_UNICODE_WCHAR
200 // return number of bytes actually written
204 #endif // wxUSE_STREAMS