]>
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 wxStringInputStream::wxStringInputStream(const wxString
& s
)
41 : m_str(s
), m_buf(wxMBConvUTF8().cWX2MB(s
).release()), m_len(strlen(m_buf
))
43 : m_str(s
), m_buf((char*)s
.c_str()), m_len(s
.length())
47 wxASSERT_MSG(m_buf
!= NULL
, _T("Could not convert string to UTF8!"));
52 wxStringInputStream::~wxStringInputStream()
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 wxFileOffset
wxStringInputStream::GetLength() const
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 wxFileOffset
wxStringInputStream::OnSysSeek(wxFileOffset ofs
, wxSeekMode mode
)
77 // nothing to do, ofs already ok
89 wxFAIL_MSG( _T("invalid seek mode") );
90 return wxInvalidOffset
;
93 if ( ofs
< 0 || wx_static_cast(size_t, ofs
) >= m_len
)
94 return wxInvalidOffset
;
96 m_pos
= wx_static_cast(size_t, ofs
);
101 wxFileOffset
wxStringInputStream::OnSysTell() const
103 return wx_static_cast(wxFileOffset
, m_pos
);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 size_t wxStringInputStream::OnSysRead(void *buffer
, size_t size
)
112 const size_t sizeMax
= m_len
- m_pos
;
114 if ( size
>= sizeMax
)
118 m_lasterror
= wxSTREAM_EOF
;
125 memcpy(buffer
, m_buf
+ m_pos
, size
);
131 // ============================================================================
132 // wxStringOutputStream implementation
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 wxFileOffset
wxStringOutputStream::OnSysTell() const
141 return wx_static_cast(wxFileOffset
, m_pos
);
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 size_t wxStringOutputStream::OnSysWrite(const void *buffer
, size_t size
)
150 const char *p
= wx_static_cast(const char *, buffer
);
152 // append the input buffer (may not be null terminated - thus
153 // the literal length
154 m_str
->Append(wxString(p
, m_conv
, size
));
156 // return number of bytes actually written
162 #endif // wxUSE_STREAMS