1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "Memory stream" classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mstream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #include <wx/stream.h>
20 #include <wx/mstream.h>
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_CLASS(wxMemoryInputStream
, wxInputStream
)
28 IMPLEMENT_CLASS(wxMemoryOutputStream
, wxOutputStream
)
29 IMPLEMENT_CLASS2(wxMemoryStream
, wxInputStream
, wxOutputStream
)
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
35 wxMemoryStreamBase
::wxMemoryStreamBase()
43 wxMemoryStreamBase
::~wxMemoryStreamBase()
45 if (!m_persistent
&& m_buffer
)
49 bool wxMemoryStreamBase
::ChangeBufferSize(size_t new_size
)
56 m_buffer
= (char *)malloc(m_length
);
58 m_buffer
= (char *)realloc(m_buffer
, m_length
);
60 return (m_buffer
!= NULL
);
63 // ----------------------------------------------------------------------------
64 // wxMemoryInputStream
65 // ----------------------------------------------------------------------------
67 wxMemoryInputStream
::wxMemoryInputStream(const char *data
, size_t len
)
71 m_buffer
= (char *)data
; // It's bad.
78 wxInputStream
& wxMemoryInputStream
::Read(void *buffer
, size_t size
)
84 if (m_position_i
+size
> m_length
)
85 size
= m_length
-m_position_i
;
87 memcpy((void *)((unsigned long)buffer
+m_position_i
), m_buffer
, size
);
94 off_t wxMemoryInputStream
::SeekI(off_t pos
, wxSeekMode mode
)
101 if ((size_t)pos
> m_length
)
103 return (m_position_i
= pos
);
106 if ((size_t)(m_position_i
+pos
) > m_length
)
109 return (m_position_i
+= pos
);
112 if ((size_t)(m_length
-pos
) > m_length
)
115 return (m_position_i
= m_length
-pos
);
121 // ----------------------------------------------------------------------------
122 // wxMemoryOutputStream
123 // ----------------------------------------------------------------------------
125 wxMemoryOutputStream
::wxMemoryOutputStream(char *data
, size_t len
)
127 m_persistent
= FALSE
;
136 wxOutputStream
& wxMemoryOutputStream
::Write(const void *buffer
, size_t size
)
138 if (m_iolimit
== 1) {
143 if (m_position_o
+size
> m_length
)
144 if (!ChangeBufferSize(m_position_o
+size
)) {
149 memcpy(m_buffer
+m_position_o
, buffer
, size
);
150 m_position_o
+= size
;
156 off_t wxMemoryOutputStream
::SeekO(off_t pos
, wxSeekMode mode
)
163 if ((size_t)pos
> m_length
)
165 return (m_position_o
= pos
);
168 if ((size_t)(m_position_o
+pos
) > m_length
)
171 return (m_position_o
+= pos
);
174 if ((size_t)(m_length
-pos
) > m_length
)
177 return (m_position_o
= m_length
-pos
);
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 wxMemoryStream
::wxMemoryStream(char *data
, size_t len
)
188 : wxMemoryInputStream(NULL
, 0), wxMemoryOutputStream(NULL
, 0)
190 m_persistent
= FALSE
;
196 wxMemoryStream
::~wxMemoryStream()