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