]> git.saurik.com Git - wxWidgets.git/blob - include/wx/sstream.h
added wxStringStream classes
[wxWidgets.git] / include / wx / sstream.h
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$
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 : m_str(s)
30 {
31 m_pos = 0;
32 }
33
34 protected:
35 virtual size_t GetSize() const { return m_str.length(); }
36 virtual off_t OnSysSeek(off_t ofs, wxSeekMode mode);
37 virtual off_t OnSysTell() const;
38 virtual size_t OnSysRead(void *buffer, size_t size);
39
40 private:
41 // the string we're reading from
42 wxString m_str;
43
44 // position in the stream in bytes, *not* in chars
45 size_t m_pos;
46
47
48 DECLARE_NO_COPY_CLASS(wxStringInputStream)
49 };
50
51 // ----------------------------------------------------------------------------
52 // wxStringOutputStream writes data to the given string, expanding it as needed
53 // ----------------------------------------------------------------------------
54
55 class WXDLLIMPEXP_BASE wxStringOutputStream : public wxOutputStream
56 {
57 public:
58 // The stream will write data either to the provided string or to an
59 // internal string which can be retrieved using GetString()
60 wxStringOutputStream(wxString *pString = NULL)
61 {
62 m_str = pString ? pString : &m_strInternal;
63 m_pos = m_str->length() / sizeof(wxChar);
64 }
65
66 // get the string containing current output
67 const wxString& GetString() const { return *m_str; }
68
69 protected:
70 virtual size_t OnSysWrite(const void *buffer, size_t size);
71
72 private:
73 // internal string, not used if caller provided his own string
74 wxString m_strInternal;
75
76 // pointer given by the caller or just pointer to m_strInternal
77 wxString *m_str;
78
79 // position in the stream in bytes, *not* in chars
80 size_t m_pos;
81
82
83 DECLARE_NO_COPY_CLASS(wxStringOutputStream)
84 };
85
86 #endif // wxUSE_STREAMS
87
88 #endif // _WX_SSTREAM_H_
89