]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stdstream.cpp
af8475cdd3b331b37edd2966c7c61859f17e8a45
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stdstream.h
3 // Purpose: Implementation of std::istream and std::ostream derived
4 // wrappers for wxInputStream and wxOutputStream
5 // Author: Jonathan Liu <net147@gmail.com>
8 // Copyright: (c) 2009 Jonathan Liu
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_STD_IOSTREAM
28 #include "wx/stdstream.h"
35 // ==========================================================================
37 // ==========================================================================
43 IosSeekDirToWxSeekMode(std::ios_base::seekdir way
,
48 case std::ios_base::beg
:
49 seekMode
= wxFromStart
;
51 case std::ios_base::cur
:
52 seekMode
= wxFromCurrent
;
54 case std::ios_base::end
:
64 } // anonymous namespace
66 // ==========================================================================
67 // wxStdInputStreamBuffer
68 // ==========================================================================
70 wxStdInputStreamBuffer::wxStdInputStreamBuffer(wxInputStream
& stream
) :
71 m_stream(stream
), m_lastChar(EOF
)
76 wxStdInputStreamBuffer::setbuf(char *WXUNUSED(s
),
77 std::streamsize
WXUNUSED(n
))
83 wxStdInputStreamBuffer::seekoff(std::streamoff off
,
84 std::ios_base::seekdir way
,
85 std::ios_base::openmode which
)
89 if ( !IosSeekDirToWxSeekMode(way
, seekMode
) )
91 if ( !(which
& std::ios_base::in
) )
94 off_t newPos
= m_stream
.SeekI((off_t
) off
, seekMode
);
96 if ( newPos
!= wxInvalidOffset
)
97 return (std::streampos
) newPos
;
103 wxStdInputStreamBuffer::seekpos(std::streampos sp
,
104 std::ios_base::openmode which
)
106 if ( !(which
& std::ios_base::in
) )
109 off_t newPos
= m_stream
.SeekI((off_t
) sp
);
111 if ( newPos
!= wxInvalidOffset
)
112 return (std::streampos
) newPos
;
118 wxStdInputStreamBuffer::showmanyc()
120 if ( m_stream
.CanRead() && (off_t
) m_stream
.GetSize() > m_stream
.TellI() )
121 return m_stream
.GetSize() - m_stream
.TellI();
127 wxStdInputStreamBuffer::xsgetn(char *s
, std::streamsize n
)
129 m_stream
.Read((void *) s
, (size_t) n
);
131 std::streamsize read
= m_stream
.LastRead();
134 m_lastChar
= (unsigned char) s
[read
- 1];
140 wxStdInputStreamBuffer::underflow()
142 int ch
= m_stream
.GetC();
144 if ( m_stream
.LastRead() == 1 )
146 m_stream
.Ungetch((char) ch
);
156 wxStdInputStreamBuffer::uflow()
158 int ch
= m_stream
.GetC();
160 if ( m_stream
.LastRead() == 1 )
172 wxStdInputStreamBuffer::pbackfail(int c
)
176 if ( m_lastChar
== EOF
)
183 return m_stream
.Ungetch((char) c
) ? c
: EOF
;
186 // ==========================================================================
188 // ==========================================================================
190 wxStdInputStream::wxStdInputStream(wxInputStream
& stream
) :
191 std::istream(NULL
), m_streamBuffer(stream
)
193 std::ios::init(&m_streamBuffer
);
196 // ==========================================================================
197 // wxStdOutputStreamBuffer
198 // ==========================================================================
200 wxStdOutputStreamBuffer::wxStdOutputStreamBuffer(wxOutputStream
& stream
) :
206 wxStdOutputStreamBuffer::setbuf(char *WXUNUSED(s
),
207 std::streamsize
WXUNUSED(n
))
213 wxStdOutputStreamBuffer::seekoff(std::streamoff off
,
214 std::ios_base::seekdir way
,
215 std::ios_base::openmode which
)
219 if ( !IosSeekDirToWxSeekMode(way
, seekMode
) )
221 if ( !(which
& std::ios_base::out
) )
224 off_t newPos
= m_stream
.SeekO((off_t
) off
, seekMode
);
226 if ( newPos
!= wxInvalidOffset
)
227 return (std::streampos
) newPos
;
233 wxStdOutputStreamBuffer::seekpos(std::streampos sp
,
234 std::ios_base::openmode which
)
236 if ( !(which
& std::ios_base::out
) )
239 off_t newPos
= m_stream
.SeekO((off_t
) sp
);
241 if ( newPos
!= wxInvalidOffset
)
242 return (std::streampos
) newPos
;
248 wxStdOutputStreamBuffer::xsputn(const char *s
,
251 m_stream
.Write((const void *) s
, (size_t) n
);
252 return (std::streamsize
) m_stream
.LastWrite();
256 wxStdOutputStreamBuffer::overflow(int c
)
259 return m_stream
.IsOk() ? c
: EOF
;
262 // ==========================================================================
264 // ==========================================================================
266 wxStdOutputStream::wxStdOutputStream(wxOutputStream
& stream
) :
267 std::ostream(NULL
), m_streamBuffer(stream
)
269 std::ios::init(&m_streamBuffer
);
272 #endif // wxUSE_STD_IOSTREAM