]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stdstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/stdstream.cpp
3 // Purpose: Implementation of std::istream and std::ostream derived
4 // wrappers for wxInputStream and wxOutputStream
5 // Author: Jonathan Liu <net147@gmail.com>
7 // Copyright: (c) 2009 Jonathan Liu
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ==========================================================================
13 // ==========================================================================
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #if wxUSE_STREAMS && wxUSE_STD_IOSTREAM
27 #include "wx/stdstream.h"
34 // ==========================================================================
36 // ==========================================================================
42 IosSeekDirToWxSeekMode(std::ios_base::seekdir way
,
47 case std::ios_base::beg
:
48 seekMode
= wxFromStart
;
50 case std::ios_base::cur
:
51 seekMode
= wxFromCurrent
;
53 case std::ios_base::end
:
63 } // anonymous namespace
65 // ==========================================================================
66 // wxStdInputStreamBuffer
67 // ==========================================================================
69 wxStdInputStreamBuffer::wxStdInputStreamBuffer(wxInputStream
& stream
) :
70 m_stream(stream
), m_lastChar(EOF
)
75 wxStdInputStreamBuffer::setbuf(char *WXUNUSED(s
),
76 std::streamsize
WXUNUSED(n
))
82 wxStdInputStreamBuffer::seekoff(std::streamoff off
,
83 std::ios_base::seekdir way
,
84 std::ios_base::openmode which
)
88 if ( !IosSeekDirToWxSeekMode(way
, seekMode
) )
90 if ( !(which
& std::ios_base::in
) )
93 off_t newPos
= m_stream
.SeekI((off_t
) off
, seekMode
);
95 if ( newPos
!= wxInvalidOffset
)
96 return (std::streampos
) newPos
;
102 wxStdInputStreamBuffer::seekpos(std::streampos sp
,
103 std::ios_base::openmode which
)
105 if ( !(which
& std::ios_base::in
) )
108 off_t newPos
= m_stream
.SeekI((off_t
) sp
);
110 if ( newPos
!= wxInvalidOffset
)
111 return (std::streampos
) newPos
;
117 wxStdInputStreamBuffer::showmanyc()
119 if ( m_stream
.CanRead() && (off_t
) m_stream
.GetSize() > m_stream
.TellI() )
120 return m_stream
.GetSize() - m_stream
.TellI();
126 wxStdInputStreamBuffer::xsgetn(char *s
, std::streamsize n
)
128 m_stream
.Read((void *) s
, (size_t) n
);
130 std::streamsize read
= m_stream
.LastRead();
133 m_lastChar
= (unsigned char) s
[read
- 1];
139 wxStdInputStreamBuffer::underflow()
141 int ch
= m_stream
.GetC();
143 if ( m_stream
.LastRead() == 1 )
145 m_stream
.Ungetch((char) ch
);
155 wxStdInputStreamBuffer::uflow()
157 int ch
= m_stream
.GetC();
159 if ( m_stream
.LastRead() == 1 )
171 wxStdInputStreamBuffer::pbackfail(int c
)
175 if ( m_lastChar
== EOF
)
182 return m_stream
.Ungetch((char) c
) ? c
: EOF
;
185 // ==========================================================================
186 // wxStdOutputStreamBuffer
187 // ==========================================================================
189 wxStdOutputStreamBuffer::wxStdOutputStreamBuffer(wxOutputStream
& stream
) :
195 wxStdOutputStreamBuffer::setbuf(char *WXUNUSED(s
),
196 std::streamsize
WXUNUSED(n
))
202 wxStdOutputStreamBuffer::seekoff(std::streamoff off
,
203 std::ios_base::seekdir way
,
204 std::ios_base::openmode which
)
208 if ( !IosSeekDirToWxSeekMode(way
, seekMode
) )
210 if ( !(which
& std::ios_base::out
) )
213 off_t newPos
= m_stream
.SeekO((off_t
) off
, seekMode
);
215 if ( newPos
!= wxInvalidOffset
)
216 return (std::streampos
) newPos
;
222 wxStdOutputStreamBuffer::seekpos(std::streampos sp
,
223 std::ios_base::openmode which
)
225 if ( !(which
& std::ios_base::out
) )
228 off_t newPos
= m_stream
.SeekO((off_t
) sp
);
230 if ( newPos
!= wxInvalidOffset
)
231 return (std::streampos
) newPos
;
237 wxStdOutputStreamBuffer::xsputn(const char *s
,
240 m_stream
.Write((const void *) s
, (size_t) n
);
241 return (std::streamsize
) m_stream
.LastWrite();
245 wxStdOutputStreamBuffer::overflow(int c
)
248 return m_stream
.IsOk() ? c
: EOF
;
251 // ==========================================================================
252 // wxStdInputStream and wxStdOutputStream
253 // ==========================================================================
255 // FIXME-VC6: it is impossible to call basic_ios<char>::init() with this
256 // compiler, it complains about invalid call to non-static member
257 // function so use a suspicious (as it uses a pointer to not yet
258 // constructed streambuf) but working workaround
260 // It also doesn't like using istream in the ctor initializer list
261 // and we must spell it out as basic_istream<char>.
264 wxStdInputStream::wxStdInputStream(wxInputStream
& stream
)
265 : std::basic_istream
<char, std::char_traits
<char> >(&m_streamBuffer
),
266 m_streamBuffer(stream
)
270 wxStdOutputStream::wxStdOutputStream(wxOutputStream
& stream
)
271 : std::basic_ostream
<char, std::char_traits
<char> >(&m_streamBuffer
),
272 m_streamBuffer(stream
)
278 wxStdInputStream::wxStdInputStream(wxInputStream
& stream
) :
279 std::istream(NULL
), m_streamBuffer(stream
)
281 std::ios::init(&m_streamBuffer
);
284 wxStdOutputStream::wxStdOutputStream(wxOutputStream
& stream
) :
285 std::ostream(NULL
), m_streamBuffer(stream
)
287 std::ios::init(&m_streamBuffer
);
292 #endif // wxUSE_STREAMS && wxUSE_STD_IOSTREAM