sstream.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/sstream.h
  3. // Purpose: string-based streams
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 2004-09-19
  7. // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_SSTREAM_H_
  11. #define _WX_SSTREAM_H_
  12. #include "wx/stream.h"
  13. #if wxUSE_STREAMS
  14. // ----------------------------------------------------------------------------
  15. // wxStringInputStream is a stream reading from the given (fixed size) string
  16. // ----------------------------------------------------------------------------
  17. class WXDLLIMPEXP_BASE wxStringInputStream : public wxInputStream
  18. {
  19. public:
  20. // ctor associates the stream with the given string which makes a copy of
  21. // it
  22. wxStringInputStream(const wxString& s);
  23. virtual wxFileOffset GetLength() const;
  24. virtual bool IsSeekable() const { return true; }
  25. protected:
  26. virtual wxFileOffset OnSysSeek(wxFileOffset ofs, wxSeekMode mode);
  27. virtual wxFileOffset OnSysTell() const;
  28. virtual size_t OnSysRead(void *buffer, size_t size);
  29. private:
  30. // the string that was passed in the ctor
  31. wxString m_str;
  32. // the buffer we're reading from
  33. wxCharBuffer m_buf;
  34. // length of the buffer we're reading from
  35. size_t m_len;
  36. // position in the stream in bytes, *not* in chars
  37. size_t m_pos;
  38. wxDECLARE_NO_COPY_CLASS(wxStringInputStream);
  39. };
  40. // ----------------------------------------------------------------------------
  41. // wxStringOutputStream writes data to the given string, expanding it as needed
  42. // ----------------------------------------------------------------------------
  43. class WXDLLIMPEXP_BASE wxStringOutputStream : public wxOutputStream
  44. {
  45. public:
  46. // The stream will write data either to the provided string or to an
  47. // internal string which can be retrieved using GetString()
  48. //
  49. // Note that the conversion object should have the life time greater than
  50. // this stream.
  51. wxStringOutputStream(wxString *pString = NULL,
  52. wxMBConv& conv = wxConvUTF8)
  53. : m_conv(conv)
  54. #if wxUSE_UNICODE
  55. , m_unconv(0)
  56. #endif // wxUSE_UNICODE
  57. {
  58. m_str = pString ? pString : &m_strInternal;
  59. m_pos = m_str->length() / sizeof(wxChar);
  60. }
  61. // get the string containing current output
  62. const wxString& GetString() const { return *m_str; }
  63. virtual bool IsSeekable() const { return true; }
  64. protected:
  65. virtual wxFileOffset OnSysTell() const;
  66. virtual size_t OnSysWrite(const void *buffer, size_t size);
  67. private:
  68. // internal string, not used if caller provided his own string
  69. wxString m_strInternal;
  70. // pointer given by the caller or just pointer to m_strInternal
  71. wxString *m_str;
  72. // position in the stream in bytes, *not* in chars
  73. size_t m_pos;
  74. // converter to use: notice that with the default UTF-8 one the input
  75. // stream must contain valid UTF-8 data, use wxConvISO8859_1 to work with
  76. // arbitrary 8 bit data
  77. wxMBConv& m_conv;
  78. #if wxUSE_UNICODE
  79. // unconverted data from the last call to OnSysWrite()
  80. wxMemoryBuffer m_unconv;
  81. #endif // wxUSE_UNICODE
  82. wxDECLARE_NO_COPY_CLASS(wxStringOutputStream);
  83. };
  84. #endif // wxUSE_STREAMS
  85. #endif // _WX_SSTREAM_H_