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 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
29 wxMemoryStreamBase
::wxMemoryStreamBase()
37 wxMemoryStreamBase
::~wxMemoryStreamBase()
39 if (!m_persistent
&& m_buffer
)
43 bool wxMemoryStreamBase
::ChangeBufferSize(size_t new_size
)
50 m_buffer
= (char *)malloc(m_length
);
52 m_buffer
= (char *)realloc(m_buffer
, m_length
);
54 return (m_buffer
!= NULL
);
57 // ----------------------------------------------------------------------------
58 // wxMemoryInputStream
59 // ----------------------------------------------------------------------------
61 wxMemoryInputStream
::wxMemoryInputStream(const char *data
, size_t len
)
65 m_buffer
= (char *)data
; // It's bad.
72 wxMemoryInputStream
::~wxMemoryInputStream()
76 wxInputStream
& wxMemoryInputStream
::Read(void *buffer
, size_t size
)
82 if (m_position_i
+size
> m_length
)
83 size
= m_length
-m_position_i
;
85 memcpy((void *)((unsigned long)buffer
+m_position_i
), m_buffer
, size
);
92 off_t wxMemoryInputStream
::SeekI(off_t pos
, wxSeekMode mode
)
99 if ((size_t)pos
> m_length
)
101 return (m_position_i
= pos
);
104 if ((size_t)(m_position_i
+pos
) > m_length
)
107 return (m_position_i
+= pos
);
110 if ((size_t)(m_length
-pos
) > m_length
)
113 return (m_position_i
= m_length
-pos
);
119 // ----------------------------------------------------------------------------
120 // wxMemoryOutputStream
121 // ----------------------------------------------------------------------------
123 wxMemoryOutputStream
::wxMemoryOutputStream(char *data
, size_t len
)
125 m_persistent
= FALSE
;
134 wxMemoryOutputStream
::~wxMemoryOutputStream()
138 wxOutputStream
& wxMemoryOutputStream
::Write(const void *buffer
, size_t size
)
140 if (m_iolimit
== 1) {
145 if (m_position_o
+size
> m_length
)
146 if (!ChangeBufferSize(m_position_o
+size
)) {
151 memcpy(m_buffer
+m_position_o
, buffer
, size
);
152 m_position_o
+= size
;
158 off_t wxMemoryOutputStream
::SeekO(off_t pos
, wxSeekMode mode
)
165 if ((size_t)pos
> m_length
)
167 return (m_position_o
= pos
);
170 if ((size_t)(m_position_o
+pos
) > m_length
)
173 return (m_position_o
+= pos
);
176 if ((size_t)(m_length
-pos
) > m_length
)
179 return (m_position_o
= m_length
-pos
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 wxMemoryStream
::wxMemoryStream(char *data
, size_t len
)
190 : wxMemoryInputStream(NULL
, 0), wxMemoryOutputStream(NULL
, 0)
192 m_persistent
= FALSE
;
198 wxMemoryStream
::~wxMemoryStream()