| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/stdstream.h |
| 3 | // Purpose: Header of std::istream and std::ostream derived wrappers for |
| 4 | // wxInputStream and wxOutputStream |
| 5 | // Author: Jonathan Liu <net147@gmail.com> |
| 6 | // Created: 2009-05-02 |
| 7 | // Copyright: (c) 2009 Jonathan Liu |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef _WX_STDSTREAM_H_ |
| 12 | #define _WX_STDSTREAM_H_ |
| 13 | |
| 14 | #include "wx/defs.h" // wxUSE_STD_IOSTREAM |
| 15 | |
| 16 | #if wxUSE_STREAMS && wxUSE_STD_IOSTREAM |
| 17 | |
| 18 | #include "wx/defs.h" |
| 19 | #include "wx/stream.h" |
| 20 | #include "wx/ioswrap.h" |
| 21 | |
| 22 | // ========================================================================== |
| 23 | // wxStdInputStreamBuffer |
| 24 | // ========================================================================== |
| 25 | |
| 26 | class WXDLLIMPEXP_BASE wxStdInputStreamBuffer : public std::streambuf |
| 27 | { |
| 28 | public: |
| 29 | wxStdInputStreamBuffer(wxInputStream& stream); |
| 30 | virtual ~wxStdInputStreamBuffer() { } |
| 31 | |
| 32 | protected: |
| 33 | virtual std::streambuf *setbuf(char *s, std::streamsize n); |
| 34 | virtual std::streampos seekoff(std::streamoff off, |
| 35 | std::ios_base::seekdir way, |
| 36 | std::ios_base::openmode which = |
| 37 | std::ios_base::in | |
| 38 | std::ios_base::out); |
| 39 | virtual std::streampos seekpos(std::streampos sp, |
| 40 | std::ios_base::openmode which = |
| 41 | std::ios_base::in | |
| 42 | std::ios_base::out); |
| 43 | virtual std::streamsize showmanyc(); |
| 44 | virtual std::streamsize xsgetn(char *s, std::streamsize n); |
| 45 | virtual int underflow(); |
| 46 | virtual int uflow(); |
| 47 | virtual int pbackfail(int c = EOF); |
| 48 | |
| 49 | // Special work around for VC8/9 (this bug was fixed in VC10 and later): |
| 50 | // these versions have non-standard _Xsgetn_s() that it being called from |
| 51 | // the stream code instead of xsgetn() and so our overridden implementation |
| 52 | // never actually gets used. To work around this, forward to it explicitly. |
| 53 | #if defined(__VISUALC8__) || defined(__VISUALC9__) |
| 54 | virtual std::streamsize |
| 55 | _Xsgetn_s(char *s, size_t WXUNUSED(size), std::streamsize n) |
| 56 | { |
| 57 | return xsgetn(s, n); |
| 58 | } |
| 59 | #endif // VC8 or VC9 |
| 60 | |
| 61 | wxInputStream& m_stream; |
| 62 | int m_lastChar; |
| 63 | }; |
| 64 | |
| 65 | // ========================================================================== |
| 66 | // wxStdInputStream |
| 67 | // ========================================================================== |
| 68 | |
| 69 | class WXDLLIMPEXP_BASE wxStdInputStream : public std::istream |
| 70 | { |
| 71 | public: |
| 72 | wxStdInputStream(wxInputStream& stream); |
| 73 | virtual ~wxStdInputStream() { } |
| 74 | |
| 75 | protected: |
| 76 | wxStdInputStreamBuffer m_streamBuffer; |
| 77 | }; |
| 78 | |
| 79 | // ========================================================================== |
| 80 | // wxStdOutputStreamBuffer |
| 81 | // ========================================================================== |
| 82 | |
| 83 | class WXDLLIMPEXP_BASE wxStdOutputStreamBuffer : public std::streambuf |
| 84 | { |
| 85 | public: |
| 86 | wxStdOutputStreamBuffer(wxOutputStream& stream); |
| 87 | virtual ~wxStdOutputStreamBuffer() { } |
| 88 | |
| 89 | protected: |
| 90 | virtual std::streambuf *setbuf(char *s, std::streamsize n); |
| 91 | virtual std::streampos seekoff(std::streamoff off, |
| 92 | std::ios_base::seekdir way, |
| 93 | std::ios_base::openmode which = |
| 94 | std::ios_base::in | |
| 95 | std::ios_base::out); |
| 96 | virtual std::streampos seekpos(std::streampos sp, |
| 97 | std::ios_base::openmode which = |
| 98 | std::ios_base::in | |
| 99 | std::ios_base::out); |
| 100 | virtual std::streamsize xsputn(const char *s, std::streamsize n); |
| 101 | virtual int overflow(int c); |
| 102 | |
| 103 | wxOutputStream& m_stream; |
| 104 | }; |
| 105 | |
| 106 | // ========================================================================== |
| 107 | // wxStdOutputStream |
| 108 | // ========================================================================== |
| 109 | |
| 110 | class WXDLLIMPEXP_BASE wxStdOutputStream : public std::ostream |
| 111 | { |
| 112 | public: |
| 113 | wxStdOutputStream(wxOutputStream& stream); |
| 114 | virtual ~wxStdOutputStream() { } |
| 115 | |
| 116 | protected: |
| 117 | wxStdOutputStreamBuffer m_streamBuffer; |
| 118 | }; |
| 119 | |
| 120 | #endif // wxUSE_STREAMS && wxUSE_STD_IOSTREAM |
| 121 | |
| 122 | #endif // _WX_STDSTREAM_H_ |