stdstream.h 4.3 KB

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