]>
git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
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 wxMemoryInputStream::~wxMemoryInputStream()
82 wxInputStream
& wxMemoryInputStream::Read(void *buffer
, size_t size
)
88 if (m_position_i
+size
> m_length
)
89 size
= m_length
-m_position_i
;
91 memcpy((void *)((unsigned long)buffer
+m_position_i
), m_buffer
, size
);
98 off_t
wxMemoryInputStream::SeekI(off_t pos
, wxSeekMode mode
)
105 if ((size_t)pos
> m_length
)
107 return (m_position_i
= pos
);
110 if ((size_t)(m_position_i
+pos
) > m_length
)
113 return (m_position_i
+= pos
);
116 if ((size_t)(m_length
-pos
) > m_length
)
119 return (m_position_i
= m_length
-pos
);
125 // ----------------------------------------------------------------------------
126 // wxMemoryOutputStream
127 // ----------------------------------------------------------------------------
129 wxMemoryOutputStream::wxMemoryOutputStream(char *data
, size_t len
)
131 m_persistent
= FALSE
;
140 wxMemoryOutputStream::~wxMemoryOutputStream()
144 wxOutputStream
& wxMemoryOutputStream::Write(const void *buffer
, size_t size
)
146 if (m_iolimit
== 1) {
151 if (m_position_o
+size
> m_length
)
152 if (!ChangeBufferSize(m_position_o
+size
)) {
157 memcpy(m_buffer
+m_position_o
, buffer
, size
);
158 m_position_o
+= size
;
164 off_t
wxMemoryOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
171 if ((size_t)pos
> m_length
)
173 return (m_position_o
= pos
);
176 if ((size_t)(m_position_o
+pos
) > m_length
)
179 return (m_position_o
+= pos
);
182 if ((size_t)(m_length
-pos
) > m_length
)
185 return (m_position_o
= m_length
-pos
);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 wxMemoryStream::wxMemoryStream(char *data
, size_t len
)
196 : wxMemoryInputStream(NULL
, 0), wxMemoryOutputStream(NULL
, 0)
198 m_persistent
= FALSE
;
204 wxMemoryStream::~wxMemoryStream()