]>
git.saurik.com Git - wxWidgets.git/blob - 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)
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 : m_str(s
), m_buf(wxMBConvUTF8().cWX2MB(s
).release()), m_len(strlen(m_buf
))
45 : m_str(s
), m_buf((char*)s
.c_str()), m_len(s
.length())
49 wxASSERT_MSG(m_buf
!= NULL
, _T("Could not convert string to UTF8!"));
54 wxStringInputStream::~wxStringInputStream()
57 // Note: wx[W]CharBuffer uses malloc()/free()
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 wxFileOffset
wxStringInputStream::GetLength() const
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 wxFileOffset
wxStringInputStream::OnSysSeek(wxFileOffset ofs
, wxSeekMode mode
)
80 // nothing to do, ofs already ok
92 wxFAIL_MSG( _T("invalid seek mode") );
93 return wxInvalidOffset
;
96 if ( ofs
< 0 || ofs
> wx_static_cast(wxFileOffset
, m_len
) )
97 return wxInvalidOffset
;
99 // FIXME: this can't be right
100 m_pos
= wx_truncate_cast(size_t, ofs
);
105 wxFileOffset
wxStringInputStream::OnSysTell() const
107 return wx_static_cast(wxFileOffset
, m_pos
);
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 size_t wxStringInputStream::OnSysRead(void *buffer
, size_t size
)
116 const size_t sizeMax
= m_len
- m_pos
;
118 if ( size
>= sizeMax
)
122 m_lasterror
= wxSTREAM_EOF
;
129 memcpy(buffer
, m_buf
+ m_pos
, size
);
135 // ============================================================================
136 // wxStringOutputStream implementation
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 wxFileOffset
wxStringOutputStream::OnSysTell() const
145 return wx_static_cast(wxFileOffset
, m_pos
);
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 size_t wxStringOutputStream::OnSysWrite(const void *buffer
, size_t size
)
154 const char *p
= wx_static_cast(const char *, buffer
);
156 // append the input buffer (may not be null terminated - thus
157 // the literal length
158 m_str
->Append(wxString(p
, m_conv
, size
));
163 // return number of bytes actually written
167 #endif // wxUSE_STREAMS