]>
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 // ----------------------------------------------------------------------------
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.
71 m_i_streambuf
->SetBufferIO(0);
74 wxMemoryInputStream::~wxMemoryInputStream()
78 char wxMemoryInputStream::Peek()
80 // wxStreamBuffer is disabled so just peek the current character.
82 return m_buffer
[m_position_i
];
85 size_t wxMemoryInputStream::DoRead(void *buffer
, size_t size
)
91 if (m_position_i
+size
> m_length
)
92 size
= m_length
-m_position_i
;
94 memcpy((void *)((unsigned long)buffer
+m_position_i
), m_buffer
, size
);
100 off_t
wxMemoryInputStream::DoSeekInput(off_t pos
, wxSeekMode mode
)
107 if ((size_t)pos
> m_length
)
109 return (m_position_i
= pos
);
112 if ((size_t)(m_position_i
+pos
) > m_length
)
115 return (m_position_i
+= pos
);
118 if ((size_t)(m_length
-pos
) > m_length
)
121 return (m_position_i
= m_length
-pos
);
127 // ----------------------------------------------------------------------------
128 // wxMemoryOutputStream
129 // ----------------------------------------------------------------------------
131 wxMemoryOutputStream::wxMemoryOutputStream(char *data
, size_t len
)
133 m_persistent
= FALSE
;
141 m_o_streambuf
->SetBufferIO(0);
144 wxMemoryOutputStream::~wxMemoryOutputStream()
149 size_t wxMemoryOutputStream::DoWrite(const void *buffer
, size_t size
)
151 if (m_iolimit
== 1) {
156 if (m_position_o
+size
> m_length
)
157 if (!ChangeBufferSize(m_position_o
+size
)) {
162 memcpy(m_buffer
+m_position_o
, buffer
, size
);
163 m_position_o
+= size
;
168 off_t
wxMemoryOutputStream::DoSeekOutput(off_t pos
, wxSeekMode mode
)
175 if ((size_t)pos
> m_length
)
177 return (m_position_o
= pos
);
180 if ((size_t)(m_position_o
+pos
) > m_length
)
183 return (m_position_o
+= pos
);
186 if ((size_t)(m_length
-pos
) > m_length
)
189 return (m_position_o
= m_length
-pos
);
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 wxMemoryStream::wxMemoryStream(char *data
, size_t len
)
200 : wxMemoryInputStream(NULL
, 0), wxMemoryOutputStream(NULL
, 0)
202 m_persistent
= FALSE
;
208 wxMemoryStream::~wxMemoryStream()