1 /////////////////////////////////////////////////////////////////////////////// 2 // Name: wx/sstream.h 3 // Purpose: string-based streams 4 // Author: Vadim Zeitlin 5 // Modified by: 6 // Created: 2004-09-19 7 // RCS-ID: $Id: sstream.h 45732 2007-05-01 13:52:19Z VZ $ 8 // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org> 9 // Licence: wxWindows licence 10 /////////////////////////////////////////////////////////////////////////////// 11 12 #ifndef _WX_SSTREAM_H_ 13 #define _WX_SSTREAM_H_ 14 15 #include "wx/stream.h" 16 17 #if wxUSE_STREAMS 18 19 // ---------------------------------------------------------------------------- 20 // wxStringInputStream is a stream reading from the given (fixed size) string 21 // ---------------------------------------------------------------------------- 22 23 class WXDLLIMPEXP_BASE wxStringInputStream : public wxInputStream 24 { 25 public: 26 // ctor associates the stream with the given string which makes a copy of 27 // it 28 wxStringInputStream(const wxString& s); 29 virtual ~wxStringInputStream(); 30 31 virtual wxFileOffset GetLength() const; 32 33 protected: 34 virtual wxFileOffset OnSysSeek(wxFileOffset ofs, wxSeekMode mode); 35 virtual wxFileOffset OnSysTell() const; 36 virtual size_t OnSysRead(void *buffer, size_t size); 37 38 private: 39 // the string that was passed in the ctor 40 wxString m_str; 41 42 // the buffer we're reading from 43 char* m_buf; 44 45 // length of the buffer we're reading from 46 size_t m_len; 47 48 // position in the stream in bytes, *not* in chars 49 size_t m_pos; 50 51 DECLARE_NO_COPY_CLASS(wxStringInputStream) 52 }; 53 54 // ---------------------------------------------------------------------------- 55 // wxStringOutputStream writes data to the given string, expanding it as needed 56 // ---------------------------------------------------------------------------- 57 58 class WXDLLIMPEXP_BASE wxStringOutputStream : public wxOutputStream 59 { 60 public: 61 // The stream will write data either to the provided string or to an 62 // internal string which can be retrieved using GetString() 63 wxStringOutputStream(wxString *pString = NULL) 64 { 65 m_str = pString ? pString : &m_strInternal; 66 m_pos = m_str->length() / sizeof(wxChar); 67 } 68 69 #if wxABI_VERSION >= 20804 && wxUSE_UNICODE 70 virtual ~wxStringOutputStream(); 71 #endif // wx 2.8.4+ 72 73 // get the string containing current output GetString()74 const wxString& GetString() const { return *m_str; } 75 76 protected: 77 virtual wxFileOffset OnSysTell() const; 78 virtual size_t OnSysWrite(const void *buffer, size_t size); 79 80 private: 81 // internal string, not used if caller provided his own string 82 wxString m_strInternal; 83 84 // pointer given by the caller or just pointer to m_strInternal 85 wxString *m_str; 86 87 // position in the stream in bytes, *not* in chars 88 size_t m_pos; 89 90 #if wxUSE_WCHAR_T 91 // string encoding converter (UTF8 is the standard) 92 wxMBConvUTF8 m_conv; 93 #else 94 wxMBConv m_conv; 95 #endif 96 97 DECLARE_NO_COPY_CLASS(wxStringOutputStream) 98 }; 99 100 #endif // wxUSE_STREAMS 101 102 #endif // _WX_SSTREAM_H_ 103 104